我可以创建一个在所有复杂类型上放置属性的XSD架构吗? [英] Can I create an XSD schema that places an attribute on all complex types?

查看:103
本文介绍了我可以创建一个在所有复杂类型上放置属性的XSD架构吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个XSD,该XSD定义一个属性,该属性可以放置在其他模式的元素上,也可以放置在任何模式之外的元素上.例如,该架构将如下所示:

I would like to create an XSD that defines an attribute which can be placed on elements from other schemas, or elements that are not in any schema. For example, the schema would look something like this:

<xs:schema id="MySchema"
    targetNamespace="http://tempuri.org/MySchema"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/MySchema"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:attribute name="myAttribute" />
</xs:schema>

文档可能看起来像这样:

And the document might look something like this:

<someElement xmlns="http://tempuri.org/OtherSchema" xmlns:m="http://tempuri.org/MySchema">
  <someOtherElement someAttribute="value" m:myAttribute="value2" />
</someElement>

此示例的"OtherSchema"如下所示:

"OtherSchema" for this example looks like this:

<xs:schema id="OtherSchema"
    targetNamespace="http://tempuri.org/OtherSchema"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/OtherSchema"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="someElement">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" name="someOtherElement">
          <xs:complexType>
            <xs:attribute name="someAttribute" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

可以从下载完整的示例,包括执行验证的C#控制台应用程序. http://dl.getdropbox.com/u/407740/SchemaTest.zip .我的目标是在无需修改"OtherSchema"的情况下进行验证.这可能吗?

A complete example, including a C# console application which performs validation, can be downloaded from http://dl.getdropbox.com/u/407740/SchemaTest.zip. My goal is to make this validate without having to modify "OtherSchema". Is this possible?

推荐答案

我必须添加一个包装程序,才能将两个不同的架构导入一个(因为xmllint仅接受单个xml架构):

I had to add a wrapper, to import the two different schema into one (because xmllint only accepts a single xml schema):

<xs:schema id="Wrapper" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import schemaLocation="MySchema.xsd" namespace="http://tempuri.org/MySchema"/>
  <xs:import schemaLocation="OtherSchema.xsd" namespace="http://tempuri.org/OtherSchema"/>
</xs:schema>

我能使类似问题"工作的唯一方法是编辑"OtherSchema,xsd"(问题不允许),因此附加一个属性通配符(在现有的通配符之后):

The only way I could get something like the Question to work was to edit OtherSchema,xsd (which is not allowed by the question), so append an attribute wildcard (after the existing one):

 <xs:attribute name="someAttribute" />
 <xs:anyAttribute namespace="##other"/>

我还不是XML Schema专家,只能说这是不可能的",但对我来说似乎是不可能的.

I'm not enough of an expert of XML Schema to say "this is impossible", but it seems impossible to me.

提案的一个问题是,您未指定新属性应出现在何处.通常,如果您声明属性(或complexElement,modelgroup等),则可以自由引用或不引用它.如果您未明确引用它,则它无效.因此,我认为您的提案将被视为已声明但未引用的属性.

One problem with your proposal is that you don't specify where the new attribute should appear. Usually, if you declare an attribute (or a complexElement, modelgroup etc), you are free to refer to it or not. If you don't explicitly refer to it, it has no effect. Therefore, I think your proposal will be treated as an attribute that is declared, but not referred to.

您真正想要的是一种将这个属性添加到每个现有complexType中"的方法-但是您不必这么说.而且,不幸的是,似乎没有这样一种说法. (甚至没有办法说将这个属性添加到这个特定的现有complexType中"-您必须将它包括在原始定义中,或者根本不包括在内.)

What you really want is a way to say "add this attribute to every existing complexType" - but you don't say this. And, unfortunately, there doesn't seem to be a way to say this. (there isn't even a way to say "add this attribute to this specific existing complexType" - you have to include it in the original definition or not at all.)

部分实现 的一种方法是在另一个模式中输入<redefine>类型-我将在第二个答案中添加它.

One way to partly do it is to <redefine> types in another schema - I'll add this in a second answer.

这篇关于我可以创建一个在所有复杂类型上放置属性的XSD架构吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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