Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT find whether sibling exist or not

Tags:

xslt

xpath

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.

like image 769
Ritesh Avatar asked Sep 24 '12 11:09

Ritesh


People also ask

Is there a way to test existence in XSLT?

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

What is an IF statement in XSLT?

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.

How to test if an element has a following sibling?

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.

When to use different content in XSLT?

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.


2 Answers

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]" />
like image 173
Tim C Avatar answered Oct 11 '22 19:10

Tim C


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>
like image 33
hielsnoppe Avatar answered Oct 11 '22 17:10

hielsnoppe