Monday, July 22, 2013

How to get index of node based on node value using XSLT?


While working with XML in some of the cases, we want to get the index or position of particular value. This can be easily done by using XSLT.

In this article, I am explaining - How to get index of node based on node value using XSLT?

Example-

Suppose, I have this XML data-

<ResponseData>
    <value>C</value>
    <value>A</value>
    <value>B</value>
</Response>

Now if I want to get the position of ‘A’ then It should return '2' .

How to get it-

This is small XSLT code to get this position-

<!--Declaring Variable with value-->
<xsl:variable name="TempData" select="A"/>
 

<!--XSLT code to get position of 'A', output will be 2 -->
<xsl:value-of select="count(//abc:ResponseData/abc:value[text()=$TempData]/preceding-sibling::*)+1" />


In above line I have declared one variable - 'TempData'
and using select I have assigned value -'A'


Now to get the position of 'A', I have used count() method.

Here this expression -

count(//abc:ResponseData/abc:value[text()=$TempData]/preceding-sibling::*)+1

Will return the count of preceding sibling of A, which will be position of 'A'.

Like 'A', we can also get the position of other Values ('B' and 'C'), for this we need to assign this value to 'TempData' variable.



Thanks


4 comments:

  1. Everyone loves it whenever people come together and share
    opinions. Great site, continue the good work!

    Also visit my webpage - Payday Loans Sackville Nova Scotia

    ReplyDelete
  2. Greetings! Very useful advice within this article! It is the little
    changes that produce the most important changes.
    Thanks for sharing!

    Visit my page watch video

    ReplyDelete
  3. I believe what you published made a lot of sense. However, consider
    this, suppose you wrote a catchier title? I ain't saying your content isn't solid, but suppose you added something
    to maybe grab a person's attention? I mean "How to get index of node based on node value using XSLT?" is a little boring.
    You could look at Yahoo's home page and watch how they create news headlines to grab viewers to open the links.
    You might try adding a video or a related pic or two to grab readers excited about what you've got to say.
    Just my opinion, it might make your posts a little livelier.


    Absolutely nobody has to find out about your money troubles because you don't even need to leave your house.
    Dishonest Lenders, Who Sometimes Break The Law - Some online lenders charge more interest than state laws allow.
    One just should simply fill the application form form with simple details like name, info,
    employment and income status. payday loans calgary online Absolutely
    nobody has to find out about your money troubles because
    you don't even need to leave your house. Dishonest Lenders, Who Sometimes Break The Law - Some online lenders charge more interest than state
    laws allow. One just should simply fill the application form form
    with simple details like name, info, employment and income status.

    ReplyDelete