XSL - 循环中的上一个节点值

时间:2012-07-27 12:47:00

标签: xml xslt

我有这样的XML - (简化)

<OrderBundle>
    <BundleDetail>
        <BundleId>12312</BundleId>
        <BundleUnit>
            <Idset>
                <PartNo>807651</PartNo>
            </Idset>
        </BundleUnit>
    </BundleDetail>
    <BundleDetail>
        <BundleId>12112</BundleId>
        <BundleUnit>
            <Idset>
                <PartNo>807650</PartNo>
            </Idset>
        </BundleUnit>
    </BundleDetail>
    <BundleDetail>
        <BundleId>12412</BundleId>
        <BundleUnit>
            <Idset>
                <PartNo>807651</PartNo>
            </Idset>
        </BundleUnit>
    </BundleDetail>
    <BundleDetail>
        <BundleId>12612</BundleId>
        <BundleUnit>
            <Idset>
                <PartNo>807651</PartNo>
            </Idset>
        </BundleUnit>
    </BundleDetail>
</OrderBundle>

我正在使用此XSL来查找循环中类似节点的先前值。我在这里简化了xsl,我需要在更大的集合中使用这个逻辑..

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:template match="/">

  <html>
  <body>

    <table border="1">
        <tr>
            <th>Bundle Id</th>
            <th>PartNo</th>
            <th>PreviousValue</th>
        </tr>
      <xsl:for-each select="/OrderBundle/BundleDetail">
      <tr>
        <td> <xsl:value-of select="BundleId"/></td>
        <td><xsl:value-of select=" BundleUnit/Idset/PartNo" /> </td>
        <td> ?? <xsl:value-of select="ancestor::BundleUnit/Idset/PartNo"/>   </td>
      </tr>
      </xsl:for-each>
    </table>


  </body>
  </html>
</xsl:template>

我尝试过祖先和前兄弟,对我不起作用.. 我期待以下结果

B.Id  PartNo PreviousValue 
12312 807651 ??             --
12112 807650 ??             -- 807651 
12412 807651 ??             -- 807650 etc
12612 807651 ??  

请问任何想法?

1 个答案:

答案 0 :(得分:5)

使用

preceding-sibling::BundleDetail[1]/BundleUnit/Idset/PartNo

完整转化

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <xsl:template match="/">

      <html>
      <body>

        <table border="1">
            <tr>
                <th>Bundle Id</th>
                <th>PartNo</th>
                <th>PreviousValue</th>
            </tr>
          <xsl:for-each select="/OrderBundle/BundleDetail">
          <tr>
            <td> <xsl:value-of select="BundleId"/></td>
            <td><xsl:value-of select=" BundleUnit/Idset/PartNo" /> </td>
            <td><xsl:value-of
            select="preceding-sibling::BundleDetail[1]/BundleUnit/Idset/PartNo"/>   </td>
          </tr>
          </xsl:for-each>
        </table>


      </body>
      </html>
    </xsl:template>
</xsl:stylesheet>

并生成了想要的正确结果

<html xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <body>
      <table border="1">
         <tr>
            <th>Bundle Id</th>
            <th>PartNo</th>
            <th>PreviousValue</th>
         </tr>
         <tr>
            <td>12312</td>
            <td>807651</td>
            <td></td>
         </tr>
         <tr>
            <td>12112</td>
            <td>807650</td>
            <td>807651</td>
         </tr>
         <tr>
            <td>12412</td>
            <td>807651</td>
            <td>807650</td>
         </tr>
         <tr>
            <td>12612</td>
            <td>807651</td>
            <td>807651</td>
         </tr>
      </table>
   </body>
</html>