xsl - last preceding sibling
Asked Answered
B

1

5

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 ?

Bah answered 27/7, 2012 at 2:36 Comment(0)
U
9

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

Untried answered 27/7, 2012 at 2:48 Comment(2)
michael, could you please explain what the "*[1]" syntax means?Microclimatology
The ::* means select elements whatever their name, and the [1] means select the first (in axis order) (which is the last in document order).Untried

© 2022 - 2024 — McMap. All rights reserved.