2

I have a schema that I would like to convert the simpleType into anonymous simpleType. Original schema is as below, I did several attempts such as:

Removing base="xxx";

Adding the simpleType after the extension;

Etc...

But ended up with invalid schema.

Questions:

  1. How to convert the input into a valid anonymous simpleType schema without losing information?
  2. Further more, is there xslt to do this automatically with dynamic input on the element name, type name etc?

Thanks in advance,

Input Schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="test" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="test" elementFormDefault="qualified">
<xs:element name="elem1">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="simpleType1">
                <xs:attribute name="att1" type="simpleType2" use="required"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>
<xs:simpleType name="simpleType1">
    <xs:restriction base="xs:decimal">
        <xs:fractionDigits value="5"/>
        <xs:totalDigits value="18"/>
        <xs:minInclusive value="0"/>
    </xs:restriction>
</xs:simpleType>
<xs:simpleType name="simpleType2">
    <xs:restriction base="xs:string">
        <xs:pattern value="[A-Z]{3,3}"/>
    </xs:restriction>
</xs:simpleType>
</xs:schema>

My attempted result:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="test" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="test" elementFormDefault="qualified">
<xs:element name="elem1">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension>
                <xs:simpleType>
                    <xs:restriction base="xs:decimal">
                        <xs:fractionDigits value="5"/>
                        <xs:totalDigits value="18"/>
                        <xs:minInclusive value="0"/>
                    </xs:restriction>
                </xs:simpleType>
                <xs:attribute name="att1" use="required"/>
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:pattern value="[A-Z]{3,3}"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>
</xs:schema>

Validation shows that this is an invalid schema with errors such as:

Attribute 'base' is required in element .

Element is not allowed under element .

Attribute 'name' is required in element .

dellair
  • 427
  • 4
  • 22

1 Answers1

1

It is not possible in XSD to use purely anonymous, local definitions when declaring an element to have both restricted content and an attribute.

You can, of course, use a local definition for the type of the attribute (att1) alone, but you cannot for the type (simpleType1) of the restricted simple content of the element (elem1) if you need to also declare att1 on elem1.

See also: How to restrict element content when element has attribute

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Yes, in fact, I've managed to use the local defined type on attribute, but not able to do it with the 'extension base' on simpleType1... – dellair Feb 08 '17 at 18:22