繁体   English   中英

XML Schema中的属性/元素共现约束

[英]Attribute/element co-occurrence constraint in XML Schema

是否可以创建一个XML Schema,它对属性/元素对强加共生约束?

<primitive-list>
    <primitive name="P1">
        <definition><!-- primitive specification --></definition>
    </primitive>
    <primitive name="P2">
        <definition><!-- primitive specification --></definition>
    </primitive>

    <!-- other common primitives are specified here-->

<primitive-list>

<composite-list>
    <composite name="C1">
        <primitive ref="P1" />
        <primitive ref="P2" />
        <primitive>
            <definition><!-- inline primitive specification --></definition>
        </primitive>        
    </composite>

    <!-- Other compisites are specified here-->

</composite-list>

架构应该意味着:

  • 如果原始的列表元素中指定了原始的元素,那么它应该包含name属性和嵌入式定义元素,但不是ref属性。
  • 如果在复合元素中指定了原始元素,那么它应该包含ref属性或definition元素。 两种情况都不允许使用该名称

我很确定它是可能的,因为XML Schema中的元素元素本身就像那样。 所以任何拥有这种神圣知识的人请分享:-)

先感谢您。

在互联网上搜索并挖掘了一些书后,我想出了如何实现它。

首先,我们需要定义一个泛型类型,它适应两种原始元素的所有属性和元素。 假设定义元素在其他地方定义。

<xs:complexType name="primitive" abstract="true">
    <xs:sequence>
        <xs:element ref="definition" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
    <xs:attribute name="name" type="xs:Name" />
    <xs:attribute name="ref" type="xs:Name" />
</xs:complexType>

然后我们定义两个原始子类型分别用于原始列表复合

<xs:complexType name="public-primitive">
    <xs:complexContent>
        <xs:restriction base="primitive">
            <xs:sequence>
                <xs:element ref="definition" minOccurs="1" maxOccurs="unbounded" />
            </xs:sequence>
            <xs:attribute name="name" type="xs:Name" use="required" />
            <xs:attribute name="ref" use="prohibited" />
        </xs:restriction>
    </xs:complexContent>
</xs:complexType>

<xs:complexType name="private-primitive">
    <xs:complexContent>
        <xs:restriction base="primitive">
            <xs:sequence>
                <xs:element ref="definition" minOccurs="0" maxOccurs="unbounded" />
            </xs:sequence>
            <xs:attribute name="name" use="prohibited" />
        </xs:restriction>
    </xs:complexContent>
</xs:complexType>

现在我们可以根据这些复杂类型定义基本列表复合元素,如下所示:

<xs:element name="primitive-list">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="primitive" type="public-primitive" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="composite">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="primitive" type="private-primitive" maxOccurs="unbounded">
                <xs:key name="definition-ref--co-occurrence--constraint">
                    <xs:selector xpath="." />
                    <xs:field xpath="definition|@ref" />
                </xs:key>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

我们来看看原始架构要求,看看它们是如何实施的:

  • 如果原始的列表元素中指定了原始的元素,那么它应该包含name属性和嵌入式定义元素,但不是ref属性。

仅通过public-primitive类型的定义来强制执行此要求。

  • 如果在复合元素中指定了原始元素,那么它应该包含ref属性或definition元素。 两种情况都不允许使用该名称

此要求由private-primitive类型的定义和在复合元素内定义的元元素中指定的xs:key元素强制执行。 xs:密钥保证refdefinition存在但不同时存在。

是的,这是可能的。 在创建XML模式时,您将根据XML树中定义元素的位置为每个方案创建复杂类型。

如果我过了一会儿,我可以尝试在这里为你做一个例子,我只是没有时间把这一切都完美地发布在这里。

我个人强烈建议您查看这个w3schools教程 ,因为它应该能满足您的需求。

好的,这是一个示例,这会让你关闭,唯一没有处理的是复合上的primitive和ref属性。 看看我能找到什么,通过模式几乎不可能做到这一点。 我不是100%肯定,但在我看到这一点的所有情况下,这样的模式用于高级验证,然后使用流程代码验证各个项目。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://www.iowacomputergurus.com/stackoverflow/samples/xsdexample"
    elementFormDefault="qualified"
    xmlns="http://www.iowacomputergurus.com/stackoverflow/samples/xsdexample"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>

  <xs:complexType name="PrimitiveType">
    <xs:sequence>
      <xs:element name="definition" type="xs:string" minOccurs="1" maxOccurs="1" />
    </xs:sequence>
    <xs:attribute name ="name" use="required" type="xs:string" />
  </xs:complexType>

  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
          <xs:element name="primitive-list" minOccurs="1" maxOccurs="1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="primitive" type="PrimitiveType" minOccurs="1" maxOccurs="unbounded" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="composite-list" minOccurs="1" maxOccurs="1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="composite">
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="primitive" minOccurs="1" maxOccurs="unbounded">
                        <xs:complexType>
                          <xs:sequence>
                            <xs:element name="definition" minOccurs="0" maxOccurs="1" />
                          </xs:sequence>
                          <xs:attribute name="ref" use="optional" type="xs:string" />
                        </xs:complexType>
                      </xs:element>
                    </xs:sequence>
                    <xs:attribute name="name" type="xs:string" use="required" />
                  </xs:complexType>
                </xs:element>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>  
</xs:schema>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM