我使用Apache XmlSchema 2.2.1来解析XSD架构。我有以下架构:
<?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代码中的错误还是我的架构无效?
答案 0 :(得分:1)
属性form
不能在具有ref
的属性使用中使用(在this paragraph of the XML Schema specification中的第3.2点中受约束)。
此外,由于引用的目标是具有目标命名空间的模式中的顶级属性声明,因此如果允许将其form
显式放置,则qualified
必须为{{1} }。
它可以解释错误,因为跟踪似乎表明它发生在那里。
这将是更正的架构:
<?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>