XSD : 如何同时使用值和属性

XSD : How to use both value and attribute

我想验证具有字符串属性和字符串值的元素:

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

我用过:

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

它说内容无效。

文档说具有属性的元素总是 "complexType"。如果我省略 xs:restriction 行,则内容必须为空。但我想要一个字符串值 ("eric").

XSD 代码应该是什么?

PS:我想避免丑陋的'mixed="true"'

您需要具有简单内容和属性的复杂类型。

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