3

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>
4

1 回答 1

2

定义一个具有所需内容的类型,如下所示:

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

之后,您可以将其用作元素的基本类型:

<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>
于 2013-02-03T00:21:09.467 回答