Sample XML is given below.
<mapNode>
<mapNode>...</mapNode>
<mapNode>...</mapNode>-----I am here at 2
<mapNode>...</mapNode>
<mapNode>...</mapNode>
</mapNode>
<mapNode>
<mapNode>...</mapNode>
<mapNode>...</mapNode>
</mapNode>
I want to know whether position 3 exist or not. Please help me.
Thanks in advance.
No XSLT programmer use count () function for testing existence. @Alejandro, thanks for pointing out. I'm not using XSLT actively for three or four years already. Will try to catch up :) That's it! I was trying to do it with slash test="/somenode" and for some reason it doesn't work like that...
XSLT if statement is defined as a conditional pattern used to process a true statement and considered a simple Statement. The statement provides a simple test to use concerning the XML file. If a certain condition is met, the respective code is executed. It takes a test attribute to return a Boolean value through the XPath expression.
If you want to test if an element has a sibling following it, you can use the sensibly named "following-sibling" xpath expression: Note that this will test if there is any following-sibling. If you only wanted to test for mapNode elements, you could do this Show activity on this post.
Different content is given only if a given condition is met. Given below are the examples of XSLT if: Here we do things in XML and XSLT versions for comparison evaluation. For our example, we are using the ebook concept as an input XML.
If you want to test if an element has a sibling following it, you can use the sensibly named "following-sibling" xpath expression:
<xsl:if test="following-sibling::*" />
Note that this will test if there is any following-sibling. If you only wanted to test for mapNode elements, you could do this
<xsl:if test="following-sibling::mapNode" />
However, this would also be true also in the following case, because following-sibling will look at all following siblings:
<mapNode>
<mapNode>...</mapNode>
<mapNode>...</mapNode>-----I am here at 2
<differentNode>...</differentNode>
<mapNode>...</mapNode>
</mapNode>
If you therefore want to check the most immediately following sibling was a mapNode element, you would do this:
<xsl:if test="following-sibling::*[1][self::mapNode]" />
In addition to @rene's answer you could also use the following-sibling
axis from within any mapNode
:
<xsl:template match="mapNode">
<xsl:if test="count(following-sibling::mapNode)>0">
<!-- has a successor -->
</xsl:if>
</xsl:template>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With