2017-09-01 69 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