wolfmason
wolfmason

Reputation: 409

XSLT -- apply-templates problem when conditionally matching nodes based on a parameter value

Depending on a specific parameter value, I need to either omit matched elements from a transformed document or allow the matched elements to be transformed according to rules in other matching templates. I have a complex xml doc with dozens of element/attribute types, all handled in a huge variety of ways in dozens of templates. If any element has the attribute value omit_attr="true" and the stylesheet has the parameter omit_param="true", I need to omit them from the transformed document. If however my parameter is omit_param="false" then I need to apply whatever other rules exist for my omit_attr="true" elements. Here's my unacceptable template:

<xsl:template match="*[@omit_attr= 'true']">
    <xsl:choose>
        <xsl:when test="($omit_param= 'true')"/>
        <xsl:otherwise>
            <xsl:apply-templates/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

This omits content just fine when omit_param="true". The problem occurs when omit_param="false" and I need to process omit_attr="true" elements with other applicable templates. This code transforms the matched element's children, but not the matched element, which is unacceptable because the matched element will have its own set of transform rules elsewhere. And of course <xsl:apply-templates select="."/> attempts to process the matched element as desired, but creates an endless loop in the process.

So, how can I test for omit_attr="true" in all possible elements, and still apply the alternate templates to omit_attr="true" elements when omit_param="false"?

If I could wrap my xsl:template in if big <if test="omit_param='true'> statement, that would do the job, but of course it's against the rules...

Upvotes: 0

Views: 33

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167641

If that is an XSLT 2 or later processor you can just use

  <xsl:template match="*[@omit_attr= 'true' and $omit_param= 'true']"/>

to block the element from being processed/copied/transformed if the two conditions hold and have your other templates applied if not. For XSLT 1 processors I am not sure they allow a variable reference in a match pattern.

Upvotes: 2

Related Questions