通过扩展/继承一组具有附加属性的共享XML类型来最小化耦合



我想将共享一组XML类型的两个开发团队的关注点解耦。共享类型是在共享XSD中定义的。然而,第二个团队需要在共享XML类型中的大多数字段上添加一组额外的属性,这些字段只与他们的一组需求相关。目前,这些适当属性嵌入到共享XSD中的大多数字段中。

我想将这些属性隔离为一组扩展共享XML类型的XML类型,就像在简单的OO语言中所做的那样。Ayesha Malik的一些想法让我开始使用在面向对象框架中构建XML模式的技术

感谢添加属性。。。为了添加facet,我可以将属性添加到各个字段的complexTypes中。但是,当我试图覆盖其中一个复杂的共享类型中的子元素的类型时,Eclipse中的验证会抱怨

粒子类型不是的粒子的有效限制基础

如果我让各个子元素类型保持不变,它将非常有效。但是,如果我将它们的类型更改为新的派生类型,验证就会失败。这很令人沮丧,因为单个子元素的类型与父元素的类型不同,这就是练习的重点。我想为父类型中的几乎每个字段/子元素添加一组属性,但我看不到任何方法

我隔离了一个示例,该示例演示了您可以使用simpleContent将属性添加到simpleType和complexType中。但是我无法将属性添加到具有complexContent的派生complexType中。例如,在下面的complexType"SearchPamphlet"中,我尝试使用<xs:extension>和<xs:restriction>。我还试着将"基础"设置为"书籍"one_answers"小册子"。所有这些方法都会产生相同的错误。有人有什么建议吗?

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:complexType name="Book">
        <xs:sequence>
            <xs:element name="Title" type="xs:string" />
            <xs:element name="Author" type="xs:string" />
            <xs:element name="ISBN" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Pamphlet">
        <xs:complexContent>
            <xs:restriction base="Book">
                <xs:sequence>
                    <xs:element name="Title" type="xs:string" />
                    <xs:element name="Author" type="xs:string" />
                    <xs:element name="ISBN" type="PamphletISBN" />
                </xs:sequence>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
    <xs:simpleType name="ISBNType">
        <xs:restriction base="xs:string" />
    </xs:simpleType>
    <xs:simpleType name="PamphletISBN">
        <xs:restriction base="ISBNType">
            <xs:maxLength value="5" />
        </xs:restriction>
    </xs:simpleType>
    <xs:complexType name="SearchablePamphlet">
        <xs:complexContent>
            <xs:restriction base="Book">
                <xs:sequence>
                    <xs:element name="Title" type="SearchableString" />
                    <xs:element name="Author" type="SearchableString" />
                    <xs:element name="ISBN" type="SearchablePamphletISBN" />
                </xs:sequence>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="SearchablePamphletISBN">
        <xs:simpleContent>
            <xs:extension base="PamphletISBN">
            <xs:attributeGroup ref="searchableAttributes" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType name="SearchableString">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attributeGroup ref="searchableAttributes" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:attributeGroup name="searchableAttributes">
        <xs:attribute name="caseMatches" type="xs:boolean" />
        <xs:attribute name="spellingMatches" type="xs:boolean" />
        <xs:attribute name="checksum" type="xs:integer" />
    </xs:attributeGroup>
</xs:schema>

下面的XSD与您发布的源代码的意图完全匹配。重点是让您了解XSD中的限制是如何工作的,以及在编写时需要考虑的开销。

为了解释:您需要从"SearcheableBook"开始,而不是已经受到限制的Book,这是一个拥有您所需一切的实体,从中可以通过各种限制进行缩减。

