I have a pile of XML like the below. What I want to do is group all the <li> elements together under a new parent, call it <ul>. I am trying to find a way to do this with XSLT, but am having problems. It seems like XSLT would be a perfect candidate for such a re-grouping of elements, but I can't figure it out. Thanks for any help!
<root>
<first>
<description>A list of trees follows</description>
<li>Fir</li>
<li>Maple</li>
<li>Pine</li>
</first>
<second>
<description>A list of cars follows</description>
<li>Mazda</li>
<li>Honda</li>
<li>Toyota</li>
</second>
</root>
I have tried using the XSLT Axis notation "following-sibling", but this doesn't produce the results I'm hoping for. I have tried this:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl=" <xslutput method="xml" indent="yes" />
<xsl:template match="/ | * | @*">
<xsl:copy>
<xsl:apply-templates select="* | @* | text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="li">
<ul>
<xsl:for-each match="./following-sibling::li">
<li><xsl:value-of select="."/>
</xsl:for-each>
</ul>
</xsl:template>
The above XSLT seems to group all the <li> elements throughout the entire XML document indiscriminantly. I'm really struggling with this. Any ideas? Thanks!
<root>
<first>
<description>A list of trees follows</description>
<li>Fir</li>
<li>Maple</li>
<li>Pine</li>
</first>
<second>
<description>A list of cars follows</description>
<li>Mazda</li>
<li>Honda</li>
<li>Toyota</li>
</second>
</root>
I have tried using the XSLT Axis notation "following-sibling", but this doesn't produce the results I'm hoping for. I have tried this:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl=" <xslutput method="xml" indent="yes" />
<xsl:template match="/ | * | @*">
<xsl:copy>
<xsl:apply-templates select="* | @* | text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="li">
<ul>
<xsl:for-each match="./following-sibling::li">
<li><xsl:value-of select="."/>
</xsl:for-each>
</ul>
</xsl:template>
The above XSLT seems to group all the <li> elements throughout the entire XML document indiscriminantly. I'm really struggling with this. Any ideas? Thanks!