Reputation: 2813
I am using xslt 2.0 to test the name of a namespace-qualitfied attribute inside a xsl:if
. I have a solution working but I believe it is a weak one, being sensitive to the particular namespace prefix. See http://xsltransform.net/jyH9rMs/1 for a working example problem.
Example Input:
<?xml version="1.0" encoding="utf-8"?>
<content xmlns:ex="http://example.com">
<ex:t1>some content</ex:t1>
<ex:t2>some content</ex:t2>
<t3 ex:attr1="attr-val-1" ex:attr2="attr-val-2">more content</t3>
</content>
Desired Output:
<?xml version="1.0" encoding="UTF-8"?>
<content xmlns:ex="http://example.com">
<ex:t1>some content</ex:t1>
<t3 ex:attr1="attr-val-1">more content</t3>
</content>
In my stylesheet I am using similar logic to iterate over elements and attributes. In the case of elements, I can test using self::ex:t1
, but I have to resort to name()='ex:attr1'
for attributes. If the namespace prefix in the stylesheet is changed from ex
to ex1
, this attribute logic fails. self::
appears not to work for attribute nodes, or at least I haven't been able to get it to work.
In a
xsl:if
what is the correct way to test for a namespace-qualified attribute name?
My stylesheet:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ex="http://example.com" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ex:*">
<xsl:for-each select=".">
<xsl:if test="self::ex:t1">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match="*[@ex:*]">
<xsl:copy>
<xsl:for-each select="@*">
<xsl:if test="name()='ex:attr1'">
<!--<xsl:if test="self::ex:attr1">-->
<xsl:attribute name="{name()}" select="."/>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:transform>
Edit:
In my production system, from which the above problem was abstracted, I ended up using one of the methods suggested by Michael Kay/Martin Honnen: <xsl:if test=". instance of attribute(ex:attr1)">
. It is clean and directly expresses the intention.
The other solutions function correctly but I disliked the weakly-implied dependency on ..
in <xsl:if test=". is ../@ex:attr1">
and <xsl:if test="node-name(.)= resolve-QName('ex:attr1', ..)">
. <xsl:if test="node-name(.) = QName('http://example.com', 'attr1')">
suffers from having to enter the namespace URL.
Upvotes: 1
Views: 1220
Reputation: 163635
Options include
test=". instance of attribute(ex:attr1)"
or
test="node-name(.) = QName('namespace', 'attr1')"
or
test=". is ../@ex:attr1"
Upvotes: 3
Reputation: 117165
An example is not a substitute for an explanation. The following stylesheet produces exactly the result you asked for - either by design, or by coincidence:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ex="http://example.com">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- remove ex:t2 -->
<xsl:template match="ex:t2"/>
<!-- remove ex:attr2 -->
<xsl:template match="@ex:attr2"/>
</xsl:stylesheet>
Note that there is nothing to test for here.
And yes, you are correct about the self::
axis: it will never lead to an attribute (or to a namespace). And no, this has nothing to do with the attribute being in a namespace.
Upvotes: 0