递归XML模式类型验证在嵌套时失败

时间:2015-10-25 10:00:38

标签: java xml xsd jaxb

我有两个XML Schema: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:从元素&#39; Set&#39;开始发现无效内容。 &#39; {&#34; http://    computation.eqec.org/schema":Computation," http://computation.eqec.org/schema&#34;:Value,WC [## other:&#34; http://    computation.eqec.org/schema"],&#34; http://computation.eqec.org/schema&#34;:配置}&#39;是预期的。

但是,当我验证有问题的片段时,只有验证成功且没有错误:

<?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实例无错误地验证?

1 个答案:

答案 0 :(得分:0)

我刚刚找到解决方案:我必须从架构定义中删除属性 blockDefault =&#34;#all&#34; ,因为它被定义为

  

可选。指定目标命名空间中element和complexType元素的block属性的默认值。块属性可防止使用具有指定派生类型的复杂类型(或元素)来代替此复杂类型。此值可以包含#all或作为扩展,限制或替换的子集的列表:

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