Olumide
Olumide

Reputation: 5809

Setting element content of an XSD element that has attributes

I'm having difficulty declaring an element that contains a text string and has two text attributes:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This schema is not valid -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="Favorite" type="xs:string">
        <xs:complexType>
            <xs:attribute name="car"    type="xs:string"/>
            <xs:attribute name="fruit" type="xs:string"/>
        </xs:complexType>
    </xs:element>       
</xs:schema>

The schema passes if I drop the attributes, like so:

<?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="Favorite" type="xs:string">
        </xs:element>
    </xs:schema>

Or if I omit the type="xs:string" in <xs:element name="Favorite" type="xs:string">.

The desired schema is supposed to validate the following XML file:

<?xml version="1.0" encoding="UTF-8"?>
<Favorite car="Volvo" fruit="banana">These are a few of my favorite things</Favorite>

PS: I'm sorry if this question is rather trivial. I'm still a beginner at XSD.

Upvotes: 0

Views: 103

Answers (1)

Olumide
Olumide

Reputation: 5809

Found the answer, from w3schools:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Favorite">
        <xs:complexType>

            <xs:simpleContent>
                <xs:extension base="xs:string">
                    <xs:attribute name="car" type="xs:string"/>                 
                    <xs:attribute name="fruit" type="xs:string"/>   
                </xs:extension>
            </xs:simpleContent>

        </xs:complexType>
    </xs:element>
</xs:schema>

Upvotes: 2

Related Questions