Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to compare current value with previous or next occurrence?

Status
Not open for further replies.

TonyMarston

Programmer
Dec 8, 1999
20
GB
After sorting my XML data I want to detect when one of the values changes so that I can do something extra, such as insert another line break. I cannot find how to compare an item value with the previous or next occurrence in the structure. Can anyone help me please?
 
There are various ways:

You can use [number] to get an occurance of a specific node
e.g: myxml/mynodes[1]

You can also use the position() function
e.g: myxml/mynodes[position() -1] points to the previous node.

Good luck,

Jordi Reineman
 
I know how to get the number of the previous node, but I am trying to get the value of an item in the previous node so I can compare it with the value of the same item in the current node.

What I am trying to do is step through a sorted list and perform some processing when a value changes between one occurrence and the next.
 
After a lot of trial and error I have managed to find the solution:

1) Create a variable to hold the current value of position()
2) Create a variable to hold the data from an item in the previous/next occurrence
3) Compare the item data in the current occurrence with the contents of variable (2).

Example code looks like this:

Code:
<xsl:variable name=&quot;position&quot; select=&quot;position()&quot; />
<xsl:variable name=&quot;next&quot; select=&quot;/UNIFACE/TABLE/OCC[$position+1]/USOURCE:UVAR&quot; />

<xsl:if test=&quot;USOURCE:UVAR != $next&quot;>
    .....
</xsl:if>

If you want the value from the previous occurrence then use [$position-1] instead.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top