473,757 Members | 2,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

xpath: predicate to choose only elements that match a one of a list of values

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('<fru it><apple>7</apple><orange>1 1</orange><banana
/></fruit>'))
BUT, how do I match on not just //apple but also //orange ?? (I have to
accomplish this with an XPATH, not a complex XQUERY 'when' clause
unfortunately.)
I even had it convoluted to this working version:

select xpath_eval('//*[local-name()=("apple" )]',
xtree_doc('<fru it><apple>7</apple><orange>7 </orange><banana
/></fruit>'))

.... hoping I could then try to add orange with a pipe (|) operator:

local-name()=("apple" |"orange") ... no dice.
Both the source and the match value are coming in at run time, so a
series of OR clauses in the predicate won't work:

noGoExample: [local-name()="apple" or [local-name()="orange"]

I KNOW there's a simple representation for this, but just try looking
for it online when you don't know what you're looking for. like looking
for a word in the dictionary you don't know how to spell.
I thought of using an XML set, something like this:
[local-name() in {"apple|orange" }]

.... but none of the dozen combinations of syntax and quoted variations worked.

I AM LOSING MY MIND!

any help folks? save my mind, thanks!
p.

Jul 3 '08 #1
6 7652
* Phantom wrote in comp.text.xml:
>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',
So use `//apple | //orange`.
>Both the source and the match value are coming in at run time, so a
series of OR clauses in the predicate won't work:

noGoExample: [local-name()="apple" or [local-name()="orange"]
Well here you have more [ than ] which usually is a syntax error. Use

//*[local-name() = 'apple' or local-name() = 'orange']
>I KNOW there's a simple representation for this, but just try looking
for it online when you don't know what you're looking for. like looking
for a word in the dictionary you don't know how to spell.
http://www.w3.org/TR/xpath has many examples, and contains everything
there is to know about XPath 1.0.
--
Björn Höhrmann · mailto:bj****@h oehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Jul 3 '08 #2
Uzytkownik "Phantom" <ph*****@exampl e.comnapisal w wiadomosci
news:2008070216 590916807-phantom@example com...
local-name()=("apple" |"orange") ... no dice.
contains(" apple, orange,",concat (" ",local-name(),","))

;>

sz.
Jul 3 '08 #3
On 2008-07-03 02:16:28 -0700, Bjoern Hoehrmann <bj****@hoehrma nn.desaid:
* Phantom wrote in comp.text.xml:
>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',

So use `//apple | //orange`.
nope, that only returns the first hit for me (apple).
>
>Both the source and the match value are coming in at run time, so a
series of OR clauses in the predicate won't work:

noGoExample: [local-name()="apple" or [local-name()="orange"]

Well here you have more [ than ] which usually is a syntax error. Use

//*[local-name() = 'apple' or local-name() = 'orange']
I mean, it would function, but I can't go there... there might be 20
fields posted, I need something more straightforward than assembling an
OR predicate each time I call the xpath.
>
>I KNOW there's a simple representation for this, but just try looking
for it online when you don't know what you're looking for. like looking
for a word in the dictionary you don't know how to spell.

http://www.w3.org/TR/xpath has many examples, and contains everything
there is to know about XPath 1.0.
thanks for the lead, but they don't cover this example, and they don't
provide much in the way of examples.

Jul 3 '08 #4
On 2008-07-03 02:53:18 -0700, "szomiz"
<sz****@kocha.d ostawac.reklamy .przez.netsaid:
Uzytkownik "Phantom" <ph*****@exampl e.comnapisal w wiadomosci
news:2008070216 590916807-phantom@example com...
>local-name()=("apple" |"orange") ... no dice.

contains(" apple, orange,",concat (" ",local-name(),","))

;>

sz.
slick 8^) however, won't this also match on crabapple and applebys?

Jul 3 '08 #5
Phantom wrote:
slick 8^) however, won't this also match on crabapple and applebys?
Nope. Note that he's concatenating a few non-name characters (space and
comma) before doing the test.

However, I *really* hate relying on testing localname and losing proper
namespace-awareness. Bad habit to get into.
If you really want to do it via a predicate, I'd suggest
"//*[self::apple or self::orange]"
which will walk the tree and return only those nodes which are apple or
orange elements.

However,
"//apple | //orange"
really should work. XPath defines the '|' character as the union
operator. This expression should return every node that is either an
apple or an orange. You say you're only getting the first hit -- but I
think that means either you're misusing your XPath API by failing to
retrieve results after the first, or it's broken and you should consider
switching implementations . (You didn't show us the surrounding code, and
I don't recognize the syntax, so I can't say much beyond that.)

Jul 4 '08 #6
On Jul 3, 4:38*pm, "Joseph J. Kesselman" <keshlam-nos...@comcast. net>
wrote:
However,
* * * * "//apple | //orange"
really should work. XPath defines the '|' character as the union
operator. This expression should return every node that is either an
apple or an orange. You say you're only getting the first hit -- but I
think that means either you're misusing your XPath API by failing to
retrieve results after the first, or it's broken and you should consider
switching implementations . (You didn't show us the surrounding code, and
I don't recognize the syntax, so I can't say much beyond that.)

The problem here is that the xpath_eval statement defaults a third
parameter which indicates that you only want the FIRST node returned.
From the Virtuoso Documentation.. ..
xpath_eval (in xpath_expressio n varchar, in xml_tree XML Entity, [in
index integer], [in named_params vector]);

Description
This function returns the result of applying the XPath expression to
the context node. By default only the first result is returned, but
supplying a third argument allows you to specify an index for the
value; the default assumes a value of 1 here. A value of 0 returns an
array of 0 or more elements, one for each value selected by the XPath
expression.
Always pays to check the documentation!!
Nick
Jul 7 '08 #7

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

Similar topics

1
6825
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
2930
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...
5
2535
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element of the currently matched element in the template, so that XPath expressions inside would be relative to that node? Specifically, I want to do this, in order to be able to issue xsl:call-template to apply some templates to some
2
1953
by: Curantil | last post by:
When I look at all the examples every selection like select="child::item" has a constant value to test against ("3" in this case). Can't this be done with a variable? eg: select="child::item" Because I'm trying to create a structure like this... usecase ->item(s) (kind of ordered list) ->alt (with @number parameter)
5
1312
by: Martin | last post by:
HI, I have some data like: <x> <y Position='Top'/> <y/> <x/> and a template like: <xsl:template match="/x/y">
14
2006
by: Mat| | last post by:
Hello :-) I am learning XPath, and I am trying to get child nodes of a node whose names do *not* match a given string, e.g : <dummy> <example> <title>Example 1</title> <body>this is an example</body>
2
2576
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
0
10069
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...
0
9904
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9884
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
8736
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
7285
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
5168
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
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
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
2697
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.