1

我有一个需要转换为 html 的 xml,它包含数字及其奇偶校验。我需要创建两列,一列用于偶数,另一列用于添加,但我不能将其放入两列

XML 代码

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="numbers.xsl"?>
<Whole_Numbers>
     <Number>
          <Value>1</Value>
          <Parity>odd</Parity>
     </Number>
     <Number>
          <Value>2</Value>
          <Parity>even</Parity>
     </Number>
     <Number>
          <Value>3</Value>
          <Parity>odd</Parity>
     </Number>
     <Number>
          <Value>4</Value>
          <Parity>even</Parity>
     </Number>
</Whole_Numbers>

XSLT -

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/">
  <html>
  <body>
  <h2>Whole Numbers</h2>
  <table border="1">
    <tr >
      <th>Odd Numbers between 1 and 100</th>
      <th>Even Numbers between 1 and 100</th>
    </tr>
    <xsl:for-each select="Whole_Numbers/Number">
    <tr>
     <xsl:if test="contains(Parity, 'odd')">
      <td><xsl:value-of select="Value"/></td>
     </xsl:if>
    </tr>
    </xsl:for-each>     
  </table>
  </body>
  </html>
</xsl:template>

输出 - 奇数列中只有奇数,如果我包含另一个 td 标签它不起作用

4

1 回答 1

1

如果他们肯定像这样交替奇偶奇偶,那么你可以只在奇Number元素上 for-each ,然后使用它following-sibling::Number[1]来获得紧随其后的偶元素:

<xsl:for-each select="Whole_Numbers/Number[Parity='odd']">
  <tr>
    <td><xsl:value-of select="Value"/></td>
    <td><xsl:value-of select="following-sibling::Number[1]/Value"/></td>
  </tr>
</xsl:for-each>
于 2013-05-29T21:49:19.260 回答