I've started using XSLT (6 month newbie), and are trying to understand utilizing a whitelist of elements to a large XML file to only keep elements in the whitelist.
The irony is I have been running a previous XSLT that is simply a large list of what not to include.
Through posts here i can see if i had just been looking at what elements too keep, the list would be far easier to maintain.
keep only white-listed elements and/or attributes
XSLT - How to keep only wanted elements from XML
I am using Saxon-HE 9.9.1.1N.
Here is a slimmed down version of my XML, where I do not want to keep the type & brand elements.
<?xml version="1.0" encoding="UTF-8"?>
<FlatTable>
<Table>
<type>1</type>
<ID>0001</ID>
<Brand>0</Brand>
<Name>Test 1</Name>
<code>X7F7</code>
</Table>
<Table>
<type>1</type>
<ID>0002</ID>
<Brand>0</Brand>
<Name>Test 2</Name>
<code>X7F5</code>
</Table>
</FlatTable>
The expected output would be:
<?xml version="1.0" encoding="UTF-8"?>
<FlatTable>
<Table>
<ID>0001</ID>
<Name>Test 1</Name>
<code>X7F7</code>
</Table>
<Table>
<ID>0002</ID>
<Name>Test 2</Name>
<code>X7F5</code>
</Table>
</FlatTable>
I'm trying to utilise the solution that Dimitre Novatchev supplied here but can not for the life of me see where I'm screwing it up: XSLT - How to keep only wanted elements from XML
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="some:ns">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<ns:WhiteList>
<name>ID</name>
<name>Name</name>
<name>code</name>
</ns:WhiteList>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"*[not(descendant-or-self::*[name()=document('')/*/ns:WhiteList/*])]"/>
</xsl:stylesheet>