2012-07-27 57 views
1

上一頁節點值我有一個這樣的XML -XSL - 在循環

<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找到環路同類節點以前的值(簡化)。我在這裏simiplified的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 ?? 

有什麼想法嗎?

+0

因此T他'PreviousValue'應該是前一個兄弟的'PartNo'?即對於第一個條目,將不會有'PreviousValue'? – Utkanos 2012-07-27 12:49:12

回答

4

使用

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> 
2

我認爲<xsl:value-of select="preceding-sibling::BundleDetail[1]/BundleUnit/Idset/PartNo"/>是你在找什麼。