Reputation: 63
I want a simple XSLT which will only keep the elements which contain a certain regex:
<example>
<abc>text</abc>
<bc>text</bc>
<ab>text</ab>
</example>
I want the same XML output but only with the elements which contain an "a":
<example>
<abc>text</abc>
<ab>text</ab>
</example>
Upvotes: 1
Views: 1191
Reputation: 111726
Start with the identity transform and add a template that suppresses elements whose name does not match your regex:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[not(matches(name(), 'a'))]"/>
</xsl:stylesheet>
Explanation: By default the identity transformation will copy everything over to the output as-is. Override this default behavior by writing a simple template that matches elements without "a" in their name and does nothing, thereby preventing such elements from appearing in the output document.
Upvotes: 3