Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > xpath query query

Reply
Thread Tools

xpath query query

 
 
David Gordon
Guest
Posts: n/a
 
      05-18-2005
Hi Folks,

I wonder if anyone can help me with the following (perhaps trivial) problem:

<xml>
<node name="a" type="a"/>
<node name="b" type=""/>
<node name="c"/>
<node name="d" type="b"/>
</xml>

we're using a command line xpath script to pluck values from
configuration files, e.g. to get the list of node names:

xpath test.xml '/xml/node/@name'

Returns the list of names in the file, easy.
a
b
c
d

Because of the way our other command line tools work, we're relying on
the result being a return separated list. We can get the list of types
like this:

xpath test.xml '/xml/node/@type'

which returns

a

b

But passes over the node with no type attribute. Is there an xpath query
which would give the result:

a


b

So where no type attribute existed, an empty string is returned?

thanks in advance,

David Gordon
 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      05-18-2005
David Gordon <(E-Mail Removed)> writes:
.....
> <xml>
> <node name="a" type="a"/>
> <node name="b" type=""/>
> <node name="c"/>
> <node name="d" type="b"/>
> </xml>
>
> xpath test.xml '/xml/node/@type'
>
> which returns
>
> a
>
> b
>
> But passes over the node with no type attribute. Is there an xpath query
> which would give the result:
>
> a
>
>
> b
>
> So where no type attribute existed, an empty string is returned?
>


Since you are using Xpath1 (rather than xpath2 or xslt for example)
You have to return nodes that exist in the source, in document order,
or return a single atomic value such as a string.
So in this case you would need to return some node to repreentthe
missing type on c and that node must occur between th etype attribute on
b and the type attribute on c. The only possiblities really then are the
node <node name="c"/> or the white space text nodes either side of that.
Given that your XPath tool outputs attribute nodes @type as just (for
example) "a" I assume it prints the string value of a node, which is
empty for <node name="c"/>, which is what you want, so..

/xml/node/@type|/xml/node[not(@type)]

returns a node set of all type attribute nodes, and all node element
nodes that don't have a type attribute.
With a bit of luck your xpath tool will output that node set by printing
the string value of each node, in document order, but i can't test that.

David
 
Reply With Quote
 
 
 
 
David Gordon
Guest
Posts: n/a
 
      05-18-2005
> ...
> With a bit of luck your xpath tool will output that node set by printing
> the string value of each node, in document order, but i can't test that.


I can test it, and I did, and... it worked!

Genius - thanks very much

David
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Memory leak" in javax.xml.xpath.XPath Marvin_123456 Java 4 07-29-2005 03:49 PM
XPath: efficiency in xpath expressions Tjerk Wolterink XML 1 11-13-2004 06:03 PM
Are there any XPath parsers that generate XPath trees? goog XML 0 01-14-2004 01:47 PM
XPath that does not include other XPath Anna XML 0 07-31-2003 07:55 AM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments