递归 XML 嵌套时架构类型验证失败
Recursive XML Schema Type Validation fails when nested
我有两个XML Schemas:computation.xsd、math.xsd,如下:
computation.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://computation.eqec.org/schema"
xmlns:tns="http://computation.eqec.org/schema" xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" blockDefault="#all">
<xs:element name="Computation" type="tns:ComputationType"
abstract="true">
</xs:element>
<xs:element name="Set" type="tns:ComputationType"
substitutionGroup="tns:Computation" />
<!-- <xs:complexType name="SetType"> <xs:complexContent> <xs:restriction
base="tns:ComputationType"> <xs:attribute name="class" use="required"> <xs:simpleType>
<xs:restriction base="xs:string"> <xs:enumeration value="org.eqec.computation.Set"
/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:restriction> </xs:complexContent>
</xs:complexType> -->
<xs:complexType name="ComputationType">
<xs:complexContent>
<xs:extension base="tns:ComputationContentType">
<xs:sequence>
<xs:element name="Configuration" type="tns:ConfigurationType"
minOccurs="0" maxOccurs="1" />
</xs:sequence>
<!-- <xs:attribute name="class" type="xs:string" use="required" /> -->
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="ConfigurationType">
<xs:sequence>
<xs:element name="SupportedTypes">
<xs:complexType>
<xs:sequence>
<xs:element name="Type" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="validityrange" type="xs:string"
use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="complex" type="xs:boolean" />
</xs:complexType>
</xs:element>
<xs:element name="OtherConfigs">
<xs:complexType>
<xs:sequence>
<xs:element name="Config1" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="Config2" type="xs:string" minOccurs="1"
maxOccurs="1" />
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="minArgs" type="xs:integer" use="required" />
<xs:attribute name="maxArgs" type="xs:integer" use="required" />
<xs:attribute name="repetative" type="xs:boolean" />
</xs:complexType>
<xs:complexType abstract="true" name="ComponentType">
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="externalize" type="xs:boolean" />
<xs:anyAttribute namespace="##any" processContents="lax" />
</xs:complexType>
<xs:complexType name="ValueType">
<xs:complexContent>
<xs:extension base="tns:ComponentType">
<xs:group ref="tns:ObjectTypeChoice" />
<xs:attribute name="ref" type="xs:string" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:group name="ObjectTypeChoice">
<xs:choice>
<xs:element name="decimal" type="xs:decimal" minOccurs="0"
maxOccurs="1" />
<xs:element name="string" type="xs:string" minOccurs="0"
maxOccurs="1" />
<xs:element name="boolean" type="xs:boolean" minOccurs="0"
maxOccurs="1" />
<xs:element name="integer" type="xs:integer" minOccurs="0"
maxOccurs="1" />
<xs:element name="datetime" type="xs:dateTime" minOccurs="0"
maxOccurs="1" />
<xs:element name="base64binary" type="xs:base64Binary"
minOccurs="0" maxOccurs="1" />
<xs:any namespace="##other" processContents="lax" minOccurs="0"
maxOccurs="1" />
</xs:choice>
</xs:group>
<xs:complexType name="ComputationContentType" abstract="true">
<xs:complexContent>
<xs:extension base="tns:ComponentType">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="tns:Computation" />
<xs:element name="Value" type="tns:ValueType" />
<xs:any namespace="##other" processContents="lax" />
</xs:choice>
</xs:sequence>
<xs:attribute name="class" type="xs:string" use="required" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
math.xsd:
<xs:schema targetNamespace="http://computation.eqec.org/schema/math"
xmlns:tns="http://computation.eqec.org/schema/math" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:op="http://computation.eqec.org/schema" elementFormDefault="qualified"
blockDefault="#all">
<xs:import namespace="http://computation.eqec.org/schema"
schemaLocation="computation.xsd" />
<xs:element name="Add" type="op:ComputationType"
substitutionGroup="op:Computation" />
<xs:element name="Subtract" type="op:ComputationType"
substitutionGroup="op:Computation" />
<xs:element name="Multiply" type="op:ComputationType"
substitutionGroup="op:Computation" />
<xs:element name="Devide" type="op:ComputationType"
substitutionGroup="op:Computation" />
<xs:element name="Sqrt" type="op:ComputationType"
substitutionGroup="op:Computation" />
<xs:element name="Square" type="op:ComputationType"
substitutionGroup="op:Computation" />
</xs:schema>
如果我验证以下 XML 实例
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Set xmlns="http://computation.eqec.org/schema" xmlns:ns2="http://computation.eqec.org/schema/math" class="org.eqec.computation.Set" name="valuesAndComputationCollection" externalize="false">
<Value name="three">
<decimal>3.00000</decimal>
</Value>
<Value name="four">
<decimal>4</decimal>
</Value>
<Set class="org.eqec.computation.Set" name="ComputationWithRefs" externalize="false">
<Value ref="three"/>
<Value ref="four"/>
</Set>
<ns2:Add class="org.eqec.computation.math.Add" name="threePlusFour" externalize="true">
<Value name="tenPointOne">
<decimal>10.1</decimal>
</Value>
<Value name="hundred">
<decimal>100</decimal>
</Value>
</ns2:Add>
<Value ref="threePlusFour"/>
</Set>
我收到这个错误:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'Set'. One of '{"http://
computation.eqec.org/schema":Computation, "http://computation.eqec.org/schema":Value, WC[##other:"http://
computation.eqec.org/schema"], "http://computation.eqec.org/schema":Configuration}' is expected.
但是,当我验证有问题的片段时,只有验证成功且没有错误:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Set xmlns="http://computation.eqec.org/schema" xmlns:ns2="http://computation.eqec.org/schema/math" class="org.eqec.computation.Set" name="myset" externalize="true">
<Value ref="three"/>
<Value ref="four"/>
</Set>
我怎样才能更改我的架构,使上面的长 XML 实例无误地验证?
我刚刚找到解决方案:我必须从架构定义中删除属性 blockDefault="#all",因为它被定义为
Optional. Specifies the default value of the block attribute on element and complexType elements in the target namespace. The block attribute prevents a complex type (or element) that has a specified type of derivation from being used in place of this complex type. This value can contain #all or a list that is a subset of extension, restriction, or substitution:
extension - prevents complex types derived by extension
restriction - prevents complex types derived by restriction
substitution - prevents substitution of elements
#all - prevents all derived complex types
我有两个XML Schemas:computation.xsd、math.xsd,如下:
computation.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://computation.eqec.org/schema"
xmlns:tns="http://computation.eqec.org/schema" xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" blockDefault="#all">
<xs:element name="Computation" type="tns:ComputationType"
abstract="true">
</xs:element>
<xs:element name="Set" type="tns:ComputationType"
substitutionGroup="tns:Computation" />
<!-- <xs:complexType name="SetType"> <xs:complexContent> <xs:restriction
base="tns:ComputationType"> <xs:attribute name="class" use="required"> <xs:simpleType>
<xs:restriction base="xs:string"> <xs:enumeration value="org.eqec.computation.Set"
/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:restriction> </xs:complexContent>
</xs:complexType> -->
<xs:complexType name="ComputationType">
<xs:complexContent>
<xs:extension base="tns:ComputationContentType">
<xs:sequence>
<xs:element name="Configuration" type="tns:ConfigurationType"
minOccurs="0" maxOccurs="1" />
</xs:sequence>
<!-- <xs:attribute name="class" type="xs:string" use="required" /> -->
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="ConfigurationType">
<xs:sequence>
<xs:element name="SupportedTypes">
<xs:complexType>
<xs:sequence>
<xs:element name="Type" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="validityrange" type="xs:string"
use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="complex" type="xs:boolean" />
</xs:complexType>
</xs:element>
<xs:element name="OtherConfigs">
<xs:complexType>
<xs:sequence>
<xs:element name="Config1" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="Config2" type="xs:string" minOccurs="1"
maxOccurs="1" />
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="minArgs" type="xs:integer" use="required" />
<xs:attribute name="maxArgs" type="xs:integer" use="required" />
<xs:attribute name="repetative" type="xs:boolean" />
</xs:complexType>
<xs:complexType abstract="true" name="ComponentType">
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="externalize" type="xs:boolean" />
<xs:anyAttribute namespace="##any" processContents="lax" />
</xs:complexType>
<xs:complexType name="ValueType">
<xs:complexContent>
<xs:extension base="tns:ComponentType">
<xs:group ref="tns:ObjectTypeChoice" />
<xs:attribute name="ref" type="xs:string" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:group name="ObjectTypeChoice">
<xs:choice>
<xs:element name="decimal" type="xs:decimal" minOccurs="0"
maxOccurs="1" />
<xs:element name="string" type="xs:string" minOccurs="0"
maxOccurs="1" />
<xs:element name="boolean" type="xs:boolean" minOccurs="0"
maxOccurs="1" />
<xs:element name="integer" type="xs:integer" minOccurs="0"
maxOccurs="1" />
<xs:element name="datetime" type="xs:dateTime" minOccurs="0"
maxOccurs="1" />
<xs:element name="base64binary" type="xs:base64Binary"
minOccurs="0" maxOccurs="1" />
<xs:any namespace="##other" processContents="lax" minOccurs="0"
maxOccurs="1" />
</xs:choice>
</xs:group>
<xs:complexType name="ComputationContentType" abstract="true">
<xs:complexContent>
<xs:extension base="tns:ComponentType">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="tns:Computation" />
<xs:element name="Value" type="tns:ValueType" />
<xs:any namespace="##other" processContents="lax" />
</xs:choice>
</xs:sequence>
<xs:attribute name="class" type="xs:string" use="required" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
math.xsd:
<xs:schema targetNamespace="http://computation.eqec.org/schema/math"
xmlns:tns="http://computation.eqec.org/schema/math" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:op="http://computation.eqec.org/schema" elementFormDefault="qualified"
blockDefault="#all">
<xs:import namespace="http://computation.eqec.org/schema"
schemaLocation="computation.xsd" />
<xs:element name="Add" type="op:ComputationType"
substitutionGroup="op:Computation" />
<xs:element name="Subtract" type="op:ComputationType"
substitutionGroup="op:Computation" />
<xs:element name="Multiply" type="op:ComputationType"
substitutionGroup="op:Computation" />
<xs:element name="Devide" type="op:ComputationType"
substitutionGroup="op:Computation" />
<xs:element name="Sqrt" type="op:ComputationType"
substitutionGroup="op:Computation" />
<xs:element name="Square" type="op:ComputationType"
substitutionGroup="op:Computation" />
</xs:schema>
如果我验证以下 XML 实例
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Set xmlns="http://computation.eqec.org/schema" xmlns:ns2="http://computation.eqec.org/schema/math" class="org.eqec.computation.Set" name="valuesAndComputationCollection" externalize="false">
<Value name="three">
<decimal>3.00000</decimal>
</Value>
<Value name="four">
<decimal>4</decimal>
</Value>
<Set class="org.eqec.computation.Set" name="ComputationWithRefs" externalize="false">
<Value ref="three"/>
<Value ref="four"/>
</Set>
<ns2:Add class="org.eqec.computation.math.Add" name="threePlusFour" externalize="true">
<Value name="tenPointOne">
<decimal>10.1</decimal>
</Value>
<Value name="hundred">
<decimal>100</decimal>
</Value>
</ns2:Add>
<Value ref="threePlusFour"/>
</Set>
我收到这个错误:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'Set'. One of '{"http:// computation.eqec.org/schema":Computation, "http://computation.eqec.org/schema":Value, WC[##other:"http:// computation.eqec.org/schema"], "http://computation.eqec.org/schema":Configuration}' is expected.
但是,当我验证有问题的片段时,只有验证成功且没有错误:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Set xmlns="http://computation.eqec.org/schema" xmlns:ns2="http://computation.eqec.org/schema/math" class="org.eqec.computation.Set" name="myset" externalize="true">
<Value ref="three"/>
<Value ref="four"/>
</Set>
我怎样才能更改我的架构,使上面的长 XML 实例无误地验证?
我刚刚找到解决方案:我必须从架构定义中删除属性 blockDefault="#all",因为它被定义为
Optional. Specifies the default value of the block attribute on element and complexType elements in the target namespace. The block attribute prevents a complex type (or element) that has a specified type of derivation from being used in place of this complex type. This value can contain #all or a list that is a subset of extension, restriction, or substitution:
extension - prevents complex types derived by extension
restriction - prevents complex types derived by restriction
substitution - prevents substitution of elements
#all - prevents all derived complex types