2017-08-15 29 views
1

我使用Apache XmlSchema 2.2.1來解析XSD架構。我有以下模式:創建QName時,本地部分不能爲「null」

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema targetNamespace="http://www.example.com/aigu" 
     xmlns="http://www.example.com/aigu" 
     xmlns:xs="http://www.w3.org/2001/XMLSchema" 
     xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="2.0"> 
    <xs:attribute name="label" type="xs:string" /> 
    <xs:element name="object"> 
     <xs:complexType> 
      <xs:attribute ref="label" form="unqualified"/> 
     </xs:complexType> 
    </xs:element> 
</xs:schema> 

下面的代碼產生異常

import org.apache.ws.commons.schema.XmlSchemaCollection; 
import org.xml.sax.InputSource; 

import java.io.ByteArrayInputStream; 
import java.nio.charset.StandardCharsets; 

public class Aigu { 
    public static void main(String[] args) { 
     String schema = "HERE_IS_CONTENT_OF_SCHEMA"; 
     XmlSchemaCollection collection = new XmlSchemaCollection(); 
     collection.read(new InputSource(new ByteArrayInputStream(schema.getBytes(StandardCharsets.UTF_8)))); 
    } 
} 

堆棧跟蹤:

Exception in thread "main" java.lang.IllegalArgumentException: local part cannot be "null" when creating a QName 
    at javax.xml.namespace.QName.<init>(QName.java:244) 
    at javax.xml.namespace.QName.<init>(QName.java:188) 
    at org.apache.ws.commons.schema.utils.XmlSchemaNamedWithFormImpl.setName(XmlSchemaNamedWithFormImpl.java:117) 
    at org.apache.ws.commons.schema.utils.XmlSchemaNamedWithFormImpl.setForm(XmlSchemaNamedWithFormImpl.java:105) 
    at org.apache.ws.commons.schema.XmlSchemaAttribute.setForm(XmlSchemaAttribute.java:170) 
    at org.apache.ws.commons.schema.SchemaBuilder.handleAttribute(SchemaBuilder.java:959) 
    at org.apache.ws.commons.schema.SchemaBuilder.handleAttribute(SchemaBuilder.java:923) 
    at org.apache.ws.commons.schema.SchemaBuilder.handleComplexType(SchemaBuilder.java:307) 
    at org.apache.ws.commons.schema.SchemaBuilder.handleElement(SchemaBuilder.java:420) 
    at org.apache.ws.commons.schema.SchemaBuilder.handleSchemaElementChild(SchemaBuilder.java:1512) 
    at org.apache.ws.commons.schema.SchemaBuilder.handleXmlSchemaElement(SchemaBuilder.java:659) 
    at org.apache.ws.commons.schema.SchemaBuilder.build(SchemaBuilder.java:157) 
    at org.apache.ws.commons.schema.XmlSchemaCollection.read(XmlSchemaCollection.java:508) 
    at org.apache.ws.commons.schema.XmlSchemaCollection.read(XmlSchemaCollection.java:717) 
    at org.apache.ws.commons.schema.XmlSchemaCollection.read(XmlSchemaCollection.java:565) 
    at com.netcracker.mediation.transition.model.xmltojava.Aigu.main(Aigu.java:23) 

它是在Apache的代碼中的錯誤或我的模式是無效的?

回答

1

屬性form不能用於具有ref(如在this paragraph of the XML Schema specification的點3.2中限制)的屬性使用中。

另外,由於引用的目標是具有目標名稱空間的模式中的頂級屬性聲明,因此它的form(如果允許明確說明)必須是qualified

它可能會解釋錯誤,因爲跟蹤似乎表明它發生在那裏。

這將是糾正模式:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema targetNamespace="http://www.example.com/aigu" 
    xmlns="http://www.example.com/aigu" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="2.0"> 
    <xs:attribute name="label" type="xs:string" /> 
    <xs:element name="object"> 
     <xs:complexType> 
      <xs:attribute ref="label"/> 
     </xs:complexType> 
    </xs:element> 
</xs:schema> 
+0

非常感謝您! –