Input:
<a q='r'>
<b x='1' y='2' z='3'/>
<!-- other a content -->
</a>
Desired output:
<A q='r' x='1' y='2' z='3'>
<!-- things derived from other a content, no b -->
</A>
Could someone kindly give me a recipe?
Easy.
<xsl:template match="a">
<A>
<xsl:copy-of select="@*|b/@*" />
<xsl:apply-templates /><!-- optional -->
</A>
</xsl:template>
The <xsl:apply-templates />
is not necessary if you have no further children of <a>
you want to process.
Note
<xsl:copy-of>
to insert source nodes into the output unchanged|
to select several unrelated nodes at onceEDIT: If you need to narrow down which attributes you copy, and which you leave alone, use this (or a variation of it):
<xsl:copy-of select="(@*|b/@*)[
name() = 'q' or name() = 'x' or name() = 'y' or name() = 'z'
]" />
or even
<xsl:copy-of select="(@*|b/@*)[
contains('|q|x|y|z|', concat('|', name(), '|'))
]" />
Note how the parentheses make the predicate apply to all matched nodes.
XSL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="a">
<A>
<xsl:apply-templates select="@*|b/@*|node()"/>
</A>
</xsl:template>
<xsl:template match="b"/>
</xsl:stylesheet>
output
<A q="r" x="1" y="2" z="3"><!-- other a content --></A>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With