2017-09-01 71 views
2

命名空間xmlns在「父級」中定義,並在「子級」中覆蓋。既然我的xsi在「parent」和「child」中是一樣的,我是否也需要重寫「child」中的xsi命名空間?屬於覆蓋命名空間的命名空間會發生什麼?

<parent xmlns="namespace_A" xmlns:xsi="namespace_C" xsi:schemaLocation="namespace_D"> 
     <child xmlns="namespace_B" xsi:schemaLocation="namespace_E"> 
     </child> 
</parent> 

所有在線驗證我試圖驗證XML作爲接受,但處理XML,它說XSI沒有在「小孩」的約束,當我得到一個錯誤。

其中我有這個問題的特定代碼是:

<?xml version="1.0" encoding="UTF-8"?> 
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"> 
    <responseDate>2017-08-24T12:54:26</responseDate> 
    <request verb="ListRecords" from="2017-08-08" set="J:10.1007:53599" metadataPrefix="CR_UNIXML" resumptionToken="91554975-0bb1-4cf5-86ae-b2222e6fe01f">http://oai.crossref.org/OAIHandler</request> 
    <!-- recipient 96 crlabs2 --> 
    <ListRecords> 
     <record> 
     <header> 
      <!-- citation-id: 92292627; type: JOURNAL_ARTICLE; --> 
      <identifier>info:doi/10.1007/s40278-017-34281-1</identifier> 
      <datestamp>2017-08-11</datestamp> 
      <setSpec>J</setSpec> 
      <setSpec>J:10.1007</setSpec> 
      <setSpec>J:10.1007:53599</setSpec> 
     </header> 
     <!-- [email protected] --> 
     <metadata> 
      <crossref xmlns="http://www.crossref.org/xschema/1.1" xsi:schemaLocation="http://www.crossref.org/xschema/1.1 http://www.crossref.org/schema/unixref1.1.xsd"> 

這是給出由外部服務響應的XML。我只是想處理一些由它接受XSLT文件來獲取所需的數據相同的外部服務給予了處理器的數據,但我得到以下錯誤:

ERROR: 'The prefix "xsi" for attribute "xsi:schemaLocation" associated with an element type "crossref" is not bound.' 
ERROR: 'com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: The prefix "xsi" for attribute "xsi:schemaLocation" associated with an element type "crossref" is not bound.' 

在類XMLNSDocumentScannerImpl出現的錯誤,方法scanStartElement()。在下面的循環中,uri爲空,並引發錯誤。

// bind attributes (xmlns are already bound bellow) 
      int length = fAttributes.getLength(); 
      // fLength = 0; //initialize structure 
      for (int i = 0; i < length; i++) { 
       fAttributes.getName(i, fAttributeQName); 

       String aprefix = fAttributeQName.prefix != null 
         ? fAttributeQName.prefix : XMLSymbols.EMPTY_STRING; 
       String uri = fNamespaceContext.getURI(aprefix); 
       // REVISIT: try removing the first "if" and see if it is faster. 
       // 
       if (fAttributeQName.uri != null && fAttributeQName.uri == uri) { 
        // checkDuplicates(fAttributeQName, fAttributes); 
        continue; 
       } 
       if (aprefix != XMLSymbols.EMPTY_STRING) { 
        fAttributeQName.uri = uri; 
        if (uri == null) { 
         fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 
           "AttributePrefixUnbound", 
           new Object[]{fElementQName.rawname,fAttributeQName.rawname,aprefix}, 
           XMLErrorReporter.SEVERITY_FATAL_ERROR); 
        } 
        fAttributes.setURI(i, uri); 
        // checkDuplicates(fAttributeQName, fAttributes); 
       } 
      } 
+0

如果你的文件真的是這樣開始的(畢竟你沒有展示它是如何結束的),那麼是的,給出這個錯誤在處理器端是一個問題。但是我不能讓Xerces因這個bug而失敗。你能顯示哪些Java代碼觸發這個錯誤? – kumesana

+0

用Xerces中的錯誤代碼再次更新了問題。 – nrad0

回答

3

什麼都不會發生在被覆蓋的命名空間。它們不再是由相應的前綴或默認設計的命名空間。就這些。

使用相同的名稱空間URI「覆蓋」相同的xmlns:前綴沒有任何效果。正如你已經注意到你的xmlns:xsi總是和它一樣,它不需要在根元素中定義。

另請注意,雖然允許,但不需要在根元素中定義xsi:schemaLocation。您可以直接給出第一個xsi:schemaLocation中所有命名空間的所有模式的完整列表,從而避免使用另一個命名空間。

All the online validators I tried verify the xml as accepted but I get an error when processing the xml, which says xsi is not bound in "child".

在你給出的例子中,xsi確實是綁定的。聲稱它不是的處理器是錯誤的。這是錯誤的,給出不正確的結果。

但是,也許你的真實文件不完全如你舉了一個例子。

這是不可能的,但可能的是,一個綁定在上升元素中的前綴將在後代元素中解綁定。所以,需要示例。

+0

我已經更新了我的問題,並提出了我的問題。 – nrad0

+0

另外,感謝您的幫助。 – nrad0