First off, your xml is invalid, you should send us what you are actually using. But after fixing your example (some of the end tags have the "/" at the end instead of something like "</FirstName>") you should do the following:

Element person=people.selectNodes("//Person[@id='282']");
Element firstName= person.element("FirstName");

This will select from the people element all Person tags with and id attribute of 282.

I highly recommend using XPE to play with the correct XPath expressions. It will allow you to "click" on the tag in question and tell you what the XPath would be to reach it. The nicely done tool can be found at
http://www.purpletech.com/xpe .

Also, take a look at the XPath local-name() function, that should help you get around the unknown namespace. You can also do indexing on your list like "//Person[1]" or "//Person[22]" etc.


Hope that helps!

Dave



Joe wrote:
Hi,

Firstly, how do you do an xpath search on just an
element?

<People>
 <Person id="282">
   <FirstName>Jenny<FirstName/>
   <LastName>Ynnej<LastName/>
   <Age>1009<Age/>
   <PhoneNumber>777 777 7777</PhoneNumber>
 </Person>
 <Person id="289">
 ...
 ...
</People>

<People/> is the root element of the document.
So lets say you have the <Person> element  with id =
"282" in an Element object, how do you perform an
xpath *relatve to just that element*.

personElement.selectNodes("/People/Person/FirstName")
works fine,
but
personElement.selectNodes("/Person/FirstName") does
not, so each xpath search is *still* performed on the
document as a whole, regardless if you perform it on a
sub element.

So how do you perform a *relative* search? The "//" is
not suitable here. Example: perhaps <PhoneNumber/>'s
element is 1 -> Infinity and you only want Jenny's
phone numbers and not every one in the entire
document.

And the second question is about namespaces and xpath.
How do you do a search when you don't know what the
xml namespace will be in the document, only the
structure. So it might be <cool:People/>,
<addressBook:People/> or whatever. Current xpath
requires you to include the namespace in the path,
"/cool:People/cool:Person/..." and so on. What would
be the best way to do this dynamically?

Regards,
Joe

--

+------------------------------------------------------------+
| David Lucas                        mailto:[EMAIL PROTECTED]  |
| Lucas Software Engineering, Inc.   (740) 964-6248 Voice    |
| Unix,Java,C++,CORBA,XML,EJB        (614) 668-4020 Mobile   |
| Middleware,Frameworks              (888) 866-4728 Fax/Msg  |
+------------------------------------------------------------+
| GPS Location:  40.0150 deg Lat,  -82.6378 deg Long         |
| IMHC: "Jesus Christ is the way, the truth, and the life."  |
| IMHC: "I know where I am; I know where I'm going."    <><  |
+------------------------------------------------------------+

Notes: PGP Key Block=http://www.lse.com/~ddlucas/pgpblock.txt
IMHO="in my humble opinion" IMHC="in my humble conviction"
All trademarks above are those of their respective owners.




-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to