Stephen
Stephen

Reputation: 19944

How do I test for a namespaced element name in xsl

I'm doing some xforms development and have a template that matches select items. If it's a single select instead of multiple, I want to add an empty value to the top of the given list:

<xsl:template match="xforms:select1|xforms:select">
    <xsl:apply-templates select="node()" />
    <xsl:if test=".[name()='xf:select1'] and not (@appearance eq 'full')">
        <xforms:item>
            <xforms:label />
            <xforms:value />
        </xforms:item>
    </xsl:if>
    ...

The issue is that this worked for one of my forms that had an xf:select1 (since the match is namespace aware), but the xforms:select1 controls in another form were broken since the name() test is only for strings.

Is there a way I can make this if statement work, regardless of what prefix I make for the http://www.w3.org/2002/xforms namespace?

Upvotes: 1

Views: 79

Answers (2)

Adrian
Adrian

Reputation: 46

You definitely want to avoid any code which relies on namespace prefixes. I think you want to use the self axis:

<xsl:if test="self::xforms:select1 and not (@appearance eq 'full')">

Upvotes: 1

Stephen
Stephen

Reputation: 19944

What I have done is split out the if into different templates... I like it because it's what xsl is geared for; I hate it because it's yet another copy of some match logic that would have to be changed in multiple places if it had a bug or an improvement...

<xsl:template match="xforms:select1[not(xforms:itemset) and not(@appearance eq 'full')]" priority="10">
    <xsl:apply-templates select="node()" />
    <xforms:item>
        <xforms:label />
        <xforms:value />
    </xforms:item>
    <xsl:call-template name="select-itemset"/>
</xsl:template> 

<xsl:template match="xforms:select1[not(xforms:itemset)]|xforms:select[not(xforms:itemset)]">
    <xsl:apply-templates select="node()" />
    <xsl:call-template name="select-itemset"/>
</xsl:template>

<xsl:template name="select-itemset">
    ...

Upvotes: 0

Related Questions