1

I'd like to validate an element having a string attribute and a string value :

<?xml version="1.0" encoding="UTF-8"?>
<name sex="M">eric</name>

I used :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="name">
  <xs:complexType>
    <xs:restriction base="xs:string"/>
    <xs:attribute name="sex" type="xs:string"/>
  </xs:complexType>
</xs:element>
</xs:schema>

It says the the content is not valid.

The doc says that an element with an attribute is always "complexType". If I omit the xs:restriction line, the content must be empty. But I want a string value in it ("eric").

What should be the XSD code ?

PS: I'd like to avoid the ugly 'mixed="true"'

Eric H.
  • 2,152
  • 4
  • 22
  • 34
  • Have a look at [this SO article](http://stackoverflow.com/questions/376582/xml-schema-element-with-attributes-containing-only-text), which addresses your question. – Tim Biegeleisen Mar 19 '15 at 14:50

1 Answers1

1

You need complex type with simple content and an attribute.

<xs:element name="name">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="sex" type="xs:string"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>
lexicore
  • 42,748
  • 17
  • 132
  • 221