如何在不更改名称的情况下在不同的命名空间中扩展复杂类型

时间:2014-06-04 17:24:37

标签: xml xsd

我有一个我无法改变的现有命名空间。我需要向复杂类型添加元素,以便生成的XML如下所示:

原始XML:

<?xml version="1.0"?>
<Book xmlns="http://bookshop.com">
  <Author>Frank Herbert</Author>
  <Title>Dune</Title>
</Book>

新XML:

<?xml version="1.0"?>
<Book xmlns="http://bookshop.com" 
      xlmns:custom="http://custombookshop.com>
  <Author>Frank Herbert</Author>
  <Title>Dune</Title>
  <custom:Publisher>Ace</custom:Publisher>
</Book>

我要求自定义元素必须如上所示显示名称空间前缀,并且复杂类型名称不得更改。

以下是我尝试使用重新定义的原始XSD和新XSD。这会有效,还是有更好的方法来实现这一目标?提前感谢您的建议。

原创XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns="http://bookshop.com" 
           targetNamespace="bookshop.com">
  <xs:complexType name="Book">
    <xs:complexContent>
      <xs:sequence>
        <xs:element name="Author" type="String32" 
                    minOccurs="1" maxOccurs="1" />
        <xs:element name="Title" type="String32" 
                    minOccurs="1" maxOccurs="1" />
      </xs:sequence>
    </xs:complexContent>
  </xs:complexType>
</xs:schema>

我尝试了新的XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns="http://custombookshop.com" 
           targetNamespace="custombookshop.com">
  <xs:redefine schemaLocation="http://bookshop.com">
    <xs:complexType name="Book">
      <xs:complexContent>
        <xs:extension base="Book">
          <xs:sequence>
            <xs:element name="Publisher" type="String32" 
                        minOccurs="1" maxOccurs="1" />
          </xs:sequence>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:redefine>
</xs:schema>

1 个答案:

答案 0 :(得分:3)

您的原始架构应在sequence内声明complexType

<xs:complexType name="Book">
    <xs:sequence>
        <xs:element name="Author" type="String32" 
            minOccurs="1" maxOccurs="1" />
        <xs:element name="Title" type="String32" 
            minOccurs="1" maxOccurs="1" />
    </xs:sequence>
</xs:complexType>

它还应声明targetNamespace等于默认的xmlns命名空间,并包含属性elementFormDefault="qualified",以便您可以在实例中使用不合格的元素。下面的模式(我添加了element声明和simpleType声明)验证了您的原始XML:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns="http://bookshop.com" 
    targetNamespace="http://bookshop.com"
    elementFormDefault="qualified">

    <xs:complexType name="Book">
        <xs:sequence>
            <xs:element name="Author" type="String32" 
                minOccurs="1" maxOccurs="1" />
            <xs:element name="Title" type="String32" 
                minOccurs="1" maxOccurs="1" />
        </xs:sequence>
    </xs:complexType>

    <xs:element name="Book" type="Book"/>

    <xs:simpleType name="String32">
        <xs:restriction base="xs:string"></xs:restriction>
    </xs:simpleType>
</xs:schema>

假设上面的架构作为原始架构,您可以在具有相同目标命名空间的新架构中重新定义 Book复杂类型作为原始架构。如果需要包含来自另一个名称空间的Publisher元素,则必须在第三个模式中声明它。在与原始架构具有相同目标命名空间的架构中,您可以像这样重新定义Book类型(假设您的原始架构位于bookshop.xsd中:

<xs:redefine schemaLocation="bookshop.xsd">
    <xs:complexType name="Book">
        <xs:complexContent>
            <xs:extension base="Book">
                <xs:sequence>
                    <xs:element ref="cs:Publisher" minOccurs="1" maxOccurs="1" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:redefine>

要使其工作,您必须导入声明Publisher元素的架构。假设它在此模式中使用http://custombookshop.com命名空间声明:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified"
    xmlns="http://custombookshop.com" 
    xmlns:bs="http://bookshop.com" 
    targetNamespace="http://custombookshop.com"> 

    <xs:import namespace="http://bookshop.com" schemaLocation="bookshop.xsd"/>
    <xs:element name="Publisher" type="bs:String32" />

</xs:schema>

然后,您可以将其导入重新定义的架构中:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified"
    xmlns="http://bookshop.com" 
    targetNamespace="http://bookshop.com"
    xmlns:cs="http://custombookshop.com"> 

    <xs:import schemaLocation="custombookshop.xsd" namespace="http://custombookshop.com"/>

    <xs:redefine schemaLocation="bookshop.xsd">
        ...
    </xs:redefine>

</xs:schema>

现在,您可以使用重新定义的架构验证您的第二个XML文件。

相关问题