XPATH Syntax to query?

A

arthuryeung198

Hello all,

I have got an XML looks like the following. How can I use an XPATH
command to select //hostname and //instance? If there is no instance in
the sever, just skip, so it returns

<hostname>Host A</hostname>
<instance name="TOM" port="12345"/>
<instance name="TOM1" port="12346"/>
<hostname>Host B</hostname>
<hostname>Host C</hostname>
<instance name="TOM2" port="12345"/>
<instance name="TOM3" port="12346"/>


Basically, I want to select all the hostname, and certain field in the
<server> block.

Thanks!

=============
<?xml version="1.0" encoding="UTF-8"?>
<servers>
<server>
<record_info>
<lastchecked>20050316</lastchecked>
<hostname>Host A</hostname>
</record_info>
<components>
<instance name="TOM" port="12345"/>
<instance name="TOM1" port="12346"/>
</components>
<supp_info>
<contact name="TOM" email=""/>
<contact name="MARY" email=""/>
</supp_info>
</server>
<server>
......
</server>
<server>
......
</server>
</servers>

=============
 
R

Richard Tobin

I have got an XML looks like the following. How can I use an XPATH
command to select //hostname and //instance?

//hostname | //instance

-- Richard
 
J

Joseph Kesselman

The XPath to "all hostname elements, at any dept" is simply //hostname,
which you seem to be aware of... so I presume your question is about how
to request that the XPath be executed, and that depends on what
environment you're working in (which you didn't tell us).
 
A

arthuryeung198

"Joseph Kesselman дµÀ£º
"
The XPath to "all hostname elements, at any dept" is simply //hostname,
which you seem to be aware of... so I presume your question is about how
to request that the XPath be executed, and that depends on what
environment you're working in (which you didn't tell us).

Thanks Richard and Joseph,

//hostname | //instance kind of works, but it will give like all the
hostname, and then all the instance

<hostname>
<hostname>
<hostname>
<instance>
<instance>
<instance>

but i need to have them in order,
like
<hostname>
<instance>
<instance>
<hostname> << if there is no instance then just return nothing

<hostname>
<instance>

Sorry I should have mentioned, I am Java and the JDOM API

Any more advice please

Thanks!!
 
J

Joe Kesselman

//hostname | //instance kind of works, but it will give like all the
hostname, and then all the instance

That XPath expression should yield everything in document order. If that
isn't what you're seeing, your implementation of XPath is broken.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top