using mixed="true" and trying to set a type

This should cover W3C XML Schema, Relax NG and DTD related problems.
ryanparrish
Posts: 2

using mixed="true" and trying to set a type

Fri Feb 13, 2009 10:33 pm

I may be going at this the entirely wrong way, so don't shy away from pointing out glaring mistakes :)

What I'm trying to get is an element that looks like this

Code: Select all

<Weight unit="lbs">12.5</Weight>


with the following schema...

Code: Select all

<element name="Weight">
    <complexType mixed="true">
        <attribute name="unit" use="required">
            <simpleType>
                <restriction base="xs:string">
                    <enumeration value="lbs"/>
                    <enumeration value="kgs"/>
                </restriction>
            </simpleType>
        </attribute>
    </complexType>
</element>   


And that works fine and all except I would like to define a type (xs:decimal) to the <Weight> element so users of my schema cannot put in strings. However it appears to not be valid to set a type when I have mixed="true", so what am I to do?
george
Site Admin
Posts: 2100

Re: using mixed="true" and trying to set a type

Fri Feb 13, 2009 10:46 pm

You need mixed to specify that you have elements and text. In that case you cannot put any other restriction on the text. However, in your case you do not have elements so you can just extend a complex type with simple content from decimal and add the attribute you need:

Code: Select all

    <xs:element name="Weight">
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="xs:decimal">
                    <xs:attribute name="unit" use="required">
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:enumeration value="lbs"/>
                                <xs:enumeration value="kgs"/>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:attribute>
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>


Best Regards,
George
George Cristian Bina
ryanparrish
Posts: 2

Re: using mixed="true" and trying to set a type

Fri Feb 13, 2009 11:06 pm

Thank you so much, that is exactly what I have been looking for for days now :D

Return to “XML Schemas”

Who is online

Users browsing this forum: No registered users and 1 guest