XPath Question

N

nihmrat

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>
 
M

Martin Honnen

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

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top