Bob.at.Indigo.Health
Bob.at.Indigo.Health

Reputation: 11915

XSD to define an element with attributes and required content

Ok, this has to be easy, but I can't find a decent explanation or example of how to do it...

I want to specify that one of my XML elements can have some attributes, and that the element content must be non-blank text. For example, this is valid:

<person age="30">Bob</person>

but these constructs are invalid because the element text is missing:

<person age="30"></person>
<person age="30" />

FWIW, my existing schema (fragment), which does NOT enforce the "required text content" rule, looks like this. I assume that I want to add an xs:restriction block somewhere in this model, but I can't figure out where it belongs.

<xs:element name="person">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="age" type="xs:int" use="optional" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

Upvotes: 4

Views: 1962

Answers (1)

user1431765
user1431765

Reputation: 153

Define a type with required content like this:

<xs:simpleType name="textType">
  <xs:restriction base="xs:string">
    <xs:pattern value=".+"/>
  </xs:restriction>
</xs:simpleType>

After that you can use it as a base type for your element:

<xs:element name="person">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="textType">
        <xs:attribute name="age" type="xsd:int"></xs:attribute>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

Upvotes: 4

Related Questions