在创建XSD时,simpleType的正确语法是什么?

时间:2011-08-11 16:41:59

标签: xml xsd

这是正确的语法还是我需要将第一个简单类型更改为复杂?

<xs:simpleType name="t_name">
       <xs:attribute name="a_name" >
          <xs:simpleType>
             <xs:restriction base="xs:hexBinary" >
                <xs:length value="4”/>
             </xs:restriction>
          </xs:simpleType>
       </xs:attribute>
</xs:simpleType>

1 个答案:

答案 0 :(得分:1)

是的,这应该是复杂类型

前提是xs定义为<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

来自W3Schools的文章“What is a Complex Element?

  

复杂元素是包含其他元素的XML元素   和/或属性。

     

有四种复杂的元素:

     
      
  • 空元素
  •   
  • 仅包含其他元素的元素
  •   
  • 仅包含文字的元素
  •   
  • 包含其他元素和文本的元素注意:每个元素   这些元素也可能包含属性!
  •   

因此,由于属性<xs:attribute name="a_name" >,您需要将其设为复杂类型

如果您将以下内容另存为文件并对其进行了验证here

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="t_name">
       <xs:attribute name="a_name" >
          <xs:simpleType>
             <xs:restriction base="xs:hexBinary" >
                <xs:length value="4"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:attribute>
     </xs:simpleType>
</xs:schema>

您将获得以下

  

每个cvc-complex-type.1.2.4:元素无效   {http://www.w3.org/2001/XMLSchema}   :元素中不允许此属性(1)   {http://www.w3.org/2001/XMLSchema}:simpleType,期待   [{http://www.w3.org/2001/XMLSchema}:annotation,{http://www.w3.org/2001/XMLSchema}:restriction,{http://www.w3.org/2001/的XMLSchema}:列表中,{} http://www.w3.org/2001/XMLSchema:联合]: