如何在不更改名称的情况下在不同的命名空间中扩展复杂类型 [英] How to extend a complex type in a different namespace without changing name

查看:28
本文介绍了如何在不更改名称的情况下在不同的命名空间中扩展复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an existing namespace that I cannot change. I need to add elements to a complex type such that the resulting XML looks like this:

原始 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>

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

I have requirements that the custom elements must appear as above with the namespace prefix, and that the complex type name must not change.

这是我尝试使用重新定义的原始 XSD 和新 XSD.这会起作用,还是有更好的方法来实现这一目标?预先感谢您的建议.

Here is the original XSD and the new XSD I've attempted using redefine. Will this work, or is there a better way to accomplish this? Thanks in advance for your advice.

原始 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>

推荐答案

你的原始架构应该在complexType中声明sequence:

Your original schema should declare the sequence inside the 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:

It should also declare the targetNamespace equal to the default xmlns namespace, and include the attribute elementFormDefault="qualified" so you can use unqualified elements in your instance. The schema below (to which I added an element declaration and a simpleType declaration) validates your original 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 中:

Assuming that the schema above as your original schema, you can redefine the Book complex type in a new schema with the same target namespace as your original schema. If it needs to contain a Publisher element from another namespace, you have to declare it in a third schema. In a schema with the same target namespace as your original schema, you can redefine the Book type like this (assuming your original schema is in 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 命名空间:

For that to work you have to import the schema where the Publisher element is declared. Suppose it is declared in this schema, with the http://custombookshop.com namespace:

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

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

You can then import it into your redefined 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 文件.

And now, you can validate your second XML file using the redefined schema.

这篇关于如何在不更改名称的情况下在不同的命名空间中扩展复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