473,729 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ 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 1727
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...@goo glemail.comwrot e:
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...@goo glemail.comwrot e:
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.mozil la.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
8304
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. I've tried Quanta. It seems to be able to run the xsltproc processor to do XSLT transformations but it doesn't seem to cope with interactively typing in XPath expressions. (As a side note, what other tools are useful with XML on Linux and...
4
4965
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 nunit test that exposes the problem. I have run the test under both the 1.0 and 1.1 framework with the same result. public void XPathBooks() { XmlDocument smallDoc = new XmlDocument();
3
3091
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> <OPTION>3<OPTION> </FIELD>
0
921
by: Sabba | last post by:
TESTRESULTS PRE STUDENT @sID=1 @Score=5 STUDENT @sID=2 @Score=2 STUDENT @sID=3
1
2265
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 service such as <soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body>
1
1403
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 element(Employee Details) in an xml file and it's child by giving either the Id or Name, through a text field . this is the code: if (document.Search.Type.checked="Id" )
2
2575
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 it to apply to many places without having to update all those different locations if the predicate needs to change. This is not a full stylesheet, just snippets of the idea
6
7651
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 returns one or more specific elements on the fly, in this example, apple and/or orange. *this* works for JUST apple: select xpath_eval('//apple', xtree_doc('<fruit><apple>7</apple><orange>11</orange><banana
3
5095
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 queries are causing errors in my program. As I am new in xml and its standards I am not sure what is causing them. Answers to the following questions might help me in understanding these problems better. Can you please help me? Thanks. 1. Which...
0
8913
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
8761
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
9426
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
9200
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
8144
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
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
3
2162
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.