xpath读取根元素

时间:2012-09-24 17:28:10

标签: xml xpath

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

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

但当我用作根元素时

<?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>而不是<beans xmlns="http ......

谢谢!

1 个答案:

答案 0 :(得分:2)

XPath 1.0中的

/beans 总是表示名为“beans”的元素,它与任何名称空间都没有明确关联。在

的情况下
<beans xmlns="http://www.springframework.org/schema/beans">

元素位于http://www.springframework.org/schema/beans命名空间中,因此您需要将该命名空间URI映射到前缀,然后在路径表达式中使用该前缀。如何进行前缀映射取决于您用来解释xpath的工具。例如,在XSLT中,将xmlns:b="http://www.springframework.org/schema/beans"添加到样式表的根元素(而不是输入文档)就足够了,然后就可以执行

之类的操作了。
<xsl:apply-templates select="/b:beans/b:bean"/>