Reputation: 3021
I need to output copy of element with all its attributes and apply-templates on children inside. main problem is that attributes are unknown.
XML:
<elem attrA="a" attrB="b" ... attrN="n">
<child><child>
<child><child>
</elem>
I have attempted to loop over all attributes but, can't get it working.
<xsl:template match="elem">
<xsl:element name="name(.)">
<xsl:for-each select="@*">
<xsl:attribute name="name()">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
Required output:
<elem attrA="a" attrB="b" ...="" attrN="n">
<processed-child></processed-child>
<processed-child></processed-child>
</elem>
Given child template:
<xsl:template match="child">
<processed-child><xsl:value-of select="."/></processed-child>
</xsl:template>
Edit:
XSLT 1.0
Upvotes: 3
Views: 7645
Reputation: 3021
Just to add to Tomalak's answer, the final solution was enhanced a bit to enable text rendering around tags. (It was not described in original post, but was a requirement)
Complete solution:
<xsl:template match="elem">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates select="*|text()" />
</xsl:copy>
</xsl:template>
<xsl:template match="text()"><xsl:value-of select="."/></xsl:template>
Upvotes: 2
Reputation: 338118
Does
<xsl:template match="elem">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates select="*" />
</xsl:copy>
</xsl:template>
not work?
Upvotes: 4