声明具有xml架构类型的元素

时间:2013-08-06 11:45:44

标签: xsd xml-validation

如何将xsd:element类型设置为XMLSchema(定义XSD文件的结构)?它甚至可能吗?

例如,我需要一个XML文件,在其根元素下列出多个XSD:

<schemas xmlns:xs=''>
  <xs:schema...>
    <xs:element name='...'/>
  </xs:schema>
  <xs:schema...>
  </xs:schema>
</schemas>

此XML的架构如下所示:

<xs:schema xmlns:xs=''>
  <xs:element name='schemas'>
    <xs:complexType>
      <xs:sequence>
        <xs:element name='schema' type='xs:schema' 
            minoccurs='0' maxoccurs='unbounded'/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

当然,没有xs:schema这样的类型。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:3)

是的,很有可能。方法如下:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <!-- 
    Any XML schema processor does know XML schema language,
    but it is not supposed to know an XML schema for it.
    You need to import it!
  -->
  <xs:import namespace="http://www.w3.org/2001/XMLSchema"
             schemaLocation="http://www.w3.org/2001/XMLSchema.xsd"/>

  <xs:element name="schemas">
    <xs:complexType>
      <xs:sequence>
        <!-- 
          You don't need an 'xs:schema' type. Rather you just need
          to reference an already existing 'xs:schema' element
        -->
        <xs:element minoccurs="0" maxoccurs="unbounded" ref="xs:schema"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

您还可以查看XSLT的XML架构: http://www.w3.org/2007/schema-for-xslt20.xsd。 他们在<xsl:import-schema>元素的定义中也这样做。