I have this tree:
<Events>
<Properties>
<Property Descriptor=100>1378314022</Property>
<Property Descriptor=200>ABC1234</Property>
</Properties>
<Properties>
<Property Descriptor=100>1378314023</Property>
<Property Descriptor=200>ABC1234</Property>
</Properties>
<Properties>
<Property Descriptor=100>1378314024</Property>
<Property Descriptor=200>ABC1234</Property>
</Properties>
<Properties>
<Property Descriptor=100>1378314022</Property>
<Property Descriptor=200>123456</Property>
</Properties>
<Properties>
<Property Descriptor=100>1378314023</Property>
<Property Descriptor=200>123456</Property>
</Properties>
<Properties>
<Property Descriptor=100>1378314024</Property>
<Property Descriptor=200>123456</Property>
</Properties>
</Events>
I'm iterating at this level: Events/Properties
How can I have only the FIRST and LAST occurrence of each Property Descriptor = 200
and its respective Property Descriptor = 100
?
I've tried so far:
- Iteration:
Events/Properties
- Select:
Property[@Descriptor=200])[last()] or Property[@Descriptor=200])[first()]
but with no success.
OUTPUT should look like this [I'm showing it in HTML, iterating in the ROW level]:
P100 | P200
1378314022 | ABC1234
1378314024 | ABC1234
1378314022 | 123456
1378314024 | 123456
In XSLT 2.0 this would be easy with
for-each-group
, grouping by the 200 value and taking the first and last members of each group. But in pure XPath (not XSLT) you need to think laterally.If the groups are always contiguous as you've shown here (i.e. all the
ABC1234
entries are adjacent to one another, and all the123456
entries are adjacent to one another) then this boils down to wanting everyProperties
element P that does not have an immediately preceding and an immediately following siblingProperties
element with the same200
value as P. I.e. you want to iterate overand then select the
Property[@Descriptor="100"]
andProperty[@Descriptor="200"]
from each of the resultingProperties
elements.You've tagged your question "xpath-2.0" but this expression is also valid in XPath 1.0.
I am not completely sure which answer you are looking for, but here I will give you two examples. One of them should probably fit your needs.
I used the following input XML:
When I use the next XSLT:
This XSLT also has a iteration over the
Properties
element, within thatProperties
element it gets the first and the last occurence of each providedDescriptor
. The result would be:If you want to get the last en first occurence over all the
Properties
the XSLT should be slightly different:The result would then be: