This stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="...">
<xsl:param name="PARAM_MODE" select="1"/>
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="my:sometag">
<xsl:if test="$PARAM_MODE!=1">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>
<xsl:template match="my:sometag2">
<xsl:if test="$PARAM_MODE!=2">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
With this input:
<root xmlns="...">
<!-- remove this element when PARAM_MODE=1 -->
<sometag />
<!-- remove this element when PARAM_MODE=2 -->
<sometag2 />
<someothertag />
</root>
Output:
<root xmlns="...">
<!-- remove this element when PARAM_MODE=1 -->
<!-- remove this element when PARAM_MODE=2 -->
<sometag2></sometag2>
<someothertag></someothertag>
</root>
Do note that if you want a simplified syntax, from http://www.w3.org/TR/xslt#result-element-stylesheet :
A simplified syntax is allowed for
stylesheets that consist of only a
single template for the root node. The
stylesheet may consist of just a
literal result element (see 7.1.1
Literal Result Elements). Such a
stylesheet is equivalent to a
stylesheet with an xsl:stylesheet
element containing a template rule
containing the literal result element;
the template rule has a match pattern
of /
.
So, you can add elements, but you can't strip them.
EDIT: Reversed logic for simplified syntax.
Suppose this stylesheet with... test.xsl
URI:
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<root
xmlns="..."
xmlns:my="..."
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xsl:version="1.0">
<PARAM_MODE>1</PARAM_MODE>
<!-- remove this element when PARAM_MODE=1 -->
<xsl:if test="document('')/my:root/my:PARAM_MODE!=1">
<sometag />
</xsl:if>
<!-- remove this element when PARAM_MODE=2 -->
<xsl:if test="document('')/my:root/my:PARAM_MODE!=2">
<sometag2 />
</xsl:if>
<someothertag />
</root>
Runnig with itself as input (I'm emphasizing this with the PI. Also, that makes fn:document()
superfluous...), it outputs:
<root xmlns="..." xmlns:my="...">
<PARAM_MODE>1</PARAM_MODE>
<sometag2 />
<someothertag />
</root>
At last, an comments driven stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="PARAM_MODE" select="1"/>
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[preceding-sibling::node()[1]
/self::comment()[starts-with(.,' remove ')]]">
<xsl:if test="$PARAM_MODE != substring-after(
preceding-sibling::comment()[1],
'PARAM_MODE=')">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output:
<root xmlns="...">
<!-- remove this element when PARAM_MODE=1 -->
<!-- remove this element when PARAM_MODE=2 -->
<sometag2></sometag2>
<someothertag></someothertag>
</root>