473,471 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Specifying predicate queries with XPath?

Suppose I have the following XML doc:

<root>
<tag id="1">
<number value="1"/>
</tag>
<tag id="2">
<something/>
</tag>
<tag id="3">
<something>
<number value="2"/>
<something/>
</tag>
</root>

I'm interested (a) 'tags that have a number element whose value
attribute 0' and also (b) 'tags that have a something element that
have a number element whose value 0'

I could run an x-path query for either (a) or (b), i.e. for (a):

'/root/tag[number/@value0]'

but what I really want is to somehow OR the two together so I end up
with:

<root>
<tag id="1">
<number value="1"/>
</tag>
<tag id="3">
<something>
<number value="2"/>
<something/>
</tag>
</root>

Can't seem to find the right syntax, any pointers?

Many thanks,

Duncan.

Mar 21 '07 #1
7 1715
Duncan Smith wrote:
Suppose I have the following XML doc:

<root>
<tag id="1">
<number value="1"/>
</tag>
<tag id="2">
<something/>
</tag>
<tag id="3">
<something>
<number value="2"/>
<something/>
</tag>
</root>

I'm interested (a) 'tags that have a number element whose value
attribute 0' and also (b) 'tags that have a something element that
have a number element whose value 0'
/root/tag[number/@value 0 or something/number/@value 0]

--

Martin Honnen
http://JavaScript.FAQTs.com/
Mar 21 '07 #2
On Mar 21, 7:45 pm, "Duncan Smith"
<DSmith1...@googlemail.comwrote:
Suppose I have the following XML doc:

<root>
<tag id="1">
<number value="1"/>
</tag>
<tag id="2">
<something/>
</tag>
<tag id="3">
<something>
<number value="2"/>
<something/>
That's not well-formed and therefore is not XML. Are you
sure you want help?
</tag>
</root>

I'm interested (a) 'tags that have a number element whose
value attribute 0' and also (b) 'tags that have a
something element that have a number element whose
value 0'

I could run an x-path query for either (a) or (b), i.e.
for (a):

'/root/tag[number/@value0]'

but what I really want is to somehow OR the two together
Naturally, you should use 'or'.

/root/tag
[number/@value 0 or something/number/@value 0]
Can't seem to find the right syntax, any pointers?
Have you tried reading the docs?

--
roy axenov

Mar 21 '07 #3
>
/root/tag[number/@value 0 or something/number/@value 0]
That's great, thanks.

Mar 22 '07 #4
<something>
<number value="2"/>
<something/>

That's not well-formed and therefore is not XML. Are you
sure you want help?
Sorry about the typo, I will validate before sending in the future.
Naturally, you should use 'or'.
Have you tried reading the docs?
I was trying 'or', but my mistake was in starting the 2nd predicate
from root again rather than being relative to the opening bracket.

Yes, I'd tried reading the docs before posting - the XPath
specification from w3c.org, but to be honest, it's a little heavy
going in places without any examples... Maybe you know of a more
digestible document?

Many thanks,

Duncan.

Mar 22 '07 #5
On Mar 22, 11:18 am, "Duncan Smith"
<DSmith1...@googlemail.comwrote:
Yes, I'd tried reading the docs before posting - the
XPath specification from w3c.org, but to be honest, it's
a little heavy going in places without any examples...
It certainly is that, and the Specs are quite unusable as
introductory reading for us mere mortals, but in all
fairness to W3C, it is normally possible to fish out the
information you need on a specific question out of their
specs (and being able to do that is a very valuable skill
to acquire).
Maybe you know of a more digestible document?
For my needs I normally use either the W3C specs or
reference materials on developer.mozilla.org (note that
those wouldn't've helped you solve your original problem,
for example, since there's no info on XPath operators there
that I'm aware of). Basically, where operators are
concerned, use your common sense with a few caveats (div
instead of /, because / is used differently in XPath, '|'
and 'or' being two vastly different things etc.)

Since this is the Net, there's a lot of reference and
tutorial materials freely available. GIYF. Maybe that's
just me, but stay away from w3schools.

--
Pavel Lepin

Mar 22 '07 #6
>Maybe you know of a more digestible document?

For XSLT, the best hardcopy book I've seen is still Mike Kay's. In
softcopy... standard pointer to material at http://www.ibm.com/xml,
among many others.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Mar 22 '07 #7
Duncan Smith wrote:
Yes, I'd tried reading the docs before posting - the XPath
specification from w3c.org, but to be honest, it's a little heavy
going in places
The W3C specs are written by experts for experts, and are not generally
recommended as a starting place for anyone who doesn't have the
background and patience needed to chase implications back and forth
through the document. Which is why tutorials get written...
--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Mar 22 '07 #8

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

Similar topics

6
by: Scott Simpson | last post by:
What is a good tool for running XPath queries on Linux? I have the O'Reilly XPath book and I'm using XPath Visualizer on Windows and that seems to work fine, but I'm looking for something on Linux....
4
by: Rune | last post by:
I have two queries that appear to be exactly the same, but one of them returns null while the other one returns a valid result! Can anyone provide an explanation to why this is so? Below is an...
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>...
0
by: Sabba | last post by:
TESTRESULTS PRE STUDENT @sID=1 @Score=5 STUDENT @sID=2 @Score=2 STUDENT @sID=3
1
by: Adam Eccleshall | last post by:
Hi, I'm trying to write a routine to extract results from a web service written in VS 2005 using XPath, however VS doesn't put a prefix on result namespaces by default, so in the output from a...
1
by: raymon2683 | last post by:
Hi i 'm new to xpath , i used it to naviguate through an Xml document, since it's much easier. But a got a problem with the predicate: i have a web page that is supposed to enable user to find an...
2
by: =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?= | last post by:
Hello guys, I was wondering if it was possible to reference a boolean predicate in a variable. Basically I want to do with the boolean predicate what you would do with any other variable; I want...
6
by: Phantom | last post by:
I totally need the help of the XML experts up here, I can't figure this one out, I've been working on it for hours today, googling my brains out. I need to write an xpath query string that...
3
by: musicgold | last post by:
Hi, I am new in XML. But I have done coding in VBA, C, and HTML. I am using VBA to extract data from an xml file. I use Xpathbuilder to generate Xpath queries for my work. However, some Xpath...
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
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
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...
1
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
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,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.