2012-04-10 97 views
0

我有困难的声明包含一个文本字符串,并有两个文本属性的元素:如果我把属性Setting元素内容属性

<?xml version="1.0" encoding="UTF-8"?> 
<!-- This schema is not valid --> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xs:element name="Favorite" type="xs:string"> 
     <xs:complexType> 
      <xs:attribute name="car" type="xs:string"/> 
      <xs:attribute name="fruit" type="xs:string"/> 
     </xs:complexType> 
    </xs:element>  
</xs:schema> 

架构通过,像这样:

<?xml version="1.0" encoding="UTF-8"?> 
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
     <xs:element name="Favorite" type="xs:string"> 
     </xs:element> 
    </xs:schema> 

或者,如果我省略<xs:element name="Favorite" type="xs:string">type="xs:string"

所需要的模式应该是验证下面的XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<Favorite car="Volvo" fruit="banana">These are a few of my favorite things</Favorite> 

PS:我很抱歉,如果这个问题是相当琐碎。我仍然是XSD的初学者。

回答

2

找到了答案,从w3schools

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="Favorite"> 
     <xs:complexType> 

      <xs:simpleContent> 
       <xs:extension base="xs:string"> 
        <xs:attribute name="car" type="xs:string"/>     
        <xs:attribute name="fruit" type="xs:string"/> 
       </xs:extension> 
      </xs:simpleContent> 

     </xs:complexType> 
    </xs:element> 
</xs:schema>