2012-09-24 69 views
1

我想读取我的xml文件中的根元素。 当我从XPath "beans/bean/......"中读取它时,它工作正常。 XML输入文件的开头:xpath读取根元素

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

但是当我的根元素使用的<beans>

<?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"> 

,而不是它不工作。

我如何读取根元素时,空<beans>,而不是<beans xmlns="http ......

谢谢!

+0

你想读的根元素豆或它的孩子豆?哦,我们需要你使用的代码/ xpath语句。我在猜测命名空间,但这是一个猜测。 –

+0

例如,我有bean

回答

2

/beans in XPath 1.0 always表示名称为“beans”的元素不与任何名称空间显式关联。在

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

的元素为在http://www.springframework.org/schema/beans命名空间,所以你需要该命名空间URI映射到一个前缀,然后使用该前缀在路径表达式。你如何做前缀映射取决于你用什么工具来解释你的xpaths。在XSLT,例如,就足够了一个xmlns:b="http://www.springframework.org/schema/beans"添加到样式表的根元素(不是输入文档),那么你可以做这样的事情

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