2012-09-24 70 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"/>