How do I copy only specific elements with their children? So, not the parent elements.
Example XLST file:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<header>Some inserted text</header>
<xsl:template match="staff">
<xsl:apply-templates select="subelement"/>
</xsl:template>
<xsl:template match="subelement">
<xsl:copy-of select="*"/>
</xsl:template>
</parent>
Example XML file:
<?xml version="1.0" encoding="utf-8"?>
<company>
<staff attrib="select" id="1001">
<name>should-1</name>
<role>copy-1</role>
</staff>
<staff id="1002">
<name>should-3</name>
<role>not-copy-3</role>
</staff>
<staff attrib="select" id="1002">
<name>should-2</name>
<role>copy-2</role>
</staff>
</company>
Wanted result: ( so, no 'company' element )
<?xml version="1.0" encoding="UTF-8"?>
<parent><header>Some inserted text</header>
<staff attrib="select" id="1001">
<name>should-1</name>
<role>copy-1</role>
</staff>
<staff id="1002">
<name>should-3</name>
<role>not-copy-3</role>
</staff>
<staff attrib="select" id="1002">
<name>should-2</name>
<role>copy-2</role>
</staff>
</parent>
Result:
<?xml version="1.0" encoding="UTF-8"?>
<parent><p>Some inserted text</p>
<company>
<staff attrib="select" id="1001">
<name>should-1</name>
<role>copy-1</role>
</staff>
<staff id="1002">
<name>should-3</name>
<role>not-copy-3</role>
</staff>
<staff attrib="select" id="1002">
<name>should-2</name>
<role>copy-2</role>
</staff>
</company>
</parent>
CodePudding user response:
Try:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/company">
<parent>
<header>Some inserted text</header>
<xsl:copy-of select="staff"/>
</parent>
</xsl:template>
</xsl:stylesheet>