Paulb
Paulb

Reputation: 1531

XSLT Multiple Conditions

I have this XSLT 2.0 template:

<xsl:template match="para[
    not(ancestor::p) 
    and not(ancestor::div) 
    and not(ancestor::paragraph)
    ]">
    <p class="para">
        <xsl:apply-templates/>
    </p>
</xsl:template>

It does what I need it to do: it prevents an HTML <p> in a <p>. But it is verbose and I suppose there is a more concise way to write it.

Is there a better way to write the multiple ancestor tests? I tried a union..that didn't work of course.

Upvotes: 3

Views: 1267

Answers (1)

Tomalak
Tomalak

Reputation: 338336

<xsl:template match="para[not(ancestor::p|ancestor::div|ancestor::paragraph)]">
    <p class="para">
        <xsl:apply-templates/>
    </p>
</xsl:template>

Alternatively

<xsl:template match="para">
    <p class="para">
        <xsl:apply-templates/>
    </p>
</xsl:template>

<xsl:template match="para[ancestor::p|ancestor::div|ancestor::paragraph]" />
<!-- or whatever you want to do in that case, <xsl:apply-templates/> maybe -->

Upvotes: 2

Related Questions