Helpful Information
 
 
Category: Programming Languages More
XSL/XPATH

Hello everyone I have a question for you regarding how to form an XPath expressions in XSL. The help on it is quite limited and I was hoping someone would know here. Here is the situation.

I have a tag like this:
<IP IPType="0"></IP>

these tags can be nested within each other and hence have siblings, descendants and anscestors. What I want to do is from the current position within the nodeset, see if out of all the SIBLINGS the attribute IPType (where it is equal to 0) is the last of the siblings. If anyone can form the XPath expression or lead me to an answer I would be very appreciative.

thanks in advance,

rudy

I have created an example xml file to what I think it is what you meant. It is:-

<?xml version="1.0" encoding="UTF-8"?>
<root>
<IP IPType="0">Level1
<IP IPType="0">Level2</IP>
<IP IPType="0">Level2
<IP IPType="0">Level3
<IP IPType="0">Level4
<IP IPType="0">Level5</IP>
</IP>
<IP IPType="2">Level4</IP>
</IP>
</IP>
</IP>
<IP IPType="0">Level1
<IP IPType="0">Level2</IP>
</IP>
<IP IPType="0">Level1</IP>
<IP IPType="1">Level1
<IP IPType="2">Level2</IP>
</IP>
<IP IPType="3">Level1</IP>
</root>

And in order to choose the last (deepest) IP with the IPType value of 0 from them all, I used this xsl:-


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="root">
<!-- the next line selects only those nodes which match your two criteria! -->
<xsl:apply-templates select="//IP[@IPType = '0'][not(child::IP)]" />
</xsl:template>

<xsl:template match="IP">
<!-- Put what you want to do with the selected node here! -->
</xsl:template>

</xsl:stylesheet>


Hope this helps...