xpath question

B

Bill

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" bk:publicationdate="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>
<bk:price>11.99</bk:price>
</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>
 
M

Martin Honnen

Bill said:
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.
 
B

Bill

OK, so remove the stylesheet statement. I am only interested in how
xpath is used to return a nodeset directly from the xml document.

Bill said:
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.
 
M

Martin Honnen

Bill said:
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>
 
B

Bill

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.

Bill said:
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>
 
M

Martin Honnen

Bill said:
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-XPath/xpath.html#XPathEvaluator-createNSResolver
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.
 
J

Joe Kesselman

Bill said:
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.
 
B

Bill

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.
 
J

Joe Kesselman

Bill said:
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.
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top