Page 1 of 1

Q about node-set comparisons, or casting, or maybe variables

Posted: Sun Apr 29, 2012 10:22 pm
by alyxandr
It would help if I even knew what sort of question it was... could someone please gently explain to me what's happening here:

Code: Select all


<types good="A">
<type>A</type>
<type>B</type>
<type>A</type>
<type>C</type>
</types>

<xsl:template match="types">
<xsl:message select="concat('1: Good=', count(type[. = @good]))"/>
<xsl:variable name="g" select="@good"/>
<xsl:message select="concat('2: Good=', count(type[. = $g]))"/>
</xsl:template>


Result:

Code: Select all

[Saxon-EE]1: Good=0
[Saxon-EE]2: Good=2
Is there something special about comparing element and attribute nodes? What sort of magical thing is the variable declaration doing here? (Saxon-EE 9.3.0.5 in Oxygen 13.2)

Thanks, --alex.

Re: Q about node-set comparisons, or casting, or maybe variables

Posted: Mon Apr 30, 2012 12:05 pm
by adrian
Hi,

There's no magic there, it's just XSLT+XPath. :)

The variable evaluates the XPath(@good) in the context of the types element (from the XSLT) and obtains the actual value of the attribute, which is 'A'.

For the other expression

Code: Select all

count(type[. = @good]))
what happens is that the "good" attribute is evaluated in the context of the type element. It's not there, so it fails.

However it works as expected like this (looks starting from the root):

Code: Select all

count(type[. = /types/@good])
or like this (looks at the attribute of the parent):

Code: Select all

count(type[. = parent::node()/@good])
Regards,
Adrian

Re: Q about node-set comparisons, or casting, or maybe variables

Posted: Mon Apr 30, 2012 7:47 pm
by alyxandr
It sounds so simple when you say it... :oops:
I am no longer (as) confused about context-in-predicates, thanks! --alex.