473,396 Members | 2,002 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.

XSLT XPATH uses node content in logical conditions???!?!

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[1] and self::node()[@colname >
1]">...

to artificially create cells at the beginning of the row if their
colname is greater than 1. The algorithm works fine, but the expression
DID NOT in every case.

.../CELL[1] actually refers to the content of CELL[1] instead of
refering to a unique identifier specific to the first child of the
parent. Who?!?! What??!?. Shouldn't the XPATH expression point to
unique cells within the XML tree as opposed to outputting the content?

A freak case emerged when of course, 2 different cells within the same
row had exactly the same content. I had to patch up the logical
condition so that it would uniquely identify the cells.

Shouldn't the distinction be made between the node itself and its
content? Or am I missing something?

Regards
Jean-Francois Michaud

Sep 19 '06 #1
8 2179
In article <11**********************@k70g2000cwa.googlegroups .com>,
Jean-François Michaud <co*****@comcast.netwrote:
>../CELL[1] actually refers to the content of CELL[1] instead of
refering to a unique identifier specific to the first child of the
parent. Who?!?! What??!?.
This is, of course, what you usually want. It's much more common to
compare the content of nodes than to compare them for identity.
>A freak case emerged when of course, 2 different cells within the same
row had exactly the same content.
That's not a freak case, it's perfectly normal.

Unfortunately there's no "natural" way to determine node identity in
XPath (at least, in XPath 1). Two unnatural ways are:

generate-id(node1) = generate-id(node2)

and (probably more efficient):

count(node1 | node2) = 1

