xml 带有来自其他命名空间的元素的 XSD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6413145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XSD with elements from other namespace
提问by poke
I have two XSDs that are definining different documents. Say A.xsddefines an element ElementAas the root, with some complex rules. Now B.xsddefines an element ElementBthat is supposed to use ElementAsomewhere in between.
我有两个定义不同文档的 XSD。SayA.xsd将元素定义ElementA为根,并具有一些复杂的规则。现在B.xsd定义一个ElementB应该ElementA在两者之间使用的元素。
For example I want the XML file for ElementBlook like this:
例如,我希望 XML 文件ElementB如下所示:
<?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>
Then B.xsdcould look like this:
然后B.xsd看起来像这样:
<?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>
The thing is that I don‘t really want to clone the specification of ElementAinto B.xsd, as there are also documents, that just have ElementAas the root (i.e. ElementBis some kind of container document).
问题是我真的不想克隆ElementAinto的规范B.xsd,因为还有文档,只是ElementA作为根(即ElementB某种容器文档)。
So, how can I allow ElementAwithin ElementBwhile completely building on top of the already existent XSD?
那么,ElementA在ElementB完全建立在已经存在的 XSD 之上的同时,我怎样才能允许呢?
回答by daveaglick
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?.
实际上有两种不同的方式来组合 XML Schema 文档:<xs:import>和<xs:include>. xs:include 旨在在包含文档的命名空间与被引用的命名空间相同时使用,因此它不是您要查找的内容。当您需要引用被引用架构中的所有(或子集)元素并且它们位于不同的目标命名空间中时,xs:import 更适合您的情况。这里有一个关于差异的问题:xsd:include 和 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>
Though you'll need A.xsd to create a complex type for ElementA that you can use in B.xsd as shown.
尽管您需要 A.xsd 来为 ElementA 创建一个复杂类型,您可以在 B.xsd 中使用它,如图所示。
This arcticle has some good information/examples and includes a discussion of some of the different composability strategies: http://www.xfront.com/ZeroOneOrManyNamespaces.html
这篇文章有一些很好的信息/例子,包括对一些不同的可组合性策略的讨论:http: //www.xfront.com/ZeroOneOrManyNamespaces.html
回答by Alexander Yezutov
You could use <xsd:import>tag to import a schema with another namespace.
您可以使用<xsd:import>标记导入具有另一个命名空间的架构。

