Friday, February 29, 2008

XML Schema - element with text and attributes

For some reason I always forget how to define an element that contains only text but also has attributes. Perhaps it's because it's so verbose, or so non-intuitive for something so simple, who knows. Either way it's something that needs to be committed to memory...

So the element:

<foo bar="bar" baz="baz"/>

is described using:

<xs:complexType name="foo">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="bar" type="xs:string"/>
<xs:attribute name="baz" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>

nice!

2 comments:

muck said...

What's wrong with the following declaration?

<xs:element name="foo">
<xs:complexType mixed="true">
<xs:attribute name="bar" type="xs:string"/>
<xs:attribute name="baz" type="xs:string"/>
</xs:complexType>
</xs:element>


Up to now, I've thought this was the canonical way...

Anesha said...
This comment has been removed by a blog administrator.