I’m a beginner with XML Schema and I’m trying to solve a (in my opinion) rather simple problem: I want to match a tag in the form
<foo bar="123">some text</foo>
i.e. a tag with both text and and attribute. Basically, I know how this can be done with the extension facility. It seems rather unintuitive, but works. This is the basic idiom:
<xs:element name="option">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="value" type="xs:string">
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
However, I also want to impose restrictions on text and attribute! The text shouldn’t exceed a certain length and the attribute should be in an integer in a certain range. How can I achieve that? It seems that I cannot use restrictions for the text when I use an extension.
Use
<xs:restriction>instead of extension. You may want to declare the simple types separately and refer to them in other constructs.EDIT: apologies for taking my time. Went to some event yesterday, as always it turned out you can’t get anywhere in my country’s traffic and I arrived late to the point of simply turning back screaming and cursing. I spent the evening getting drunk instead.
But now I’m sober and even in that state this is the best I managed to come up with:
Oh my gawd. So apparently you’re supposed to make a restriction of an extension. The above will restrict element
option‘s content to a string of max length 10 and attributevalueto an integer in range [0, 10], inclusive.Yeah, that sure isn’t too verbose…