<?xml version="1.0" encoding="UTF-8"?>
<!--XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:complexType name="SearchableBook" abstract="true">
        <xs:sequence>
            <xs:element name="Title" type="SearchableString"/>
            <xs:element name="Author" type="SearchableString"/>
            <xs:element name="ISBN" type="SearchableString"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Book">
        <xs:complexContent>
            <xs:restriction base="SearchableBook">
                <xs:sequence>
                    <xs:element name="Title" type="SimpleTitle"/>
                    <xs:element name="Author" type="SimpleAuthor"/>
                    <xs:element name="ISBN" type="SimpleISBN"/>
                </xs:sequence>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="SimpleTitle">
        <xs:simpleContent>
            <xs:restriction base="SearchableString">
                <xs:attribute name="caseMatches" type="xs:boolean" use="prohibited"/>
                <xs:attribute name="spellingMatches" type="xs:boolean" use="prohibited"/>
                <xs:attribute name="checksum" type="xs:integer" use="prohibited"/>              
            </xs:restriction>
        </xs:simpleContent>     
    </xs:complexType>
    <xs:complexType name="SimpleAuthor">
        <xs:simpleContent>
            <xs:restriction base="SearchableString">
                <xs:attribute name="caseMatches" type="xs:boolean" use="prohibited"/>
                <xs:attribute name="spellingMatches" type="xs:boolean" use="prohibited"/>
                <xs:attribute name="checksum" type="xs:integer" use="prohibited"/>                              
            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType name="SimpleISBN">
        <xs:simpleContent>
            <xs:restriction base="SearchableString">
                <xs:attribute name="caseMatches" type="xs:boolean" use="prohibited"/>
                <xs:attribute name="spellingMatches" type="xs:boolean" use="prohibited"/>
                <xs:attribute name="checksum" type="xs:integer" use="prohibited"/>                              
            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType name="Pamphlet">
        <xs:complexContent>
            <xs:restriction base="SearchableBook">
                <xs:sequence>
                    <xs:element name="Title" type="SimpleTitle"/>
                    <xs:element name="Author" type="SimpleAuthor"/>
                    <xs:element name="ISBN" type="PamphletISBN"/>
                </xs:sequence>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="PamphletISBN">
        <xs:simpleContent>
            <xs:restriction base="SearchableString">
                <xs:maxLength value="5"/>
                <xs:attribute name="caseMatches" type="xs:boolean" use="prohibited"/>
                <xs:attribute name="spellingMatches" type="xs:boolean" use="prohibited"/>
                <xs:attribute name="checksum" type="xs:integer" use="prohibited"/>                              
            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType name="SearchablePamphlet">
        <xs:complexContent>
            <xs:restriction base="SearchableBook">
                <xs:sequence>
                    <xs:element name="Title" type="SearchableString"/>
                    <xs:element name="Author" type="SearchableString"/>
                    <xs:element name="ISBN" type="SearchablePamphletISBN"/>
                </xs:sequence>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="SearchablePamphletISBN">
        <xs:simpleContent>
            <xs:restriction base="SearchableString">
                <xs:maxLength value="5"/>
            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType name="SearchableString">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attributeGroup ref="searchableAttributes"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:attributeGroup name="searchableAttributes">
        <xs:attribute name="caseMatches" type="xs:boolean"/>
        <xs:attribute name="spellingMatches" type="xs:boolean"/>
        <xs:attribute name="checksum" type="xs:integer"/>
    </xs:attributeGroup>
</xs:schema> 

此外,我会尝试用不同的方式来思考这个模型,通过扩展,看看是否可以用我称之为更干净的方式来完成它(我最近看到了一个复杂的限制案例是如何扼杀市场上最昂贵的XSD编辑器的…)

我感谢您的回复。它在语法上起作用。不幸的是,答案只适用于只有派生类型集的示例场景。在多个开发团队在共享类型的公共库中工作的情况下,这是不可扩展的。

例如,在共享类型的标准公司库包含示例类型"Book"、"Pamphlet"one_answers"PomphletIBN的情况下,库XSD文件被标准流程"冻结"。

在这种情况下,搜索团队将无法修改Book类型,您也不会真正希望他们修改。因为其他开发团队有相互竞争的需求。例如,销售团队希望具有"lifeTimeSales"、"topRanking"、"highestRatings等的属性,而生产团队可能希望添加"1monthlyPrinting、"numberInInventory等等的属性。


从昨天开始,我尝试使用元素来解决这个需求。不幸的是,这种方法也不能扩展到单个类型级别之外。

我将要研究的另一种方法是使用替代群,看看这是否开辟了任何途径。如果我找到解决方案,我会发布它。

再次感谢你的建议。

我找到了一个解决方案,可以隔离我所关注的问题。解决方案是使用"Salami Slice"设计模式,并结合替代组

有了一个无所不知且有先见之明的分析师,基础文件可能会预测到非常重要的开发需求,或者如果您公司的标准委员会同意在公司中的开发小组每次提出他们想要添加的另一个元素或属性时更改基础类型XSD文件。但在我职业生涯中的工作经历中,我从未有机会与这样的个人或标准委员会合作。

