Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XPath Question

Reply
Thread Tools

XPath Question

 
 
nihmrat
Guest
Posts: n/a
 
      02-14-2008
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().getQuali fiedName());
assertEquals(2,doc.getRootElement().elements().siz e());
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"
xmlnssi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="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/schem...-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"
pgnlProperties="#{'psid':'1001'}"
p:linkAlias="ps" p:condition="ps.product_structure_id=$
{psid}"/>
<bean id="criteriaProductStatus"
class="com.company.component.sql.Criteria"
pgnlProperties="#{'statusCode':'1'}"
p:linkAlias="ps" p:condition="ps.product_status_cr_fk in ($
{statusCode})"/>
</beans>
--------------
End XML
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      02-14-2008
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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Memory leak" in javax.xml.xpath.XPath Marvin_123456 Java 4 07-29-2005 03:49 PM
XPath: efficiency in xpath expressions Tjerk Wolterink XML 1 11-13-2004 06:03 PM
Are there any XPath parsers that generate XPath trees? goog XML 0 01-14-2004 01:47 PM
XPath that does not include other XPath Anna XML 0 07-31-2003 07:55 AM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments