XML Schemas

While writing a schema for the xml documents my simulation model accepts as input, I came across a tricky situation. How to define an element that has an attribute, but which also has plain text content between it’s tags (rather than more elements or nothing).

Since I knew very little about schemas a couple of days ago, I found that all tutorials and examples completely missed this combination which I’m sure must be more common than made out.

Here is my solution:

<pre>&lt;xsd:complexType name="test"&gt;
 &lt;xsd:simpleContent&gt;
  &lt;xsd:extension base="xsd:string"&gt;
   &lt;xsd:attribute name="name" type="xsd:string"/&gt;
  &lt;/xsd:extension&gt;
 &lt;/xsd:simpleContent&gt;
&lt;/xsd:complexType&gt;</pre>

Attributes go within the extension tag, and you need simpleContent rather than complexContent. It makes load of sense, so I’m not complaining about the design. It was just very hard to find somewhere that explained it.



5 comments ↓

#1   andrew brown on 05.09.06 at 5:30 pm

xml is pretty awesome. see the latest version of ms office now has its native file type as xml. i think ms is bowing down to standards unlike the past and thats something to be happy about. even if they otherwise do suck

#2   Joel on 05.09.06 at 5:38 pm

yeah, at least we can be grateful for small miracles 🙂

#3   Bob on 05.10.06 at 9:58 am

It is a very small miricle. Compare the OpenDocument (also XML) format to MSXML.

Not human readable or easily computer parsable. Yuck.

#4   Joel on 05.10.06 at 3:18 pm

Uck. I’m sorry I said it was a miracle. MSXML is awful, even after doing minor work with XML I can see silly design decisions.

#5   andrew brown on 05.10.06 at 3:22 pm

at least it’s open so other people can now read it without having to guess it’s inner workings

Leave a Comment