带有来自其他命名空间的元素的 XSD [英] XSD with elements from other namespace

查看:29
本文介绍了带有来自其他命名空间的元素的 XSD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个定义不同文档的 XSD.假设 A.xsd 定义了一个元素 ElementA 作为根元素,有一些复杂的规则.现在 B.xsd 定义了一个元素 ElementB,它应该在中间的某处使用 ElementA.

I have two XSDs that are definining different documents. Say A.xsd defines an element ElementA as the root, with some complex rules. Now B.xsd defines an element ElementB that is supposed to use ElementA somewhere in between.

例如,我希望 ElementB 的 XML 文件如下所示:

For example I want the XML file for ElementB look like this:

<?xml version="1.0" encoding="utf-8"?>
<ElementB xmlns="http://example.com/namespace/for/ElementB">
  <foo>Bla</foo>
  <bar>Blub</bar>
  <ElementA xmlns="http://example.com/namespace/for/ElementA">
    <!-- ... -->
  </ElementA>
</ElementB>

然后 B.xsd 可能看起来像这样:

Then B.xsd could look like this:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="http://example.com/namespace/for/ElementB" targetNamespace="http://example.com/namespace/for/ElementB" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="ElementB">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="foo" type="xs:string" />
        <xs:element name="bar" type="xs:string" />

        <!-- And now I want to include ElementA somehow -->
        <xs:element name="ElementA" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

问题是我真的不想将 ElementA 的规范克隆到 B.xsd 中,因为还有文档,只有 ElementA 作为根(即 ElementB 是某种容器文档).

The thing is that I don‘t really want to clone the specification of ElementA into B.xsd, as there are also documents, that just have ElementA as the root (i.e. ElementB is some kind of container document).

那么,我怎样才能在 ElementB 中允许 ElementA 而完全建立在已经存在的 XSD 之上?

So, how can I allow ElementA within ElementB while completely building on top of the already existent XSD?

推荐答案

实际上有两种不同的方式来组合 XML Schema 文档:.xs:include 旨在在包含文档的命名空间与被引用的命名空间相同时使用,因此它不是您要查找的内容.当您需要引用被引用架构中的所有(或子集)元素并且它们位于不同的目标命名空间中时,xs:import 更适合您的情况.这里有一个关于差异的问题:xsd:include 和 xsd 之间的区别是什么:导入?.

There are actually two different ways to compose XML Schema documents: <xs:import> and <xs:include>. xs:include is intended to be used when the namespace of the containing document is the same as the one being referenced, so it's not quite what you're looking for. xs:import is better for your situation when you need to reference all (or a subset) of elements in the referenced schema and they're in a different target namespace. There's a question here on the differences: What's the difference between xsd:include and xsd:import?.

无论如何,回到这个具体问题.你可能想要的是这样的:

Anyway, back to this specific question. What you probably want is something like this:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema 
    xmlns="http://example.com/namespace/for/ElementB"
    targetNamespace="http://example.com/namespace/for/ElementB"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"
    xmlns:ea="http://example.com/namespace/for/ElementA">
 <xs:import namespace="http://example.com/namespace/for/ElementA" schemaLocation="A.xsd" /> 
 <xs:element name="ElementB">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="foo" type="xs:string" />
    <xs:element name="bar" type="xs:string" />
    <!-- This introduces a element named ElementA that uses the ComplexType ea:ElementA defined in A.xsd -->
    <xs:element name="ElementA" type="ea:ElementA" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>

尽管您需要 A.xsd 来为 ElementA 创建一个复杂类型,您可以在 B.xsd 中使用该类型,如图所示.

Though you'll need A.xsd to create a complex type for ElementA that you can use in B.xsd as shown.

这篇文章有一些很好的信息/例子,包括对一些不同组合策略的讨论:http://www.xfront.com/ZeroOneOrManyNamespaces.html

This arcticle has some good information/examples and includes a discussion of some of the different composability strategies: http://www.xfront.com/ZeroOneOrManyNamespaces.html

这篇关于带有来自其他命名空间的元素的 XSD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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