如何从另一个 xml-schema 扩展一个 xml-schema? [英] How to extend an xml-schema from another xml-schema?

查看:33
本文介绍了如何从另一个 xml-schema 扩展一个 xml-schema?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于下面的 xml 文档 (resources.xml),带有相应的 xml-schema (resources.xsd).这个 xml 文档是手动维护的(即添加/删除/编辑资源元素).总共可能有 500-1000 个资源元素.每个资源都可以是变体X或变体Y(在现实生活"中,还有更多变体).

I have an xml document that looks something like below (resources.xml), with a corresponding xml-schema (resources.xsd). This xml document is manually maintained (i.e adding/removing/editing resource elements). In total there are maybe 500-1000 resource elements. Each resource can be of either variantX or variantY (in "real life", there are a few more variants).

我想把xml文档拆分成几个xml文档.每个变体(在本例中为 X 和 Y)的一个 xml 文档,以及相应的新 xml 架构.每个变体的 xml 架构应该扩展原始架构,并且只为其变体"属性添加一个硬编码"(固定?)值.

I would like to split up the xml document into several xml documents. One xml document for each variant (X and Y in this case), with a corresponding new xml-schemas. The xml schemas for each variant, should extend the original schema and only add a "hardcoded" (fixed?) value for it's "variant" attribute.

原因:避免在每个资源元素中重复variant"属性.

Reason: To avoid repeating the "variant" attribute within each resource element.

这可能吗?每个变体的 xml 架构是什么样的?需要在resources.xsd 中做哪些更改?

Is this possible? What would the xml-schemas for each variant look like? What changes needs to be done in resources.xsd?

也欢迎任何其他建议:)

Any other suggestions are also welcomed :)

    <?xml version="1.0" encoding="UTF-8"?>
    <resources xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:noNamespaceSchemaLocation="resources.xsd">
        <resource name="res00" variant="X" >
            <property name="propA" value="..." />
            <property name="propB" value="..." />
        </resource>
        <resource name="res01" variant="X" >
            <property name="propA" value="..." />
            <property name="propB" value="..." />
        </resource>
        <resource name="res02" variant="Y" >
            <property name="propA" value="..." />
            <property name="propB" value="..." />
        </resource>
        <resource name="res03" variant="Y" >
            <property name="propA" value="..." />
            <property name="propB" value="..." />
        </resource>
    </resources>

资源.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:rb="http://example.org/resourcebase">

    <xs:complexType name="property">
        <xs:attribute name="name" type="xs:string" />
        <xs:attribute name="value" type="xs:string" />
    </xs:complexType>

    <xs:complexType name="resource">
        <xs:sequence>
            <xs:element name="property" type="property" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="name" type="xs:string" use="required" />
        <xs:attribute name="variant" type="xs:string" use="required" />
    </xs:complexType>

    <xs:element name="resources">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="resource" type="resource" minOccurs="0" maxOccurs="unbounded" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

这就是我的想象.一个variant=X 的xml 文档,它指的是resourcesX.xsd.不需要添加variant"属性,因为它是由引用的resourcesX.xsd添加的.

Here's how I picture this. One xml document for variant=X, which referrers to resourcesX.xsd. No need to add the "variant" attribute, as it is added by the referred resourcesX.xsd.

    <?xml version="1.0" encoding="UTF-8"?>
    <resources xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:noNamespaceSchemaLocation="resourceX.xsd">
        <resource name="res00" >
            <property name="propA" value="..." />
            <property name="propB" value="..." />
        </resource>
        <resource name="res01" >
            <property name="propA" value="..." />
            <property name="propB" value="..." />
        </resource>
    </resources>

variant=Y 的另一个 xml 文档,它指的是 resourcesY.xsd.不需要添加variant"属性,因为它是由引用的resourcesY.xsd添加的.

Another xml document for variant=Y, which referrers to resourcesY.xsd. No need to add the "variant" attribute, as it is added by the referred resourcesY.xsd.

    <?xml version="1.0" encoding="UTF-8"?>
    <resources xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:noNamespaceSchemaLocation="resourceY.xsd">
        <resource name="res02" >
            <property name="propA" value="..." />
            <property name="propB" value="..." />
        </resource>
        <resource name="res03" >
            <property name="propA" value="..." />
            <property name="propB" value="..." />
        </resource>
    </resources>

resourceX.xsd 和resourceY.xsd 可以扩展resources.xsd 吗?如果是这样,他们会是什么样子?需要在 resources.xsd 中进行任何更改吗?

Can resourceX.xsd and resourceY.xsd extend the resources.xsd? If so, what would they look like? any changes needed to be made in resources.xsd?

谢谢!/亚历克斯

推荐答案

听起来您的目标是在特定文件中只拥有单个变体的资源,在这种情况下,而不是删除 variant 属性,将其移动到根 resources 类型是否有意义?这将允许文档结构保留有关正在表示的变体的信息,但也确保特定文件中的所有 resource 元素与特定变体相关联.类似的东西:

It sounds like your goal is to only have resources of a single variant in a particular file, in that case, instead of removing the variant attribute, would it make sense to move it to the root resources type? This would allow the document structure to retain the information about which variant is being represented, but also ensure that all of the resource elements in a particular file are associated with a particular variant. Something like:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:complexType name="property">
        <xs:attribute name="name" type="xs:string" />
        <xs:attribute name="value" type="xs:string" />
    </xs:complexType>

    <xs:complexType name="resource">
        <xs:sequence>
            <xs:element name="property" type="property" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="name" type="xs:string" use="required" />
    </xs:complexType>

    <xs:element name="resources">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="resource" type="resource" minOccurs="0" maxOccurs="unbounded" />
            </xs:sequence>
            <xs:attribute name="variant" type="xs:string" use="required"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

和实例文档:

<?xml version="1.0" encoding="UTF-8"?>
<resources xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="resources.xsd"
        variant="X">
    <resource name="res00" >
        <property name="propA" value="..." />
        <property name="propB" value="..." />
    </resource>
    <resource name="res01" >
        <property name="propA" value="..." />
        <property name="propB" value="..." />
    </resource>
</resources>

这篇关于如何从另一个 xml-schema 扩展一个 xml-schema?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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