I’ll admit I’m new to xslt and I’m going through various tutorials and documentation as I go, but I have a situation that I’m not sure how to look up the answer for.
Basically it is this. I have an xml document that looks like so…
<tag1> <tag2> foo </tag2> <tag3> bar </tag3> <tag2> goo </tag2> <tag3> mar </tag3> </tag1>
So the catch here is that the order is important. Items in tag2 and tag3 go together in groups. That is, ‘foo’ and ‘bar’ need to be processed together, as do ‘goo’ and ‘mar’.
I don’t know how to capture that in a construct. That is, I can match tag2 and tag3, but I don’t know how to get them in order, together.
Any ideas?
Here’s a straightforward way to do grouping if your source data is already ordered the way you expect:
This will apply templates to every odd-positioned child element of
tag1irrespective of name. You use a mode to limit the template that gets applied (because that template has to match*). Within the template, you can use thefollowing-siblingsearch axis to get the next node. This gives you something like:It’s even simpler if you can rely on the element names:
Note that if you’re grouping every N elements, you can get a list of the current element and its following N-1 siblings with: