Reputation: 63
I have xml
:
<Root>
<A>
<B>X</B>
<C>Y</C>
<D>Z</D>
</A>
</Root>
With use of xslt
transform, I need to get this:
<Root>
<Wrap type="B">
<A>
<B>X</B>
</A>
</Wrap>
<Wrap type="C">
<A>
<B>Y</B>
</A>
</Wrap>
<Wrap type="D">
<A>
<B>Z</B>
</A>
</Wrap>
</Root>
What kind of xsl::select
should I use? And how to modify just created elements in xsl
.
EDIT: correct typo, question updated. I add another one element to a list, now it is the list of A, that should be classified.
<Root>
<A>
<B>X</B>
<C>Y</C>
<D>Z</D>
</A>
<A>
<B>X1</B>
<C>Y1</C>
<D>Z1</D>
</A>
</Root>
And now I want to get
<Root>
<Wrap type="B">
<A>
<B>X</B>
<B>X1</B>
</A>
</Wrap>
<Wrap type="C">
<A>
<B>Y</B>
<B>Y1</B>
</A>
</Wrap>
<Wrap type="D">
<A>
<B>Z</B>
<B>Z1</B>
</A>
</Wrap>
</Root>
Upvotes: 1
Views: 247
Reputation: 26084
Updated with grouping:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Root">
<xsl:copy>
<xsl:for-each-group select="A/*" group-by="name()">
<xsl:element name="Wrap">
<xsl:attribute name="name"><xsl:value-of select="current-grouping-key()" /></xsl:attribute>
<A>
<xsl:for-each select="current-group()">
<B>
<xsl:value-of select="." />
</B>
</xsl:for-each>
</A>
</xsl:element>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Reputation: 3445
Use This
<xsl:template match="Root">
<xsl:copy>
<xsl:for-each select="A[1]/*">
<xsl:variable name="en" select="local-name(current())"/>
<Wrap type="{local-name(current())}">
<A>
<xsl:for-each select="//A/*[name() eq $en]">
<B>
<xsl:value-of select="."/>
</B>
</xsl:for-each>
</A>
</Wrap>
</xsl:for-each>
</xsl:copy>
</xsl:template>
Upvotes: 0