xpath question

Discussion in 'XML' started by Bill, Jun 18, 2009.

  1. Bill

    Bill Guest

    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>
     
    Bill, Jun 18, 2009
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Jun 18, 2009
    #2
    1. Advertising

  3. Bill

    Bill Guest

    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 <> 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/
     
    Bill, Jun 18, 2009
    #3
  4. 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/
     
    Martin Honnen, Jun 18, 2009
    #4
  5. Bill

    Bill Guest

    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 <> 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/
     
    Bill, Jun 18, 2009
    #5
  6. 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-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.

    --

    Martin Honnen
    http://msmvps.com/blogs/martin_honnen/
     
    Martin Honnen, Jun 18, 2009
    #6
  7. 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.
     
    Joe Kesselman, Jun 18, 2009
    #7
  8. Bill

    Bill Guest

    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 <>
    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.
     
    Bill, Jun 18, 2009
    #8
  9. 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.
     
    Joe Kesselman, Jun 19, 2009
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    2,010
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,482
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    555
  4. goog
    Replies:
    0
    Views:
    527
  5. Tjerk Wolterink

    XPath: efficiency in xpath expressions

    Tjerk Wolterink, Nov 13, 2004, in forum: XML
    Replies:
    1
    Views:
    1,681
    Richard Tobin
    Nov 13, 2004
Loading...

Share This Page