Xml 是否可以为XSD模式创建XSD模式,或者对元素定义进行限制?怎么用?

Xml 是否可以为XSD模式创建XSD模式,或者对元素定义进行限制?怎么用?,xml,xsd,Xml,Xsd,我的目标是对XSD元素定义进行一个简单的限制:我想定义一个元素,以便 所有元素定义都需要 “声明的”和“上次修改的”属性添加到中,“注释作者”和“注释添加的”属性添加到元素中 有了下面的内容,我可以让模式2进行验证,但是我无法执行我上面提到的规则,我想在模式1中声明这些规则 我希望通过修改、验证的XSD模式(模式2)实现的示例: 方案2 <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.

我的目标是对XSD元素定义进行一个简单的限制:我想定义一个元素,以便

  • 所有元素定义都需要
  • “声明的”和“上次修改的”属性添加到
    中,“注释作者”和“注释添加的”属性添加到
    元素中
  • 有了下面的内容,我可以让模式2进行验证,但是我无法执行我上面提到的规则,我想在模式1中声明这些规则

    我希望通过修改、验证的XSD模式(模式2)实现的示例:

    方案2

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
               xmlns:ds="http://documentation_schema">
    
      <xs:import namespace="http://documentation_schema" 
                 schemaLocation="documentation_schema.xsd"/>
    
      <xs:element name="document" 
                  ds:created="2013-06-20" 
                  ds:last_modified="2013-06-20">
    
        <xs:annotation>
          <xs:documentation ds:note_author="xsd_user1" 
                            ds:note_added="2013-06-20">
          The root element for a document
          </xs:documentation>
        </xs:annotation>
    
        <xs:complexType>
          <xs:sequence maxOccurs="unbounded">
            <xs:element ref="subelement"/>
          </xs:sequence>    
        </xs:complexType>
      </xs:element>
    
      <xs:element name="subelement" type="xs:string"  
                  ds:created="2013-06-20" 
                  ds:last_modified="2013-07-20">
    
        <xs:annotation>
          <xs:documentation ds:note_author="xsd_user1" 
                            ds:note_added="2013-06-20">
          A subelement child of document
          </xs:documentation>
    
          <xs:documentation ds:note_author="xsd_user2" 
                            ds:note_added="2013-07-20">
          changed from complex to string type
          </xs:documentation>
        </xs:annotation>`
      </xs:element>
    
    </xs:schema>
    
    
    文档的根元素
    文档的子元素
    从复杂类型更改为字符串类型
    `
    
    模式1(documentation_Schema)——我要验证模式2的模式:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
               targetNamespace="http://documentation_schema">
    
      <xs:attribute name="created" type="xs:date"/>
    
      <xs:attribute name="last_modified" type="xs:date"/>
    
      <xs:attribute name="note_added" type="xs:date"/>
    
      <xs:attribute name="note_author">
        <xs:simpleType>
          <xs:restriction base="xs:token">
            <xs:enumeration value="xsd_user1"/>
            <xs:enumeration value="xsd_user2"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
    
    </xs:schema>
    
    
    
    问得好

    应当指出几点:

    • 首先,正如我猜您已经知道的(尽管一些问题和答案的读者可能不知道),您的模式文档1已经是合法的了。因此,您不需要修改模式文档的模式以使其合法。但是,如果我理解正确,你希望它不仅仅是有效的;您希望在没有xs:documentation元素或没有ds:added和ds:last_modified属性的情况下进行元素声明的变体无效

    • 其次,可以按照您描述的方式修改XSD模式文档的XSD模式,并且原则上可以根据模式文档的修改模式验证您的模式文档

      但是,模式验证器允许具有任意名称空间中某些模式组件的内置知识,并且需要具有XSD名称空间的内置知识。因此,一致性模式验证器可能会看到验证模式文档1的请求,比如“啊哈,我已经知道关于名称空间的所有我需要知道的,我不需要阅读用户提供的模式文档”,然后不强制执行您指定的附加约束

      具体地说,在我的测试中,我能够使用Xerces J根据模式文档的修改模式验证模式文档,但Saxon似乎忽略了修改,并规定模式文档有效,即使元素声明缺少所需的属性

    • 类似地,在根据您编写的模式验证文档时,不可能要求您的XSD验证器反对不满足您的高要求的模式文档

    有了这些背景知识,下面是你需要做的

  • 您不需要将ds架构导入到架构文档1中。导入它意味着您可以将对属性ds:added等的引用添加到模式文档1中声明的类型中——也就是说,它将其导入为目标命名空间定义的模式中。它对用于验证模式文档的模式(即命名空间的模式)没有影响

  • 您需要创建一个模式来修改模式文档的标准模式,而不仅仅是通过添加新组件,而是通过重新定义或覆盖模式文档的现有模式中的组件。有两种方法可以做到这一点:使用xs:redefine和(在xsd1.1中)使用xs:override。在XSD 1.1中,xs:redefinite被弃用,因为它的规范已经被证明是不完整和/或不一致的,并且对于某些使用它的方法来说,处理器之间的互操作性很差

  • 您需要使用XSD验证器根据修改后的模式文档验证模式文档。如上所述,这可能不适用于所有验证器。如果您无法使XSD验证器处理该情况,您可能需要考虑编写SchemaTRON架构来强制执行您所提到的几个约束,并将SchemaTRON验证建立到工作流程中。(Schematron的优点之一是只验证文档的几个孤立方面非常容易。在某些情况下,这可能是一个缺点,但在这种情况下,或者在其他情况下,不希望增加验证量。如果您的用户名表明您是XSLT用户,Schematron可能会感到非常困难对你来说很自然。)

  • 作为测试,我使用xs:override修改XSD1.1模式文档的模式;模式文档如下所示,我希望这些注释能够合理地说明正在发生的事情。如果您只能访问XSD1.0处理器,则需要使用xs:redefinite;一些细节将会改变

    <?xml version='1.0'?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
               xmlns:ds="http://documentation_schema"
               blockDefault="#all"
               elementFormDefault="qualified" 
               xml:lang="en"
               targetNamespace="http://www.w3.org/2001/XMLSchema"
               version="Id: structures.xsd,v 1.2 2004/01/15 11:34:25 ht Exp ">
    
      <xs:annotation>
        <xs:documentation>
          This schema document redefines some things in the schema for 
          schema documents.  
          First we import the ds and xml namespaces.
        </xs:documentation>
      </xs:annotation>
    
      <xs:import namespace="http://www.w3.org/XML/1998/namespace"/>
      <xs:import namespace="http://documentation_schema"
                 schemaLocation="xslt_user_schema2.xsd">
        <xs:annotation>
          <xs:documentation>
            Get access to the ds:* attributes.
          </xs:documentation>
        </xs:annotation>
      </xs:import>
    
      <xs:annotation>
        <xs:documentation>
          Then we include the schema for schema documents defines in XSD 1.1, with
          some modifications.  
        </xs:documentation>
      </xs:annotation>
    
      <xs:override schemaLocation="sfsd-11.xsd">
        <xs:annotation>
          <xs:documentation>
            The schema document "sfsd-11.xsd" is our local copy of the 
            XSD 1.1 schema for schema documents.  
          </xs:documentation>
        </xs:annotation>
    
        <xs:element name="documentation" id="documentation" 
                    type="xs:documentation-extended">
          <xs:annotation>
            <xs:documentation>
              Here, we change the xs:documentation element from using
              a local complex type to using a named type (declared below).  
            </xs:documentation>
          </xs:annotation>
        </xs:element>
    
        <xs:element name="annotation" id="annotation">
          <xs:annotation>
            <xs:documentation>
              Next, we require xs:annotation elements to contain 
              at least one xs:documentation element.
            </xs:documentation>
          </xs:annotation>
    
          <xs:complexType>
            <xs:complexContent>
              <xs:extension base="xs:openAttrs">
                <!--* modified the content model to require at least one 
                    * occurrence of xs:documentation 
                    *-->
                <xs:sequence>
                  <xs:element ref="xs:appinfo" minOccurs="0" maxOccurs="unbounded"/>
                  <xs:element ref="xs:documentation"/>
                  <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element ref="xs:appinfo"/>
                    <xs:element ref="xs:documentation"/>
                  </xs:choice>
                </xs:sequence>            
                <xs:attribute name="id" type="xs:ID"/>
              </xs:extension>
            </xs:complexContent>
          </xs:complexType>
        </xs:element>
    
        <!--* Finally, we override the complex type used for all forms 
            * of element declarations, by including the attribute group
            * xs:schema-documentation-attributes (defined below).
            * Note that we cannot include references to the attributes
            * directly, because the default schema for schema documents
            * doesn't import the ds namespace.  One level of indirection,
            * however, suffices to solve the problem.
            *-->
    
        <xs:complexType name="element" abstract="true">
          <xs:annotation>
            <xs:documentation>
              The element element can be used either
              at the top level to define an element-type binding globally,
              or within a content model to either reference a globally-defined
              element or type or declare an element-type binding locally.
            The ref form is not allowed at the top level.</xs:documentation>
            <xs:documentation>
              This modification of the type adds two required attributes:
              ds:added and ds:last_modified.  They will be inherited by
              all the various restrictions of this type.
            </xs:documentation>
          </xs:annotation>
          <xs:complexContent>
            <xs:extension base="xs:annotated">
              <xs:sequence>
                <xs:choice minOccurs="0">
                  <xs:element name="simpleType" type="xs:localSimpleType"/>
                  <xs:element name="complexType" type="xs:localComplexType"/>
                </xs:choice>
                <xs:element name="alternative" type="xs:altType" 
                            minOccurs="0" maxOccurs="unbounded"/>
                <xs:group ref="xs:identityConstraint" minOccurs="0"
                          maxOccurs="unbounded"/>
              </xs:sequence>
              <xs:attributeGroup ref="xs:defRef"/>
              <xs:attribute name="type" type="xs:QName"/>
    
              <xs:attribute name="substitutionGroup">
                <xs:simpleType>
                  <xs:list itemType="xs:QName"/>
                </xs:simpleType>
              </xs:attribute>
              <xs:attributeGroup ref="xs:occurs"/>
              <xs:attribute name="default" type="xs:string"/>
              <xs:attribute name="fixed" type="xs:string"/>
              <xs:attribute name="nillable" type="xs:boolean" use="optional"/>
              <xs:attribute name="abstract" type="xs:boolean" default="false"
                            use="optional"/>
              <xs:attribute name="final" type="xs:derivationSet"/>
              <xs:attribute name="block" type="xs:blockSet"/>
              <xs:attribute name="form" type="xs:formChoice"/>
              <xs:attribute name="targetNamespace" type="xs:anyURI"/>
              <!--* add the ds:* attributes *-->
              <xs:attributeGroup ref="xs:schema-documentation-attributes"/>
              <!--* end of added material. *-->
            </xs:extension>
          </xs:complexContent>
        </xs:complexType>
    
      </xs:override>
    
      <xs:complexType name="documentation-extended" mixed="true">
        <xs:annotation>
          <xs:documentation>
            This complex type is just like the usual type for the 
            xs:documentation element, except that it reuqires the 
            ds:note_author and ds:note_added attributes.
          </xs:documentation>
        </xs:annotation>
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
          <xs:any processContents="lax"/>
        </xs:sequence>
        <xs:attribute name="source" type="xs:anyURI"/>
        <xs:attribute ref="xml:lang"/>
        <!--* additions ... *-->
        <xs:attribute ref="ds:note_author" use="required"/>
        <xs:attribute ref="ds:note_added" use="required"/>
        <!--* end added material *-->
        <xs:anyAttribute namespace="##other" processContents="lax"/>
      </xs:complexType>
    
      <xs:attributeGroup name="schema-documentation-attributes">
        <xs:annotation>
          <xs:documentation>
            This attribute group serves to include the two
            attributes specified in the complex type for
            element declarations.
          </xs:documentation>
        </xs:annotation>
        <xs:attribute ref="ds:created" use="required"/>
        <xs:attribute ref="ds:last_modified" use="required"/>
      </xs:attributeGroup> 
    
    </xs:schema>
    
    
    此架构文档重新定义了架构中的某些内容
    模式文档。
    首先,我们导入ds和xml名称空间。
    访问ds:*属性。
    然后,我们包括XSD1.1中定义的模式文档的模式,其中
    一些修改。
    模式文档“sfsd-11.xsd”是
    模式文档的XSD 1.1模式。
    这里,我们将xs:documentation元素从使用
    使用命名类型(下面声明)创建的本地复杂类型。
    接下来,我们需要xs:annotation元素包含
    至少一个xs:documentation元素。
    
        <!DOCTYPE schema SYSTEM "XMLSchema.dtd"> 
        <xs:schema targetNamespace="w3.org/2001/XMLSchema"
                   blockDefault="#all" 
                   elementFormDefault="qualified" 
                   version="1.0" 
                   xmlns:xs="w3.org/2001/XMLSchema"
                   xml:lang="EN" 
                   xmlns:hfp="w3.org/2001/XMLSchema-hasFacetAndProperty"
                   finalDefault="" 
                   attributeFormDefault="unqualified">