473,399 Members | 3,603 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,399 software developers and data experts.

xpath query query

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
Jul 20 '05 #1
2 1730
David Gordon <no****@nospam.com> 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
Jul 20 '05 #2
> ...
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
Jul 20 '05 #3

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

Similar topics

1
by: Joshua Beall | last post by:
Hi All, I have a task that should be very simple but I'm running into trouble. All I want to do is query a document using XPath, and save the resulting XML in a string. Here's that I am trying...
6
by: 0wl | last post by:
Hi, I am trying to get the value of child from xmlstr = """<p:root xmlns:p="http://tempuri.org/string"><p:child DataType="String">Hellpppp</p:child></p:root>""" using...
8
by: Terry P | last post by:
Are there any tools (java classes, tag libraries) which can translate xpath statements into a SQL query? Given an xpath query which has a predicate that filters node values or attributes, I want...
3
by: gfrommer | last post by:
Hello Everyone, I'm writing a server in java, and I want the clients to pass me an XPath query. I want the XPath queries to be in a specific format though, I'm pretty sure it's valid but I want...
7
by: Ot | last post by:
I posted this to the wrong group. It went to m.p.dotnet.languages.vb. Ooops. -------------------------------------------------------------------- I have this tiny problem. I have learned...
10
by: Michael C# | last post by:
OK, here's the deal. I have a small XML file that represents a small database table. I load it into a System.XML.XMLDocument. So far so good. I run an XPath query against it to retrieve all the...
5
by: Gnic | last post by:
Hi , I have an XmlDocument instance, I want to find a node in the xml, but I don't know it's path until runtime, for example <aaa> <bbb name="x"/> <aaa attr="y"> <ccc>sometext</ccc> </aaa>
6
by: dotnetnoob | last post by:
i would like to know how i can build xpath expression dynamiclly. let's say i have a following xml file: <EventEnrollment InstanceNumber = "675"> <EventSource> <ObjectReference...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
2
by: =?Utf-8?B?RGlmZmlkZW50?= | last post by:
Hello All, I am trying to construct an XPath query against an XML document, for a requirement that I have. Below is the XML fragment: <SUBMISSIONS> <SUBMISSION YEAR="2004"> <MONTH...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.