XPath Question

Discussion in 'XML' started by nihmrat, Feb 14, 2008.

  1. nihmrat

    nihmrat Guest

    While trying to interrogate a spring configuration XML document I was
    trying to iterate results from
    /beans/bean
    This path returned no results, neither did "/beans"

    XPath only seemed to return results I wanted for the path
    /*[name()='beans']/*[name()='bean']

    Would someone explain or reference why?
    I suspect it may have something to do with namespaces, but don't
    really know where to look.


    Start Unit Test Method
    ------------------------------
    public void testDocument() throws DocumentException {
    URL r = getClass().getResource("MarshallingTestBeans.xml");
    Document doc = new SAXReader().read(r);
    assertNotNull(doc);
    assertEquals("beans",doc.getRootElement().getQualifiedName());
    assertEquals(2,doc.getRootElement().elements().size());
    Element b = (Element) doc.selectSingleNode("/*[name()='beans']");
    assertNotNull(b);
    assertEquals(2,doc.selectNodes("/*[name()='beans']/
    *[name()='bean']").size());

    // Why does the following line fail???????
    assertEquals(2,doc.selectNodes("/beans/bean").size());

    }
    ------------------------------
    End Unit Test Method



    Start XML
    --------------
    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-2.5.xsd"
    >

    <bean id="criteriaProductStructureId"
    class="com.company.component.sql.Criteria"
    p:eek:gnlProperties="#{'psid':'1001'}"
    p:linkAlias="ps" p:condition="ps.product_structure_id=$
    {psid}"/>
    <bean id="criteriaProductStatus"
    class="com.company.component.sql.Criteria"
    p:eek:gnlProperties="#{'statusCode':'1'}"
    p:linkAlias="ps" p:condition="ps.product_status_cr_fk in ($
    {statusCode})"/>
    </beans>
    --------------
    End XML
    nihmrat, Feb 14, 2008
    #1
    1. Advertising

  2. nihmrat wrote:
    > While trying to interrogate a spring configuration XML document I was
    > trying to iterate results from
    > /beans/bean
    > This path returned no results, neither did "/beans"
    >
    > XPath only seemed to return results I wanted for the path
    > /*[name()='beans']/*[name()='bean']
    >
    > Would someone explain or reference why?
    > I suspect it may have something to do with namespaces, but don't
    > really know where to look.



    > <beans
    > xmlns="http://www.springframework.org/schema/beans"


    With that default namespace declaration the root element and its
    descendants are in the namespace
    http://www.springframework.org/schema/beans.
    For XPath 1.0 to select elements in a namespace you need to bind a
    prefix to the namespace URI and use that prefix e.g. if you bind 'bs' to
    'http://www.springframework.org/schema/beans' then you can use
    /bs:beans/bs:bean

    So check the documentation of your XPath API how you can bind a prefix
    to a namespace URI.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Feb 14, 2008
    #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. Marvin_123456

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

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

    XPath: efficiency in xpath expressions

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

Share This Page