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

2023-11-25

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

原始 XML:

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

New 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>

Your original模式应该声明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>

假设上面的架构是您的original架构,你可以redefine the 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 文件。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在不同的命名空间中扩展复杂类型而不更改名称 的相关文章

随机推荐