tyguy
tyguy

Reputation: 358

Validate a element and an attribute with xsd

<spokenLanguages>
        <language fluency="3">German</language>
        <language fluency="3">English</language>
        <language fluency="1">Spanish</language>
    </spokenLanguages>

I am trying to validate this xml document so that I can have at least a minimal of 2 language and a maximum of unbounded. I tried using this code

<xsd:complexType name="LanguageType">
<xsd:choice minOccurs="2">
    <xsd:element name="language" type="xsd:string" minOccurs="2" maxOccurs="unbounded">     
        <xsd:attribute name="fluency" use="required">
                <xsd:restriction base="xsd:integer">
                    <xsd:enumeration value="1"/>
                    <xsd:enumeration value="2"/>
                    <xsd:enumeration value="3"/>
                    <xsd:enumeration value="4"/>
                </xsd:restriction>
        </xsd:attribute>    
    </xsd:element>
</xsd:choice>

but it isn't validating. Any suggestions??

Upvotes: 1

Views: 33

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522817

You had several problems with your XSD schema, including that you never defined an <xsd:element> for the spokenLanguages tag. Also, the fluency attribute should be defined as a simple type.

<?xml version="1.0" encoding="utf-16"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
    version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="spokenLanguages">
        <xsd:complexType>
            <xsd:sequence minOccurs="2" maxOccurs="unbounded">
                <xsd:element name="language">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:integer">
                            <xsd:enumeration value="1"/>
                            <xsd:enumeration value="2"/>
                            <xsd:enumeration value="3"/>
                            <xsd:enumeration value="4"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

Upvotes: 1

Related Questions