在表列中从左到右读取xml值

时间:2011-12-12 19:27:52

标签: html xml xslt

您好我正在尝试创建一个可以使用XML数据填充的动态表。表中有两行三列。第一行应为“@ n1three”,第二行应填充“@ n1three3”数据。在xml标记n1third内部,有三个值来填充列。情况可能并非总是如此,它可能只有两个,但无论是否存在数据,都应保留三列。

我可以将数据读出来,但绝不能以正确的格式显示。下面是我的XML和HTML代码。有人可以解释一下。感谢。

XML:

<numberNodes>
 <node pos="1">
  <n1first n1first="a1">
<n1second n1second="aa2">
   <n1third n1three = "aaa1" n1three3="23"/>
   <n1third n1three = "aaa2" n1three3="24"/>
   <n1third n1three = "aaa3" n1three3="25"/>
  </n1second>
   <n1second n1second="aa2">
   <n1third n1three = "aaa1" n1three3="23"/>
   <n1third n1three = "aaa2" n1three3="23"/>
   <n1third n1three = "aaa3" n1three3="23"/>
  </n1second>
 </n1first>
 </node>
</numberNodes>

HTML:

<table border="16" cellspacing="2">
 <xsl:for-each select="n1third">
<tr><td><xsl:value-of select="@n1three"/></td></tr>
  <tr>
   <td><xsl:value-of select="@n1three3"/></td>
  </tr>
 </xsl:for-each>
</table>

1 个答案:

答案 0 :(得分:0)

如果你不能确定你有3个第三个输入值,你将不得不使用递归:

<table border="16" cellspacing="2">
  <tr>
    <xsl:call-template name="writeRow">
       <xsl:with-param name="input" select="n1third/@n1three"/>
       <xsl:with-param name="cellCount" select="3"/>
    </xsl:call-template>
  </tr>
  <tr>
    <xsl:call-template name="writeRow">
       <xsl:with-param name="input" select="n1third/@n1three3"/>
       <xsl:with-param name="cellCount" select="3"/>
    </xsl:call-template>
  </tr>
</table>

writeRow定义如下:

<xsl:template name="writeRow">
  <xsl:param name="input"/>
  <xsl:param name="cellCount"/>
  <td><xsl:value-of select="$input[1]"/></td>
  <xsl:if test="$cellCount &gt; 1">
    <xsl:call-template name="writeRow">
       <xsl:with-param name="input" select="$input[position() &gt; 1]"/>
       <xsl:with-param name="cellCount" select="$cellCount - 1"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

(注意,可能包含错误)