Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD to define an element with attributes and required content

Tags:

xml

xsd

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>
like image 892
Bob.at.Indigo.Health Avatar asked Oct 19 '25 14:10

Bob.at.Indigo.Health


1 Answers

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>
like image 125
user1431765 Avatar answered Oct 21 '25 12:10

user1431765