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

Here should go questions about transforming XML with XSLT and FOP.
alyxandr
Posts: 18
Joined: Sat Aug 01, 2009 7:52 pm

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

Post 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.
adrian
Posts: 2879
Joined: Tue May 17, 2005 4:01 pm

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

Post 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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
alyxandr
Posts: 18
Joined: Sat Aug 01, 2009 7:52 pm

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

Post by alyxandr »

It sounds so simple when you say it... :oops:
I am no longer (as) confused about context-in-predicates, thanks! --alex.
Post Reply