By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,222 Members | 1,458 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,222 IT Pros & Developers. It's quick & easy.

xpath: comparing two node sets

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Andy Fish wrote:
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:output 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

Jul 20 '05 #2

P: n/a
"Andy Fish" <aj****@blueyonder.co.uk> writes:
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
Jul 20 '05 #3

P: n/a
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

"David Carlisle" <da****@nag.co.uk> wrote in message
news:yg*************@penguin.nag.co.uk...
"Andy Fish" <aj****@blueyonder.co.uk> writes:
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

Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.