473,670 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

xpath: comparing two node sets

Hi,

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

<xsl:template match="WX:sect" >
<xsl:variable name="thisSect" select="."/>
<div>
<xsl:for-each select="//WX:sect">
<xsl:if test=".=$thisSe ct">
<xsl:attribut e name="class">Se ction<xsl:value-of
select="positio n()"/></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 3664
Andy Fish wrote:
Hi,

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

<xsl:templat e match="WX:sect" >
<xsl:variable name="thisSect" select="."/>
<div>
<xsl:for-each select="//WX:sect">
<xsl:if test=".=$thisSe ct">
<xsl:attribut e name="class">Se ction<xsl:value-of
select="positi on()"/></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:styleshe et 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=".=$thisSe ct">
<xsl:attribut e name="class">Se ction<xsl:value-of
select="positio n()"/></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>Correctio n: 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****@blueyon der.co.uk> writes:
Hi,

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

<xsl:template match="WX:sect" >
<xsl:variable name="thisSect" select="."/>
<div>
<xsl:for-each select="//WX:sect">
<xsl:if test=".=$thisSe ct">
<xsl:attribut e name="class">Se ction<xsl:value-of
select="positio n()"/></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:attribut e name="class">Se ction<xsl:numbe r 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(preceedin g...). hopefully that might speed it up
a bit too...

Andy

"David Carlisle" <da****@nag.co. uk> wrote in message
news:yg******** *****@penguin.n ag.co.uk...
"Andy Fish" <aj****@blueyon der.co.uk> writes:
Hi,

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

<xsl:template match="WX:sect" >
<xsl:variable name="thisSect" select="."/>
<div>
<xsl:for-each select="//WX:sect">
<xsl:if test=".=$thisSe ct">
<xsl:attribut e name="class">Se ction<xsl:value-of
select="positio n()"/></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:attribut e name="class">Se ction<xsl:numbe r
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
6817
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 elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
6
2910
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 this, and came across a problem that I haven't been able to solve elegantly. The problem is to find "linker" vertexes that a pair of verteces from a pre-defined set. For example, if the graph verteces represent cities and edges represent flights...
4
2158
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 following xpath statement: "//AAA/BBB/c" should only return the last <c> node containing the 6, correct? Also consider the statement "//AAA/BBB" returns the first two <BBB> items, correct? the and's and or's in the predicates are all right yes?
7
1805
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"; XmlElement crsElement = (XmlElement)m_colorRangeSwatchDoc.SelectSingleNode(expr); bool fr = bool.Parse(crsElement.GetAttribute("fixed").ToString()); The element returned is always the 1st, All Blue Colors, why doesn't the expression...
9
2148
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 should work. Also the second nav.OuterXml appears to also be wrong to me. Can someone explain to me why this does not work? (This is an example from a program we have where xpath can be entered in two parts so we have to be able
8
2192
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 instructions that allow me to transform a CALS table model into our own specific table model and I used this expression: <xsl:if test="self::node()=../CELL and self::node()">...
3
2854
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 of orders for each Male customer? Because some tables in my DataSet have >1 foreign key columns, it is not possible to set up a nested DataRelation for all relationships - a DataTable can only be the child of at most one nested DataRelation. As...
6
7293
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 figure out what is going on here. I have taken it all the way back to just selecting the configuration node (top level node), and it fails! How can this line fail (returns null)??
4
2798
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 expression, checking each one to see if it's also contained in the other set, but I'm wondering if there might be ways of doing this by looking at or rewriting the actual XPath expressions themselves, without evaluating them.
0
8466
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8384
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8901
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8591
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4388
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2799
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1791
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.