Using XSLT, how do I separate nodes based on their value?
Posted
by Will Goring
on Stack Overflow
See other posts from Stack Overflow
or by Will Goring
Published on 2010-03-25T15:30:04Z
Indexed on
2010/03/25
15:33 UTC
Read the original article
Hit count: 359
I have a pretty flat XML structure that I need to reorder into categorised sections and, for the life of me, I can't figure out how to do it in XSLT (not that I'm by any means an expert.)
Basically, the original XML looks kinda like:
<things>
<thing>
<value>one</value>
<type>a</type>
</thing>
<thing>
<value>two</value>
<type>b</type>
</thing>
<thing>
<value>thee</value>
<type>b</type>
</thing>
<thing>
<value>four</value>
<type>a</type>
</thing>
<thing>
<value>five</value>
<type>d</type>
</thing>
</things>
And I need to output something like:
<data>
<a-things>
<a>one</a>
<a>four</a>
</a-things>
<b-things>
<b>two</b>
<b>three</b>
</b-things>
<d-things>
<d>five</d>
</d-things>
</data>
Note that I can't output <c-things>
if there aren't any <c>
elements, but I do know ahead of time what the complete list of types is, and it's fairly short so handcoding templates for each type is definitely possible. It feels like I could probably hack something together using <xsl:if>
and <xsl:for-each>
but it also feels like there must be a more ... 'templatey' way to do it. Can anyone help?
Cheers.
© Stack Overflow or respective owner