Import xs:attributeGroups to multiple namespaces x

2019-08-02 17:54发布

问题:

I have an attributeGroup with long list of allowed attributes say a:attr-group, that I import to another xsd to restrict an element's (defined in another xsd) allowed attributes with.

I also want to allow the same element to use the same attributes under a second namespace say b:attr-group, and wish to use the same file (rather than repeat all the attributes and group definition).

Is there a simple way of doing this? All my attempts so far have been thwarted by the import namespace must equal targetNamespace rule.

Thanks in advance!

回答1:

This pattern is also known as chameleon. It means you're including an XML Schema that has no target namespace, which in turn makes that schema assume the namespace of the parent schema.

UPDATE: considering the sample XML provided in the comments:

First schema file (AttrGroup.xsxd):

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema 
elementFormDefault="qualified" attributeFormDefault="qualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:attributeGroup name="attr-group">
        <xsd:attribute name="attr1" type="xsd:string"/>
        <xsd:attribute name="attr2" type="xsd:int"/>
    </xsd:attributeGroup>
</xsd:schema>

Second schema file (A.xsd):

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd/a"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd/a"
xmlns:a="http://tempuri.org/XMLSchema.xsd/a"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:include schemaLocation="AttrGroup.xsd"/>

    <xsd:attributeGroup name="a-group">
        <xsd:attributeGroup ref="attr-group"/>
    </xsd:attributeGroup>

</xsd:schema>

Third schema file (B.xsd):

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd/b"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd/b"
xmlns:b="http://tempuri.org/XMLSchema.xsd/b"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:include schemaLocation="AttrGroup.xsd"/>

    <xsd:attributeGroup name="b-group">
        <xsd:attributeGroup ref="attr-group"/>
    </xsd:attributeGroup>
</xsd:schema>

Fourth XML Schema (Element.xsd):

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)-->
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:a="http://tempuri.org/XMLSchema.xsd/a" xmlns:b="http://tempuri.org/XMLSchema.xsd/b">
    <xsd:import namespace="http://tempuri.org/XMLSchema.xsd/a" schemaLocation="A.xsd"/>
    <xsd:import namespace="http://tempuri.org/XMLSchema.xsd/b" schemaLocation="B.xsd"/>

    <xsd:element name="element">
        <xsd:complexType>
            <xsd:attributeGroup ref="a:a-group"/>
            <xsd:attributeGroup ref="b:b-group"/>           
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

Sample of valid XML:

<element xmlns:a="http://tempuri.org/XMLSchema.xsd/a" xmlns:b="http://tempuri.org/XMLSchema.xsd/b" a:attr1="hello" b:attr2="10" />

Sample of invalid XML:

<element xmlns:a="http://tempuri.org/XMLSchema.xsd/a" xmlns:b="http://tempuri.org/XMLSchema.xsd/b" a:attr1="hello" b:attr2="10a" />

When validating against Element.xsd the invalid sample, I am getting this error message (on my tool): Error occurred while loading [], line 1 position 116 The 'http://tempuri.org/XMLSchema.xsd/b:attr2' attribute is invalid - The value '10a' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:int' - The string '10a' is not a valid Int32 value. D:...\SampleAttrGroup.xml is invalid.