如何在XML模式中声明仅包含属性的元素?

时间:2012-01-10 19:13:50

标签: xml xsd schema

假设:

<authentication password="turkey" partnerid="exam" />

如何在XML模式中声明此元素?

我有:

<xs:element name="authentication" type="auth_type" />

<xs:complexType name ="auth_type">
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="password" type="xs:string" />
      <xs:attribute name="partnerid" type="xs:string" />
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>

但它会允许元素有文本内容,是吗?我不希望......

2 个答案:

答案 0 :(得分:16)

您可以删除xs:simpleContentxs:extension ....

  <xs:element name="authentication" type="auth_type" />

  <xs:complexType name ="auth_type">
    <xs:attribute name="password" type="xs:string" />
    <xs:attribute name="partnerid" type="xs:string" />
  </xs:complexType>

答案 1 :(得分:0)

你需要的是一个复杂的元素,例如:

<product prodid="1345" />

上面的“product”元素根本没有内容。要定义一个没有内容的类型,我们必须定义一个允许元素在其内容中的类型,但我们实际上并没有声明任何元素,如下所示:

<xs:element name="product">
  <xs:complexType>
    <xs:complexContent>
      <xs:restriction base="xs:integer">
        <xs:attribute name="prodid" type="xs:positiveInteger"/>
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>
</xs:element>

检查http://www.w3schools.com/schema/schema_complex_empty.asp