【问题标题】:How to declare an element with only attributes in a XML schema?如何在 XML 模式中声明仅具有属性的元素?
【发布时间】:2012-02-07 05:00:38
【问题描述】:

给定:

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

但它会允许元素有文本内容,对吗?我不想那样...

【问题讨论】:

    标签: xml xsd schema


    【解决方案1】:

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

    <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

    【讨论】:

      【解决方案2】:

      您可以删除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>
      

      【讨论】:

      • @Mirko - 在您的示例中,您必须使用 xs:extension 来扩展 xs:simpleContentxs:simpleContent 是允许文本出现在您的元素中的原因。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-16
      • 2011-09-18
      相关资源
      最近更新 更多