2012-01-10 56 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

相關問題