How can I copy all children of an element EXCEPT one element? I studied a few examples but that solution does not work apparently in my case.
Example input:
<?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="1003">
<name>should-2</name>
<role>copy-2</role>
</staff>
</company>
Expected output: so, <role> excluded:
<?xml version="1.0" encoding="utf-8"?>
<staff attrib="select" id="1001">
<name>should-1</name>
</staff>
<staff id="1002">
<name>should-3</name>
</staff>
<staff attrib="select" id="1003">
<name>should-2</name>
</staff>
My XSLT script: with a number of 'tries' without any success.
<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"/>
<!-- try 1 -->
<xsl:template match="/company/staff/role" />
<!-- try 2 -->
<xsl:template match="role" />
<!-- try 3 -->
<xsl:template match="//role" />
<xsl:template match="/company">
<parent>
<xsl:copy-of select="staff"/>
</parent>
</xsl:template>
</xsl:stylesheet>
CodePudding user response:
You could do it like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<!-- Remove all role elements -->
<xsl:template match="role"/>
<!-- Identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The identity transform copies everything, and the role template catches all role elements and does nothing with them, effectively removing them.
See it working here: https://xsltfiddle.liberty-development.net/3N7DtjK