473,396 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Jul 20 '05 #1
3 3644
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
6
by: Ramon M. Felciano | last post by:
Helo all -- I'm trying to gain a deeper understand for what type of semi-declarative programming can be done through XML and XPath/XSLT. I'm looking at graph processing problems as a testbed for...
4
by: gfrommer | last post by:
Hello everyone, I've been reading through a bunch of XPath tutorials and am confused by a couple items. First, is it possible to have multiple predicates in my XPath statement. For example, the...
7
by: steve bull | last post by:
I have the following code snippet to read the colorRange attributes for the colorRangeSwatch in the xml file listed below. string expr = "/swatches/colorRangeSwatch/colorRange";...
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
8
by: Jean-François Michaud | last post by:
Who in the name of #%@! thought this one out?? I noticed this behavior when trying to debug a problem I was having. I used this logical expression and some XPATH in a specific sequence of...
3
by: jmagaram | last post by:
I have a DataSet I want to work with as Xml using XmlDataDocument. I can't figure out how to query the resultant Xml using XPath. From the following XML below, what XPath query will return the list...
6
by: J.Marsch | last post by:
I must be completely losing my mind. I have some code that writes to config files. It works great with app.config files, but fails miserably with web.config files. For the life of me, I cannot...
4
by: Weston | last post by:
Are there any quick, reliable shortcuts to determine if two arbitrary XPath expressions yield result sets that intersect? The obviously way would be to iterate over the set of nodes from one...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.