简体   繁体   English

XML模式:限制来自另一个名称空间的元素

[英]XML Schema: restricting an element from another namespace

I'm writing an XML Schema for a document type that includes tags from other namespaces. 我正在为包含来自其他命名空间的标签的文档类型编写XML模式。 (Atom and GML, to be specific: well-known standards.) Requiring a particular tag from another namespace seems easy enough: (具体来说,是Atom和GML:众所周知的标准。)从另一个名称空间中请求特定标签似乎很容易:

<xs:import namespace="http://www.w3.org/2005/Atom" schemaLocation="atom.xsd" />
<!-- and then, within a complex type definition: -->
<xs:element ref="atom:link" />

But in several instances, I need to restrict the attributes on this tag. 但是在某些情况下,我需要限制此标记的属性。 For example, I need to be able to specify the value of rel attribute of the atom:link tag. 例如,我需要能够指定atom:link标记的rel属性值。 That is, I need to assert that a tag in the namespace I'm writing the schema for contain <atom:link rel="self" href="..." /> and not <atom:link rel="somethingelse" /> . 也就是说,我需要断言我正在编写模式的名称空间中的标记包含<atom:link rel="self" href="..." />而不是<atom:link rel="somethingelse" />

I know how to do this with an <xs:attribute /> tag on a complexType I'm defining myself, but I don't know how to do this on a tag which lives within another namespace. 我知道如何在我要定义的complexType上使用<xs:attribute />标记来执行此操作,但是我不知道如何在另一个名称空间中的标记上执行此操作。 I can edit atom.xsd if necessary, but note that in different places in my schema I need to assert different rel values, so I can't just add a single rel restriction on every occurrence of atom:link . 如有必要,我可以编辑atom.xsd ,但是请注意,在架构的不同位置,我需要声明不同的rel值,因此我不能只对每次atom:link添加一个rel限制。

Is this doable? 这可行吗? Thanks! 谢谢!

Yes, it's a problem. 是的,这是一个问题。 To solve this problem XSD 1.1 allows targetNamespace to be specified on local element and attribute declarations (but only when you are refining a type in that namespace). 为了解决此问题,XSD 1.1允许在本地元素和属性声明中指定targetNamespace(但仅当您在该命名空间中优化类型时才可以指定)。 If you don't have access to an XSD 1.1 processor, your only solution is to "hijack" the original namespace - define a schema document whose target namespace is the same as the namespace you are defining a restriction of, and put your restricted types in that namespace. 如果您无权使用XSD 1.1处理器,则唯一的解决方案是“劫持”原始名称空间-定义其目标名称空间与您要定义限制的名称空间相同的纲要文档,并放入限制类型在那个名称空间中。 Your element declarations, of course, can still go in your own namespace. 当然,您的元素声明仍然可以放在您自己的名称空间中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM