设置属性为所有类型的XML模式 [英] Set attribute to all types in XML Schema

查看:122
本文介绍了设置属性为所有类型的XML模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我存储在 XML 文档我的脚本的设置。剧本是多平台,但它的一些设置都没有。我在 XSD 文件中创建适当的属性。它决定哪个系统的设置。

I store settings of my script in XML document. The script is multiplatform but some of its settings are not. I created proper attribute in my XSD file. It decides to which system is the setting.

<xs:attribute name="system" use="optional" default="none">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="none"/>
            <xs:enumeration value="windows"/>
            <xs:enumeration value="unix"/>
        </xs:restriction>
    </xs:simpleType>
</xs:attribute>

一个样本 XML 文件看起来像下面。 (不要看太多小心,这只是一个例子。)

A sample XML file looks like below. (Don't read too carefully. It's just an example.)

<root>
    <name>Foo</name>
    <path system="windows">%appdata%\Bar</path>
    <path system="unix">~/Bar/</path>
    <foo system="unix">
        <bar>baz</bar>
        <!-- more elements -->
    </foo>
    <foo>
        <bar system="windows">baz</bar>
        <!-- more elements -->
    </foo>
</root>

问题是我需要的可能性属性系统添加到我的 XML 的每个元素。在这个时刻,我不能简单地写:

The problem is I need possibility to add attribute system to each element of my XML. At this moment I can't simply write:

<xs:element name="path" type="xs:string"/>

相反,我必须写:

Instead that I have to write:

<xs:element name="path">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute ref="system"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>

(我知道我能之前定义复杂的类型和多次使用)。这是非常扩展 XSD 和我有很多我自己的类型吧。他们中许多人只在一个地方使用。其实我在 XSD 设置属性系统每个类型,但它需要大量的空间,是不是很可读。

(I know that I can define complex type before and use it multiple times.) It is very expanded XSD and I have a lot of my own types in it. Many of them are used in one place only. Actually I set attribute system to each type in my XSD but it takes a lot of space and is not very readable.

是否有可能属性设置为所有的类型 XSD

Is it possible to set attribute to all types in XSD?

推荐答案

您对全局定义一个共同的属性,在许多地方被使用的选项包括:

Your options for globally defining a common attribute to be used in many locations include:


  • 拥有各类从一个共同的类型包括属性派生。

  • 使用 XS:属性/ @ REF ,你已经提到

  • 使用的xs:attributeGroup / @ REF ,这样可以让你引用多个
    常用集体声明的属性。 更新:在XSD 1.1,一个默认属性
    组可以通过 defaultAttributes XS指定
    :架构适用于所有元素
    复杂类型定义。的感谢@sergioFC这个好主意。

  • 使用 XS:任何元素,然后(在XSD 1.1) XS:断言有关属性,但
    那么你就失去了最普通的元素和属性声明
    由于 XS设施:任何的内在心慈手软

  • Have all types derive from a common type that includes the attribute.
  • Use xs:attribute/@ref, which you've already mentioned.
  • Use xs:attributeGroup/@ref, which would allow you to reference multiple commonly declared attributes collectively. Update: In XSD 1.1, a default attribute group can be specified via defaultAttributes on the xs:schema element that applies to all complex type definitions. Thanks to @sergioFC for this good idea.
  • Use xs:any element, and then (in XSD 1.1) xs:assert about the attribute, but then you lose most of the normal element and attribute declaration facilities due to xs:any's intrinsic leniency.

除了这种间接的选择,没有在XSD没有机制,用于集中说明一个属性可能/必须在所有元素present。

Other than such indirect options, there is no mechanism in XSD for centrally stating that an attribute may/must be present on all elements.

这篇关于设置属性为所有类型的XML模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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