flyingaxel
flyingaxel

Reputation: 351

Copy element with attributes but without child elements

With the following xml document

<?xml version="1.0" encoding="UTF-8"?>

<a name="john">
   <b/>
</a>

and the following xslt

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="a">
        <xsl:copy/>
    </xsl:template>

</xsl:stylesheet>

The output is

<?xml version="1.0" encoding="UTF-8"?><a/>

What i want is <a name="John"/>. How do i get the element a along with its attribute name and without its child b?

Upvotes: 1

Views: 639

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

Copy the attributes as well:

<xsl:template match="a">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
    </xsl:copy>
</xsl:template>

Upvotes: 3

Related Questions