1

My system parses two XML files (via JAXB unmarshalling and xjc generated code)

  • componentModel.xml : generated from a modeling tool
  • detailedDesign.xml : manual, to add informations

In order to avoid to code checks upon content and structures, each file is validated with its own XSD

  • componentModel.xsd
  • detailedDesign.xsd

Both of these xsd inclulde "utilities.xsd" where common complex and simple types are defined

All works well

I now want to add consistency check :

  • unicity of Ids (names of components)
  • components in "detailsDesign" must refers one of the components in "componentModel" via the "name" and "nameRef" attributes

Here are the structures (i've simlplified names and structures in files to show only relevant informations) Note : as you can see, i don't use any namespace except the default one

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>

It works for unicity of 'name' attribute among components (error retrieved from ValidationEventHandler set on unmarshaller) :

Duplicate key value [id1] declared for identity constraint of element "components".

But i can't get the "keyref" functionnality working (error retrieved from ValidationEventHandler set on unmarshaller) :

Identity Constraint error: identity constraint "KeyRef@272ed83b" has a keyref which refers to a key or unique that is out of scope.

How can i make the keyRef to see the Key defined in the orther XSD file

Thanks

Matth

MatthG
  • 13
  • 5

0 Answers0