xpath: comparing two node sets

A

Andy Fish

Hi,

I just found this template in someone else's xslt (it's Microsoft's
"word2html" stylesheet to convert WordProcessingML to HTML)

<xsl:template match="WX:sect">
<xsl:variable name="thisSect" select="."/>
<div>
<xsl:for-each select="//WX:sect">
<xsl:if test=".=$thisSect">
<xsl:attribute name="class">Section<xsl:value-of
select="position()"/></xsl:attribute>
.....

It seems that the author is looping through all the WX:sect nodes looking
for the context node, in order to extract the position. However, I don't
understand the <xsl:if> test. According to the xpath spec:

"If both objects to be compared are node-sets, then the comparison will be
true if and only if there is a node in the first node-set and a node in the
second node-set such that the result of performing the comparison on the
string-values of the two nodes is true".

For this code to work, the "=" operator needs to compare two node sets to
see if they are pointing at the same object (like == in Java), which is a
very different thing. But the output looks correct.

Can anyone shed some light on this?

TIA

Andy
 
S

Soren Kuula

Andy said:
Hi,

I just found this template in someone else's xslt (it's Microsoft's
"word2html" stylesheet to convert WordProcessingML to HTML)

<xsl:template match="WX:sect">
<xsl:variable name="thisSect" select="."/>
<div>
<xsl:for-each select="//WX:sect">
<xsl:if test=".=$thisSect">
<xsl:attribute name="class">Section<xsl:value-of
select="position()"/></xsl:attribute>
....

It seems that the author is looping through all the WX:sect nodes looking
for the context node, in order to extract the position. However, I don't
understand the <xsl:if> test. According to the xpath spec:

"If both objects to be compared are node-sets, then the comparison will be
true if and only if there is a node in the first node-set and a node in the
second node-set such that the result of performing the comparison on the
string-values of the two nodes is true".
Yes, it doesn't work, strictly speaking.

I simplified the stylesheet (namwspaces away, and a little guessing):

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:eek:utput method="xml" indent="yes"/>

<xsl:template match="doc">
<toc>
<xsl:apply-templates/>
</toc>
</xsl:template>

<xsl:template match="sect">
<xsl:variable name="thisSect" select="."/>
<div>
<xsl:for-each select="//sect">
<xsl:if test=".=$thisSect">
<xsl:attribute name="class">Section<xsl:value-of
select="position()"/></xsl:attribute>
</xsl:if>
</xsl:for-each>

<stupid-summary>
<xsl:value-of select="."/>
</stupid-summary>

</div>
</xsl:template>
</xsl:stylesheet>

With the input doc:
<doc>
<sect>
<title>About foo</title>
<para>foo is green</para>
</sect>
<sect>
<title>About bar</title>
</sect>
<sect>
<title>About baz</title>
</sect>
<sect>
<title>About foo</title>
<para>Correction: foo is red</para>
</sect>
<sect>
<title>About bar</title>
</sect>
</doc>


it transforms (xsltproc) to:
<?xml version="1.0"?>
<toc>
<div class="Section1"><stupid-summary>
About foo
foo is green
</stupid-summary></div>
<div class="Section5"><stupid-summary>
About bar
</stupid-summary></div>
<div class="Section3"><stupid-summary>
About baz
</stupid-summary></div>
<div class="Section4"><stupid-summary>
About foo
Correction: foo is red
</stupid-summary></div>
<div class="Section5"><stupid-summary>
About bar
</stupid-summary></div>
</toc>

The thing compared is the text value of the node sets (as you wrote).
They are the same for the 2nd and 5th sect elements -- so two attributes
with the same name were output -- the last one won; there is a section
5 after section 1.

Of course, having the same title child but some different (text values
of) other children (1st and 4th sect elements) WILL distinguish the text
values of the nodes.

Soren
 
D

David Carlisle

Andy Fish said:
Hi,

I just found this template in someone else's xslt (it's Microsoft's
"word2html" stylesheet to convert WordProcessingML to HTML)

<xsl:template match="WX:sect">
<xsl:variable name="thisSect" select="."/>
<div>
<xsl:for-each select="//WX:sect">
<xsl:if test=".=$thisSect">
<xsl:attribute name="class">Section<xsl:value-of
select="position()"/></xsl:attribute>
....

It seems that the author is looping through all the WX:sect nodes looking
for the context node, in order to extract the position. However, I don't
understand the <xsl:if> test. According to the xpath spec:

"If both objects to be compared are node-sets, then the comparison will be
true if and only if there is a node in the first node-set and a node in the
second node-set such that the result of performing the comparison on the
string-values of the two nodes is true".

For this code to work, the "=" operator needs to compare two node sets to
see if they are pointing at the same object (like == in Java), which is a
very different thing. But the output looks correct.

Can anyone shed some light on this?

TIA

Andy

the code you posted compares the string value of the node (ie the
concatenation of all the descendent text nodes), and the string value of
every other sect node in the document, it will do this for every sect in
the document even if the first one tests equal as as it's written it
needs to use the last such found.

This is likely to be slow/expensive.

If node identity is intended the test should be
<xsl:if test="count(.|$thisSect)=1">

But even then a search on // seems a very strange way to calculate this
number I think probably

<xsl:template match="WX:sect">
<div class="Section{count(preceding::WX:sect)}">
....

or

<xsl:template match="WX:sect">
<div>
<xsl:attribute name="class">Section<xsl:number level="any"/></xsl:attribute>
....

was intended, but hard to tell just from the sample you posted.
But the output looks correct.
even if you have two sect elements with the same text content?

David
 
A

Andy Fish

Thanks to both for these replies - I see what's going on now.

I'll replace it with count(preceeding...). hopefully that might speed it up
a bit too...

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top