Welcome to WebmasterWorld Guest from 34.204.203.142

Forum Moderators: open

Message Too Old, No Replies

Anyone know much about XML XPath Syntax?

Anyone know much about XML XPath Syntax?

     
10:10 am on Feb 18, 2007 (gmt 0)

New User

10+ Year Member

joined:July 18, 2004
posts:14
votes: 0


In a XML file:

<purse>
<coin>
<value>0.5</value>
<name lang="en">half dollar</name>
</coin>
<coin>
<value>0.25</value>
<name lang="en">quarter</name>
</coin>
</purse>

What is the XPath expression that would give the name of the first attribute element of the first name element?

guessing this, but I am really not sure...

name(/purse/coin[1]/name/@[1])

or maybe

name(/purse/name[1]/@[1])

?

thanks

11:26 am on Feb 18, 2007 (gmt 0)

New User

10+ Year Member

joined:Apr 16, 2006
posts:15
votes: 0


Attributes in XML are unordered (unlike elements). So, even if you could construct an XPath expression to grab the first attribute from an element, any XPath parser would be free to decide what constitutes the first attribute of an element. While your favourite parser might work, you would find that any another parser might not - thus reducing your document's usefulness.

The solution would be to move the data from an attribute to an element, whose ordering parsers must obey.

For a few rants on the subject trying searching for "xml attribute ordering".

Looking at your example, it would seem natural for the "lang" element to be an attribute. Why can you not select for /purse/coin[1]/name[@lang="en"]?

11:50 am on Feb 18, 2007 (gmt 0)

New User

10+ Year Member

joined:July 18, 2004
posts: 14
votes: 0


/purse/coin[1]/name[@lang="en"]?



would probably work, however, this is for an assignment for a Java class. Our textbook is terribly brief and I cannot find a clear answer in it.

I think I need to use some other path statement that doesn't directly refer to the attribute by specifying the name.

thanks

12:22 pm on Feb 18, 2007 (gmt 0)

New User

10+ Year Member

joined:Apr 16, 2006
posts:15
votes: 0


If you are looking to grab all of the attributes of an element & then use Java to pick through them, you can use an XPath expression like

/purse/coin[1]/name/attribute::*

Dunno if this might help.

2:16 pm on Feb 18, 2007 (gmt 0)

New User

10+ Year Member

joined:July 18, 2004
posts:14
votes: 0


No, it doesn't but thanks...

I need one statement that does it, without directly specifying the attribute name, since the name function returns the name.