2013-08-21 61 views
0

我有這樣的XML:XPath和命名空間,選擇節點迭代

<?xml version="1.0" encoding="UTF-8"?> 
<documentAnswer xmlns="http://schemas.pkh.hr/doc/2013/documents/rda_30" domainName="rda" domainVersion="3.0"> 
<answerTimestamp>2013-08-21T13:35:25.894</answerTimestamp> 
<correct> 
    <docId>RDA2_29F81D27-1409BE49E2E-7FF8</docId> 
    <attachments> 
     <attachment> 
      <format>application/pdf</foraat> 
      <generatedType>StampanNaBlanko</generatedType> 
      <encodedPdf>JVBERiYKJSVFT0YK</encodedPdf> 
     </attachment> 
    </attachments> 
</correct> 

那些附件標籤可以是一個或多個。我試圖用

XPathAPI.selectNodeIterator(節點, 「/ documentAnswer /正確/附件/附件」)

,然後重複,以獲得附件NodeIterator在java中標籤,但我我沒有成功。我猜這個問題在xpath中,它與命名空間有關,但不知道如何解決它。我試着用這種的XPath,但沒有成功:

XPathAPI.selectNodeIterator(節點, 「/ RDA:documentAnswer /正確/附件/附件」, 「RDA」, 「http://schemas.pkh.hr/doc/2013/documents/rda_30」)

回答

1
/rda:documentAnswer/correct/attachments/attachment 

在這裏你只根元素分配rda命名空間,但它在XML作爲默認命名空間中聲明。因此,所有您的元素都在該名稱空間中。您必須使用

/rda:documentAnswer/rda:correct/rda:attachments/rda:attachment 

不幸的是,XPath標準不支持默認命名空間的聲明。

0

3種方法:

A-嵌入命名空間的QName查詢不僅本地名稱()

與CORRE反恐執行報頭:

<ns0:documentAnswer xmlns:ns0="http://schemas.pkh.hr/doc/2013/documents/rda_30" domainName="rda" domainVersion="3.0"> 

的xpath

/ns0:documentAnswer 

B-僅查詢本地名字與本地名稱()函數

/*[local-name()="documentAnswer"] 

C-查詢本地名稱和名稱空間(查詢QNames)

/*[local-name()="documentAnswer" and namespace-uri()='http://schemas.pkh.hr/doc/2013/documents/rda_30'] 

看到http://nealwalters.blogspot.fr/2005/01/common-xpath-examples-questions-issues.html

0

如果你喜歡,你可以把它忽略了命名空間是這樣的:

/*:documentAnswer/*:correct/*:attachments/*:attachment 
+0

只有在XPath 2.0中。如果這是我們談論的Xalan的XPathAPI,那麼它只支持XPath 1.0,它允許'prefix:*'而不是'*:localname'。 –

+0

@伊·羅伯茨,是的,你是對的。我的錯誤忽略它,對不起! – jvverde