Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   xpath: comparing two node sets (http://www.velocityreviews.com/forums/t168943-xpath-comparing-two-node-sets.html)

Andy Fish 03-09-2005 06:37 PM

xpath: comparing two node sets
 
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



Soren Kuula 03-09-2005 10:47 PM

Re: xpath: comparing two node sets
 
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


David Carlisle 03-09-2005 10:51 PM

Re: xpath: comparing two node sets
 
"Andy Fish" <ajfish@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

Andy Fish 03-10-2005 09:16 AM

Re: xpath: comparing two node sets
 
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" <davidc@nag.co.uk> wrote in message
news:yg4d5u8toun.fsf@penguin.nag.co.uk...
> "Andy Fish" <ajfish@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





All times are GMT. The time now is 06:35 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.