Supposed I have the following XML input file file:
<root>
<NodeX>
...
</NodeX>
<NodeY>
...
</NodeY>
<Description>
<section01>
<subsection key="KeyA">Some text</subsection>
<subsection key="KeyB">Some text</subsection>
</section1>
<section02>
<subsection key="KeyC">Some text</subsection>
</section2>
<section03>
<subsection key="KeyD">Some Text</subsection>
</section3>
</Description>
...
</root>
And another XML file with "rules" where the keys of the subsections are listed together with an audience attribute.
Example excerpt:
<rules>
<subsection id="01">
<key audience="internalOnly">KeyA</key>
<key audience="internalOnly">KeyB</key>
</subsection>
<subsection id="02">
<key>KeyC</key>
</subsection>
<rules>
I try to write an XSL transformation that removes subsections from the input XML file based on the value of the audience attribute in the rules XML fille. If the value is "internalOnly" the subsection has to be removed.
In the example the following output XML should result:
<root>
<NodeX>
...
</NodeX>
<NodeY>
...
</NodeY>
<Description>
<section02>
<subsection key="KeyC">Some text</subsection>
</section2>
<section03>
<subsection key="KeyD">Some Text</subsection>
</section3>
</Description>
...
</root>
(The whole section01 is removed because both subsections are "internalOnly").
The problems are 1) the "lookup" in the rules XML file and 2) the removal of section elements if all the corresponding subsections are removed:
If the audience attribute would be included in the input XML file I would create the following XSLT for the requirement:
<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="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="subsection[@audience='internalOnly']"></xsl:template>
</xsl:stylesheet>