-- Richard
Sep 19 '06 #2
On 2006-09-19, Jean-François Michaud <co*****@comcast.netwrote:
[snip]
Shouldn't the distinction be made between the node itself and its
content? Or am I missing something?
You must have missed the first sentence of the fifth paragraph of the
"Booleans" section of the XPATH specification (see
://www.w3.org/TR/xpath#booleans): "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."
Sep 19 '06 #3
The problem is the definition of comparison of nodesets as comparison of
their values to see if any node in the set compares true. That's
actually a very useful behavior, but as you've just demonstrated it
isn't always the one you want.

To obtain a unique identifier for that node for identity-comparison
purposes, use the generate-id() function. In fact there's an explicit
example of this in XSLT spec, during their description of the document()
function, where they use it to illustrate that repeated retrieval of the
same URI yields the same actual nodes and not just the same data:
generate-id(document("foo.xml"))=generate-id(document("foo.xml"))
Sep 19 '06 #4
Richard Tobin wrote:
count(node1 | node2) = 1
Thanks; I'd forgotten that alternative.
--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Sep 19 '06 #5

Richard Tobin wrote:
In article <11**********************@k70g2000cwa.googlegroups .com>,
Jean-François Michaud <co*****@comcast.netwrote:
../CELL[1] actually refers to the content of CELL[1] instead of
refering to a unique identifier specific to the first child of the
parent. Who?!?! What??!?.

This is, of course, what you usually want. It's much more common to
compare the content of nodes than to compare them for identity.
Hmmm. It seems upside down to me.
A freak case emerged when of course, 2 different cells within the same
row had exactly the same content.

That's not a freak case, it's perfectly normal.
According to what I read of the spec it seems to be. It seems to me
though as if it should be the other way around. Or at least, I believe
it should be made explicit weather we are referencing the node itself
or its content, giving us the choice of dealing with structure or with
content without having to scope into esoteric programming. As it is,
the XPATH expression gives me the impression that we are talking about
structure as opposed to content; it so happens to be the other way
around we attain content by talking about structure. Do XPATH
expressions in 'select' and 'match' behave the same way? If this isn't
the case, then it seems we have inconsistent behavior. Two XPATH
expressions talking about 2 different things. One talking about
structure whereas the other talks about content.
Unfortunately there's no "natural" way to determine node identity in
XPath (at least, in XPath 1). Two unnatural ways are:

generate-id(node1) = generate-id(node2)

and (probably more efficient):

count(node1 | node2) = 1
Thanks for the tip.

Regards
Jean-Francois Michaud

Sep 19 '06 #6
In article <11**********************@m73g2000cwd.googlegroups .com>,
Jean-François Michaud <co*****@comcast.netwrote:
>This is, of course, what you usually want. It's much more common to
compare the content of nodes than to compare them for identity.

Hmmm. It seems upside down to me.
XSLT is primarily a language for processing text. It's far more
common to, for example, find the elements whose "name" attribute is
equal to some other element's "name" attribute than it is to test
whether two nodes are the same node.

I wouldn't be surprised if you've written dozens of expressions that
assumed that node equality compared text, without even realising it.
Look at your other uses of the = operator where the arguments are
node sets.
>According to what I read of the spec it seems to be. It seems to me
though as if it should be the other way around. Or at least, I believe
it should be made explicit weather we are referencing the node itself
or its content, giving us the choice of dealing with structure or with
content without having to scope into esoteric programming. As it is,
the XPATH expression gives me the impression that we are talking about
structure as opposed to content; it so happens to be the other way
around we attain content by talking about structure. Do XPATH
expressions in 'select' and 'match' behave the same way? If this isn't
the case, then it seems we have inconsistent behavior. Two XPATH
expressions talking about 2 different things. One talking about
structure whereas the other talks about content.
Read the definition of the equality operator when the arguments are
node sets. It's perfectly well-defined and consistent.

-- Richard
Sep 19 '06 #7
Richard Tobin wrote:
I wouldn't be surprised if you've written dozens of expressions that
assumed that node equality compared text
Yep. All those instances of FOO[@BAR="something"], or FOO[@bar=
, without even realising it.
Look at your other uses of the = operator where the arguments are
node sets.
>According to what I read of the spec it seems to be. It seems to me
though as if it should be the other way around. Or at least, I believe
it should be made explicit weather we are referencing the node itself
or its content, giving us the choice of dealing with structure or with
content without having to scope into esoteric programming. As it is,
the XPATH expression gives me the impression that we are talking about
structure as opposed to content; it so happens to be the other way
around we attain content by talking about structure. Do XPATH
expressions in 'select' and 'match' behave the same way? If this isn't
the case, then it seems we have inconsistent behavior. Two XPATH
expressions talking about 2 different things. One talking about
structure whereas the other talks about content.

Read the definition of the equality operator when the arguments are
node sets. It's perfectly well-defined and consistent.

-- Richard

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Sep 20 '06 #8
Joe Kesselman wrote:
Richard Tobin wrote:
I wouldn't be surprised if you've written dozens of expressions that
assumed that node equality compared text

Yep. All those instances of FOO[@BAR="something"], or FOO[@bar=
Oh I have, but this still refers to a structural construct rather than
content.

The FOO element that meets the predicate requirement of having its BAR
attribute equal "something". To me this is still clearly a structural
reference, but oh well :).

[snip]

Regards
Jeff

Sep 20 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Iain | last post by:
I want to create an XML configuration file which might look like <REGION Name="Europe" WingDing="Blue"> <COUNTRY Name="UK" WingDing="white"> <TOWN Name="London" WingDing="Orange" /> </COUNTRY>...
3
by: Justine Hlista | last post by:
I'm using xalan-j_2_6_0 and trying to get an example from Michael Kay's book to work: <xsl:template match="/"> <xsl:variable name="rainbow"> <color>red</color> <color>blue</color>...
1
by: inquirydog | last post by:
Can anyone explain to me why the following XQuery expression (a simple xpath expression) returns a different result than the same expression in xslt? document("document.xml")//a/@b For the...
3
by: Ian Roddis | last post by:
Hello, I want to embed SQL type queries within an XML data record. The XML looks something like this: <DISPLAYPAGE> <FIELD NAME="SERVER" TYPE="DROPDOWN"> <OPTION>1<OPTION> <OPTION>2<OPTION>...
1
by: Sergey Dubinets | last post by:
In effort to prioritize our goals we composed the list of random features each of them may add value to set of XSLT tools offered from Microsoft. 1. XSLTc (Compiler for XSLT...
5
by: jorgedelgadolopez | last post by:
Hi all, I am using the xpathnavigator evaluate function on .net (xpath 1 right?). Now I need to expand the code to do multiple contains, compare dates (such as 'before', 'between' and 'after'),...
2
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.