EK.
EK.

Reputation: 2996

xpath read root element

I want to read a root element in my xml file. When i read it from XPath "beans/bean/......", it works fine. The XML input file begins:

<?xml version="1.0" encoding="UTF-8"?>
<!-- The file defines all the configurations of the SemanticServer -->
<beans>
  <bean class="org.spr 

but when I use as root element

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

instead of <beans> it does not work.

How can I read root element when it empty <beans> and not <beans xmlns="http ......

Thanks!

Upvotes: 1

Views: 336

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122364

/beans in XPath 1.0 always means an element named "beans" that is not explicitly associated with any namespace. In the case of

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

the element is in the http://www.springframework.org/schema/beans namespace, so you need to map that namespace URI to a prefix then use that prefix in your path expressions. How you do the prefix mapping depends on what tool you're using to interpret your xpaths. In XSLT, for example, it is sufficient to add an xmlns:b="http://www.springframework.org/schema/beans" to the root element of the stylesheet (not the input document), then you can do things like

<xsl:apply-templates select="/b:beans/b:bean"/>

Upvotes: 2

Related Questions