在XSD中使用keyRef:如何引用位于不同XSD中的键(超出范围错误)

时间:2017-11-09 11:31:58

标签: xsd jaxb schema xsd-validation keyref

我的系统分析两个XML文件(通过JAXB解组和xjc生成的代码)

  • componentModel.xml:从建模工具生成
  • detailedDesign.xml:manual,添加信息

为了避免对内容和结构进行代码检查,每个文件都使用自己的XSD进行验证

  • componentModel.xsd
  • detailedDesign.xsd

这两个xsd inclulde" utilities.xsd"其中定义了常见的复杂和简单类型

一切正常

我现在想添加一致性检查:

  • Ids的独特性(组件名称)
  • 组件" detailsDesign"必须引用" componentModel"中的一个组件。通过"名称"和" nameRef"属性

以下是结构(我在文件中简化了名称和结构,仅显示相关信息) 注意:正如您所看到的,我不会使用除默认名称之外的任何名称空间

componentModel.xml

<?xml version="1.0" encoding="UTF-8"?>
<componentModel>
    <components>
        <component name="id1"> ... </component>
        <component name="id1"> ... </component>
        <component name="id2"> ... </component>
    </components>
</componentModel>

componentModel.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:include schemaLocation="utilities.xsd" />
<xs:complexType name="CMComponent">
    <xs:complexContent>
            ...
            <xs:attribute name="name" type="MUUpperFirstName" use="required" />
    </xs:complexContent>
</xs:complexType>
<xs:complexType name="CMComponents">
        <xs:sequence>
        <xs:element name="component" type="CMComponent" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
</xs:complexType>
<xs:complexType name="CMComponentModel">
    <xs:complexContent>
            <xs:sequence>
                <xs:element name="components" type="CMComponents">
                    <!--  Key used to check "name" unicity and as a ref by nameRef attribute in detailedDesign model -->
                    <xs:key name="componentNameKey">
                        <xs:selector xpath="component" />
                        <xs:field xpath="@name" />
                    </xs:key>
                </xs:element>
                <xs:element name="functionalChains" minOccurs="0" maxOccurs="1" />
            </xs:sequence>
            <xs:attribute name="name" type="MUModelName" use="required" />
    </xs:complexContent>
</xs:complexType>

<xs:element name="componentModel" type="CMComponentModel" />
</xs:schema>

utilities.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="MUString">
    <xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="MUUpperFirstName">
    <xs:restriction base="MUString">
        <xs:pattern value="([A-Z]+[a-zA-Z0-9]*[.])*[A-Z]+[a-zA-Z0-9]*"/>
    </xs:restriction>
</xs:simpleType>
<xs:simpleType name="MUReferenceName">
    <xs:restriction base="MUString">
        <xs:pattern value="([A-Z]+[a-zA-Z0-9]*[.])*[a-zA-Z]+[a-zA-Z0-9]*"/>
    </xs:restriction>
</xs:simpleType>
</xs:schema>

detailedDesign.xml

<?xml version="1.0" encoding="UTF-8"?>
<detailedDesignModel>
    <components>
        <component nameRef="id1"> ... </component>
        <component nameRef="id2"> ... </component>
        <component nameRef="id3"> ... </component>
    </components>
</detailedDesignModel>

detailedDesign.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="utilities.xsd" />
<xs:include schemaLocation="componentModel.xsd" />
<xs:complexType name="DDMComponent">
    <xs:complexContent>
        ...
        <xs:attribute name="nameRef" type="MUReferenceName" use="required" />
    </xs:complexContent>
</xs:complexType>
<xs:complexType name="DDMComponents">
    <xs:sequence>           
        <xs:element name="component" type="DDMComponent" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
</xs:complexType>
<xs:complexType name="DDMDetailDesignModel">
    <xs:complexContent>
        <xs:sequence>
            <xs:element name="components" type="DDMComponents">
                <xs:keyref name="componentNameKeyRef" refer="componentNameKey">
                    <xs:selector xpath="component" />
                    <xs:field xpath="@nameRef" />
                </xs:keyref>
            </xs:element>
        </xs:sequence>
    </xs:complexContent>
</xs:complexType>

<xs:element name="detailDesignModel" type="DDMDetailDesignModel" />
</xs:schema>

它适用于“名称”的独特性。组件之间的属性(从ValidationEventHandler上设置的unmarshaller检索到的错误):

  

为元素&#34;组件&#34;的身份约束声明的重复键值[id1]。

但我无法获得&#34; keyref&#34; functionnality working(从ValidationEventHandler上设置的unmarshaller检索到的错误):

  

身份约束错误:身份约束&#34; KeyRef @ 272ed83b&#34;有一个keyref引用超出范围的键或唯一。

如何让keyRef看到在其他XSD文件中定义的密钥

谢谢

Matth

0 个答案:

没有答案