dietr
dietr

Reputation: 1238

XML Schema: element with attributes and text with restrictions

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.

Upvotes: 3

Views: 3934

Answers (2)

user2314351
user2314351

Reputation: 393

I have encountered similar problem, and I'd rather define 2 simpleTypes. One for the attribute, the other for the content.

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:simpleType name="restrictedLengthString">
        <xs:restriction base="xs:string">
            <xs:maxLength value="10"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="boundedInteger">
        <xs:restriction base="xs:integer">
            <xs:minInclusive value="0" />
            <xs:maxInclusive value="10" />
        </xs:restriction>
    </xs:simpleType>

    <xs:element name="foo">
        <xs:complexType>
            <xs:simpleContent> 
                <xs:extension base="restrictedLengthString">
                    <xs:attribute name="bar" type="boundedInteger"/>
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>

</xs:schema>

Upvotes: 1

G_H
G_H

Reputation: 12019

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:

<xs:element name="option">
    <xs:complexType>
        <xs:simpleContent>
            <xs:restriction base="optionType">
                <xs:maxLength value="10" />
            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>

<xs:complexType name="optionType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute name="value">
                <xs:simpleType>
                    <xs:restriction base="xs:integer">
                        <xs:minInclusive value="0" />
                        <xs:maxInclusive value="10" />
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

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 attribute value to an integer in range [0, 10], inclusive.

Yeah, that sure isn't too verbose...

Upvotes: 3

Related Questions