Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xsl - last preceding sibling

Tags:

xml

siblings

xslt

I am stuck with a logic related to preceding sibling,

Trying to keep XML simple .

<order>
<orderList>
<itemid><id>100</id></itemid>
<itemid><id>100</id></itemid>
<itemid><id>111</id></itemid>
<itemid><id>111</id></itemid>
<itemid><id>123</id></itemid>
<itemid><id>324</id></itemid>
<itemid><id>244</id></itemid>
<itemid><id>244</id></itemid>
 </orderList>
</order>

I am trying to find the preceding sibling for each node using below xsl. i need to use for each loop to fit this logic in a larger xsl...

  <html>
  <body>
     <table border="1">
   <xsl:for-each select="order/orderList/itemid">
      <tr>
        <td>itemid</td>
        <td><xsl:value-of select="id" /> </td>
        <td> <xsl:value-of select="preceding-sibling::node()"/>   </td>
      </tr>
      </xsl:for-each>
    </table>    
  </body>
  </html>
</xsl:template>

I get these Results 

itemid 100  
itemid 100 100   
itemid 111 100 
itemid 111 100   - expecting 111
itemid 123 100   - expecting 111 etc
itemid 324 100 
itemid 244 100 
itemid 244 100 

any help please ?

like image 310
Sr7 Avatar asked Jul 27 '12 02:07

Sr7


People also ask

What is preceding :: sibling XSLT?

The preceding-sibling:: axis is an axis of navigation that includes all the preceding sibling elements to the focus element. By "sibling" we mean a different element which has the same parent to the reference item. By "preceding" we mean a node that occurs before the reference one.

What is following sibling in XSLT?

The following-sibling axis indicates all the nodes that have the same parent as the context node and appear after the context node in the source document.


1 Answers

In XSLT 1.0, xsl:value-of when given a node-set, returns the string value of the first node in that node-set, taken in document order. (XSLT 2.0 returns the string values of all the nodes in the node-set).

preceding-sibling::node() returns a node-set containing all the preceding siblings of a node.

If you only want the last preceding sibling, use preceding-sibling::*[1].

like image 102
Michael Kay Avatar answered Oct 07 '22 04:10

Michael Kay