4

我有一个这样的 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 ??  

请问有什么想法吗?

4

2 回答 2

8

使用

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>
于 2012-07-27T12:57:06.823 回答
2

我想<xsl:value-of select="preceding-sibling::BundleDetail[1]/BundleUnit/Idset/PartNo"/>这就是你要找的。

于 2012-07-27T12:56:26.213 回答