Example xml is:
<a amp="a"><b><c>this is the text</c></b></a>
Needs to be transformed to:
<a amp="a"><c>this is the text</c></a>
Example xml is:
<a amp="a"><b><c>this is the text</c></b></a>
Needs to be transformed to:
<a amp="a"><c>this is the text</c></a>
Solution #1: A slight improvement to smaccoun's solution that would preserve any attributes on the c
element (not necessary for example XML):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="c">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
Solution #2 Another alternative that leverages the built-in template rules, which apply-templates for all elements and copy all text()
:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--identity template for the c element, it's decendant nodes,
and attributes (which will only get applied from c or
descendant elements)-->
<xsl:template match="@*|c//node()|c">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Solution #3: A modified identity transform:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--identity template, copies all content by default-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--don't generate content for these matched elements,
just apply-templates to it's children-->
<xsl:template match="a|b">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
Solution #4 If you know what you want, just copy it from a match on the root node
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:copy-of select="a/b/c" />
</xsl:template>
</xsl:stylesheet>
If you want to simply remove the <b>
element from your input, then a modified identity transform should be used with a template matching the <b>
element that simply applies templates to it's children.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--identity template, copies all content by default-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--don't generate content for the <b>, just apply-templates to it's children-->
<xsl:template match="b">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
Apply the template on <c>
and then just use a copy design pattern.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match='c'>
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>