Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSL - Previous node value in the loop

Tags:

xml

xslt

I have an XML like this - (simplified)

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

I am using this XSL to find the previous value of similar node in the loop. I simiplified the xsl here, i need to fit this logic in a bigger set..

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

I tried with ancestor and previous-sibling, doesnt work for me.. I am expecting below result

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

any ideas please ?

like image 687
Sr7 Avatar asked Dec 26 '22 20:12

Sr7


1 Answers

Use:

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

The complete transformation becomes:

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

and the wanted, correct result is produced:

<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>
like image 181
Dimitre Novatchev Avatar answered Dec 29 '22 11:12

Dimitre Novatchev