2012-01-10 57 views
8

考虑:如何在XML模式中声明仅具有属性的元素?

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

,但将允许元素有文本内容,会吗?我不想说......

回答

15

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

我想XS:扩展名是允许元素具有属性... – Mirko 2012-01-11 13:07:30

+0

@Mirko - 在您的示例中,您必须使用'xs:extension'来扩展'xs:simpleContent','xs:simpleContent'是允许文本在您的元素中的内容。 – 2012-01-11 15:10:11

0

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

<product prodid="1345" /> 

的“产品“元素完全没有内容。要定义,没有内容的类型,我们必须定义一个类型,允许元素的内容,但我们实际上不声明任何元素,比如:

<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

相关问题