Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > xpath question

Reply
Thread Tools

xpath question

 
 
Bill
Guest
Posts: n/a
 
      06-18-2009
Which elements in the xml file below should the following xpath match
on?

//bk:book[bk:author/bk:first-name='Herman']

I am thinking all three books by Herman Melville should match because
2 of the books have a default namespace, although different in each
case, the rule is that the prefix used in the xpath expression to
match a default namespace may be arbitrary. The book with the bk
namespace should also match because the prefix in the xpath exactly
matches the prefixed namespace.

<?xml version="1.0" standalone="yes"?>
<?xml-stylesheet type="text/xml" href="books.xsl"?>
<bookstore xmlns="http://www.google.com" >
<book>
<title>ASP in a nutshell</title>
</book>
<book genre="autobiography" publicationdate="1981-03-22"
ISBN="1-861003-11-0" xmlns="http://www.yahoo.com">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin<nickname>Benjy</nickname>
</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967-11-17"
ISBN="0-201-63361-2" xmlns="http://www.yahoo.com">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<bk:book bk:genre="novel" bkublicationdate="1967-11-17"
bk:ISBN="0-201-63361-2" xmlns:bk="c:\temp">
<bk:title>The Confidence Man</bk:title>
<bk:author>
<bk:first-name>Herman</bk:first-name>
<bk:last-name>Melville</bk:last-name>
</bk:author>
<bkrice>11.99</bkrice>
</bk:book>
<book genre="novel" publicationdate="1967-11-17"
ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991-02-15"
ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
<fname />
</author>
<price>9.99</price>
</book>
</bookstore>

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      06-18-2009
Bill wrote:
> Which elements in the xml file below should the following xpath match
> on?
>
> //bk:book[bk:author/bk:first-name='Herman']
>
> I am thinking all three books by Herman Melville should match because
> 2 of the books have a default namespace, although different in each
> case, the rule is that the prefix used in the xpath expression to
> match a default namespace may be arbitrary. The book with the bk
> namespace should also match because the prefix in the xpath exactly
> matches the prefixed namespace.


The XPath expression using the prefix bk can only be evaluated if you
show us how the prefix is mapped to a namespace URI. So you need to show
that mapping.
The input document and its namespace prefixes are not relevant, the
mapping comes from an XSLT stylesheet or some means in the XPath API you
use/want to use.



--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
 
 
 
Bill
Guest
Posts: n/a
 
      06-18-2009
OK, so remove the stylesheet statement. I am only interested in how
xpath is used to return a nodeset directly from the xml document.

On Jun 18, 4:51*am, Martin Honnen <(E-Mail Removed)> wrote:
> Bill wrote:
> > Which elements in the xml file below should the following xpath match
> > on?

>
> > //bk:book[bk:author/bk:first-name='Herman']

>
> > I am thinking all three books by Herman Melville should match *because
> > 2 of the books have a default namespace, although different in each
> > case, the rule is that the prefix used in the xpath expression to
> > match a default namespace may be arbitrary. The book with the bk
> > namespace should also match because the prefix in the xpath exactly
> > matches the prefixed namespace.

>
> The XPath expression using the prefix bk can only be evaluated if you
> show us how the prefix is mapped to a namespace URI. So you need to show
> that mapping.
> The input document and its namespace prefixes are not relevant, the
> mapping comes from an XSLT stylesheet or some means in the XPath API you
> use/want to use.
>
> --
>
> * * * * Martin Honnen
> * * * *http://msmvps.com/blogs/martin_honnen/


 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      06-18-2009
Bill wrote:
> OK, so remove the stylesheet statement. I am only interested in how
> xpath is used to return a nodeset directly from the xml document.


You had the following XPath in your initial post:

>>> //bk:book[bk:author/bk:first-name='Herman']


That path uses the prefix 'bk' to qualify element names so to be able to
evaluate that XPath expression we need to know how that prefix is mapped
to a namespace URI. You did not provide any information about that
mapping so it is not possible to tell which elements the XPath selects.
If you use XPath outside of XSLT then you need some namespace manager or
resolver to map prefixes to namespace URIs if your XPath expression
contains prefixes.

Let's say we map 'bk' to 'http://www.yahoo.com', one of the namespace
URIs in your XML sample, then that XPath expression would select
elements with local name 'book' in the namespace 'http://www.yahoo.com'
where there is a child element with local name 'author' in the same
namespace that has a child element with local name 'first-name' in the
same namespace where the string value is 'Herman'. So it would select

<book genre="novel" publicationdate="1967-11-17"
ISBN="0-201-63361-2" xmlns="http://www.yahoo.com">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>


