Xalan-j XPathAPI and namespaces

Discussion in 'XML' started by Dino Morelli, Nov 3, 2003.

  1. Dino Morelli

    Dino Morelli Guest

    Looking for someone familiar with Xalan-j..

    I'm having problems isolating a node in a Document with Xalan-j's
    XPathAPI class when that element is in a namespace.

    using:
    Xalan-j v2.5.1
    JDK v1.4.2-b28

    Given an XML document that looks like this:

    <a xmlns:foo="foo-ns">
    <b>value 1</b>
    <foo:b>value 2</foo:b>
    </a>

    These calls return null:

    // Document doc contains the parsed above document
    Node n = XPathAPI.selectSingleNode(doc, "/a/foo:b");
    Node n = XPathAPI.selectSingleNode(doc, "/a/foo:b",
    doc.getDocumentElement());

    However, this xpath works, retrieving the second b with no namespace
    explicitly requested:

    "/a/b[2]"


    Does anyone know how to make the first type of xpath "/a/food:b" work?


    --
    Dino Morelli .~.
    http://www.debian.org Debian GNU/Linux /V\
    /( )\
    ^^-^^
    Dino Morelli, Nov 3, 2003
    #1
    1. Advertising

  2. Dino Morelli

    Dino Morelli Guest

    Re: Xalan-j XPathAPI and namespaces SOLVED

    On Mon, 3 Nov 2003, Dino Morelli wrote:

    >I'm having problems isolating a node in a Document with Xalan-j's
    >XPathAPI class when that element is in a namespace.
    >
    >Given an XML document that looks like this:
    >
    ><a xmlns:foo="foo-ns">
    > <b>value 1</b>
    > <foo:b>value 2</foo:b>
    ></a>
    >
    >These calls return null:
    >
    >// Document doc contains the parsed above document
    >Node n = XPathAPI.selectSingleNode(doc, "/a/foo:b");
    >Node n = XPathAPI.selectSingleNode(doc, "/a/foo:b",
    > doc.getDocumentElement());
    >


    I figured this out for myself.

    When constructing the DocumentBuilder to parse the XML, you must set up
    the DocumentBuilderFactory like this:

    DocumentBuilderFactory dbf =
    DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ important part

    Then get your DocumentBuilder:

    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(/* yada yada RTFM */);

    In order for subsequent XPath processing to be namespace aware.
    Then this is all that's necessary to use it:

    Node n = XPathAPI.selectSingleNode(doc, "/a/foo:b");


    --
    Dino Morelli .~.
    http://www.debian.org Debian GNU/Linux /V\
    /( )\
    ^^-^^
    Dino Morelli, Nov 4, 2003
    #2
    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. Joona I Palaste

    Xalan XPathAPI question

    Joona I Palaste, Jan 7, 2004, in forum: Java
    Replies:
    11
    Views:
    2,853
    Mike Schilling
    Jan 11, 2004
  2. Erwan
    Replies:
    0
    Views:
    611
    Erwan
    Oct 20, 2004
  3. cvissy
    Replies:
    0
    Views:
    592
    cvissy
    Nov 16, 2004
  4. Replies:
    0
    Views:
    426
  5. Replies:
    2
    Views:
    398
    Joe Kesselman
    Nov 1, 2006
Loading...

Share This Page