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

查看:23
本文介绍了我可以创建一个 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天全站免登陆