--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
Bill
Guest
Posts: n/a
 
      06-18-2009
Got it. I looked for an xpath expression tester but none of the ones I
found support namespaces, but it should not be hard to do. I think all
that is required is the user must provide the xpath expression, the
prefix and the the namespaceuri they are interested in. then the test
program would use a namespacemanger and add the namespace. So if the
user inputs

//bk:book[bk:author/bk:first-name='Herman']
bk
'http://www.yahoo.com'

then the program addes the namespace:

NameSpaceManager nsmgr=new NameSpaceManager();
nsmgr.AddNameSpace("bk", 'http://www.yahoo.com');

and then the program would use SelectNodes passing the xpath and
nsmgr.

Or, the xpath test program could provide an option to match against
all namespaces and go through the document executing the AddNameSpace
method for every namespace found, but not sure if anyone would ever
want that.

On Jun 18, 6:49*am, Martin Honnen <(E-Mail Removed)> wrote:
> Bill wrote:
> > OK, so remove the stylesheet statement. I am only interested in how
> > xpath is used to return a nodeset directly from the xml document.

>
> You had the following XPath in your initial post:
>
> >>> //bk:book[bk:author/bk:first-name='Herman']

>
> That path uses the prefix 'bk' to qualify element names so to be able to
> evaluate that XPath expression we need to know how that prefix is mapped
> to a namespace URI. You did not provide any information about that
> mapping so it is not possible to tell which elements the XPath selects.
> If you use XPath outside of XSLT then you need some namespace manager or
> resolver to map prefixes to namespace URIs if your XPath expression
> contains prefixes.
>
> Let's say we map 'bk' to 'http://www.yahoo.com', one of the namespace
> URIs in your XML sample, then that XPath expression would select
> elements with local name 'book' in the namespace 'http://www.yahoo.com'
> where there is a child element with local name 'author' in the same
> namespace that has a child element with local name 'first-name' in the
> same namespace where the string value is 'Herman'. So it would select
>
> * * *<book genre="novel" publicationdate="1967-11-17"
> ISBN="0-201-63361-2" xmlns="http://www.yahoo.com">
> * * * * *<title>The Confidence Man</title>
> * * * * *<author>
> * * * * * * *<first-name>Herman</first-name>
> * * * * * * *<last-name>Melville</last-name>
> * * * * *</author>
> * * * * *<price>11.99</price>
> * * *</book>
>
> --
>
> * * * * Martin Honnen
> * * * *http://msmvps.com/blogs/martin_honnen/


 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      06-18-2009
Bill wrote:

> Or, the xpath test program could provide an option to match against
> all namespaces and go through the document executing the AddNameSpace
> method for every namespace found, but not sure if anyone would ever
> want that.


The W3C DOM Level 3 XPath API
http://www.w3.org/TR/DOM-Level-3-XPa...eateNSResolver
allows you to create an XPathNSResolver from a DOM node for instance.
The only problem with that is that often XML documents use a default
namespace declaration and XPath 1.0 has no notion of a default namespace.

--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      06-18-2009
Bill wrote:
> Got it. I looked for an xpath expression tester but none of the ones I
> found support namespaces


The JAXP/TrAX APIs definitely support namespaces in XPaths.
 
Reply With Quote
 
Bill
Guest
Posts: n/a
 
      06-18-2009
Right -I was looking for a desktop tool. Most all the xml products I
am familiar with are written in Java -xmlspy, stylus studio, and I
think all of the native xml databases, but no majorxml products or
APIs that has been developed in .Net. do you know why that might be?
are there limitations to the .net xml classes? I know that some of the
xml gurus that hang out on the xml-dev mailing list dont think much
of .net and all seem to be very heavy into Java for xml development. I
wonder why that is.

On Jun 18, 1:11*pm, Joe Kesselman <(E-Mail Removed)>
wrote:
> Bill wrote:
> > Got it. I looked for an xpath expression tester but none of the ones I
> > found support namespaces

>
> The JAXP/TrAX APIs definitely support namespaces in XPaths.


 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      06-19-2009
Bill wrote:
> wonder why that is.


Reasons I can think of immediately:

Most of the W3C's XML work, and the early development efforts, predate
the .net introduction.

..net is Microsoft-specific. Most of us who write software want to be
able to run it on non-Microsoft platforms as well.

Java bytecodes are extremely portable since they will run unaltered on
multiple architectures. This makes Java a good "least common denominator".

I'm sure there are others. I'm also sure there are counter arguments.
Speaking entirely for myself, .net just doesn't interest me.
 
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