I have an XML document structured as follows
<items>
<item>
<name>item1</name>
<attributes>a,b,c,d</attributes>
</item>
<item>
<name>item2</name>
<attributes>c,d,e</attributes>
</item>
</items>
For each unique attribute value (delimited by commas) I need to list all item names associated with that value like so:
a : item1
b : item1
c : item1, item2
d : item1, item2
e : item2
My initial plan was to use a template to parse the attributes into Attribute nodes, surrounding each with appropriate tags, and then separating out the unique values with an XPATH expression like
Attribute[not(.=following::Attribute)]
but since the result of the template isn't a node-set that ever goes through an XML parser, I can't traverse it. I also tried exslt's node-set() function only to realize it does not allow me to traverse the individual Attribute nodes either.
At this point I'm at a loss for a simple way to do this and would really appreciate any help or ideas on how to proceed. Thanks!
My first thought is to make two passes. First, tokenize the
attributes
elements using a (slightly) modified version of @Alejandro's answer to this previous question:Which produces:
Then apply the following stylesheet to that output:
Producing:
Doing this in one stylesheet would require more thought (or an extension function).
This transformation:
when applied on the provided XML document:
produces the wanted, correct result:
Explanation:
.2. Pass2 takes the result of Pass1 (converted to a nodeset using the extension function
ext:node-set()
) as input, performs Muenchian grouping and produces the final, wanted result.