我发布了三个XSD文件,展示了以下关注点的隔离,以及一个示例XML文件。在发布的代码中,我假设BaseTypes.xsd文件被冻结,并处于更改审查委员会的控制之下。我还假设DerivedTypes.xsd、Products.xsd和Test.xml文件将由本地产品开发组生成

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- XMLInheritance_BaseDataTypes -->
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      targetNamespace="http://www.company.org/XMLInheritance" xmlns:bk="http://www.company.org/XMLInheritance"
      elementFormDefault="qualified" attributeFormDefault="unqualified">
      <xs:element name="Book" type="bk:BookType" />
      <xs:element name="Title" type="xs:string" />
      <xs:element name="Author" type="xs:string" />
      <xs:element name="ISBN" type="bk:ISBNType" />
      <xs:simpleType name="ISBNType">
        <xs:restriction base="xs:string" />
      </xs:simpleType>
      <xs:complexType name="BookType">
        <xs:sequence>
          <xs:element ref="bk:Title" />
          <xs:element ref="bk:Author" />
          <xs:element ref="bk:ISBN" />
        </xs:sequence>
      </xs:complexType>
    </xs:schema>

    ----------
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- XMLInheritance_DerivedDataTypes -->
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      targetNamespace="http://www.company.org/XMLInheritance" xmlns:bk="http://www.company.org/XMLInheritance"
      elementFormDefault="qualified" attributeFormDefault="unqualified">
      <xs:include schemaLocation="XMLInheritance_BaseDataTypes.xsd" />
      <xs:element name="SearchableBook" type="bk:SearchableBookType"
        substitutionGroup="bk:Book" />
      <xs:element name=" SearchableTitle " type="bk:SearchableString"
        substitutionGroup="bk:Title" />
      <xs:element name="SearchableAuthor" type="bk:SearchableString"
        substitutionGroup="bk:Author" />
      <xs:element name="SearchableISBN" type="bk:SearchableISBNType"
        substitutionGroup="bk:ISBN" />
      <xs:attributeGroup name="searchableAttributes">
        <xs:attribute name="caseMatches" type="xs:boolean" />
        <xs:attribute name="spellingMatches" type="xs:boolean" />
        <xs:attribute name="checksum" type="xs:integer" />
      </xs:attributeGroup>
      <xs:complexType name="SearchableBookType">
        <xs:complexContent>
          <xs:restriction base="bk:BookType">
            <xs:sequence>
              <xs:element ref="bk:SearchableTitle" />
              <xs:element ref="bk:SearchableAuthor" />
              <xs:element ref="bk:SearchableISBN" />
            </xs:sequence>
          </xs:restriction>
        </xs:complexContent>
      </xs:complexType>
      <xs:complexType name="SearchableISBNType">
        <xs:simpleContent>
          <xs:extension base="bk:ISBNType">
            <xs:attributeGroup ref="bk:searchableAttributes" />
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
      <xs:complexType name="SearchableString">
        <xs:simpleContent>
          <xs:extension base="xs:string">
            <xs:attributeGroup ref="bk:searchableAttributes" />
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
    </xs:schema>

    ----------
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- XMLInheritance_DerivedProducts.xsd -->
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      targetNamespace="http://www.company.org/XMLInheritance" xmlns:bk="http://www.company.org/XMLInheritance"
      elementFormDefault="qualified">
      <xs:include schemaLocation="XMLInheritance_DataTypes.xsd" />
      <xs:element name="Product">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Books">
              <xs:complexType>
                <xs:sequence maxOccurs="unbounded">
                  <xs:element name="Book" type="bk:BookType" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
            <xs:element name="SearchableBooks" minOccurs="0">
              <xs:complexType>
                <xs:sequence maxOccurs="unbounded">
                  <xs:element name="SearchableBook" type="bk:SearchableBookType" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>

    ----------
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- XMLInheritance_Test.xml -->
    <bk:Product xmlns:bk="http://www.company.org/XMLInheritance"
      xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
      xs:schemaLocation="http://www.company.org/XMLInheritance XMLInheritance_DerivedProducts.xsd ">
      <bk:Books>
        <bk:Book>
          <bk:Title>Title</bk:Title>
          <bk:Author>Author</bk:Author>
          <bk:ISBN>ISBN</bk:ISBN>
        </bk:Book>
      </bk:Books>
      <bk:SearchableBooks>
        <bk:SearchableBook>
          <bk:SearchableTitle>Searchable Title</bk:SearchableTitle>
          <bk:SearchableAuthor>Searchable Author</bk:SearchableAuthor>
          <bk:SearchableISBN>Searchable ISBN</bk:SearchableISBN>
        </bk:SearchableBook>
      </bk:SearchableBooks>
    </bk:Product>

最新更新