xml - 如何从另一个 xml 模式扩展一个 xml 模式?

标签 xml xsd

我有一个如下所示的 xml 文档 (resources.xml),具有相应的 xml 架构 (resources.xsd)。此 xml 文档是手动维护的(即添加/删除/编辑资源元素)。总共可能有 500-1000 个资源元素。每个资源都可以是 variantX 或 variantY(在“现实生活”中,还有更多变体)。

我想将 xml 文档拆分成几个 xml 文档。每个变体(在本例中为 X 和 Y)对应一个 xml 文档,并带有相应的新 xml 模式。每个变体的 xml 模式应该扩展原始模式,并且只为其“变体”属性添加一个“硬编码”(固定?)值。

原因:为了避免在每个资源元素中重复“variant”属性。

这可能吗?每个变体的 xml 模式是什么样的? resources.xsd 需要做哪些改变?

也欢迎任何其他建议:)

资源.xml

    <?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 添加的。

资源X.xml

    <?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 添加的。

资源Y.xml

    <?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 进行任何更改吗?

谢谢! /亚历克斯

最佳答案

要将其拆分为多个模式文件,您首先需要修改基本模式以仅包含将共享的类型和元素,并且需要细化这些元素以允许扩展(或限制您的派生模式).在您的情况下,看起来 property 类型将被重用并且 resource 类型将受到限制,因此基本架构看起来像:

资源.xsd

<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:attribute name="variant" type="xs:string"/>
    </xs:complexType>

</xs:schema>

请注意,在资源中,variant 属性不再是 use="required",因为您的目标是您不希望实例文档中需要它.

对于架构的“X”变体,您需要包含 resources.xsd 架构,并且需要定义受限的 resourceX 元素和包含 resources 元素。

资源X.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:include schemaLocation="resources.xsd"/>

    <xs:complexType name="resourceX">
        <xs:complexContent>
            <xs:restriction base="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" fixed="X"/>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>

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

</xs:schema>

xs:include 语句将包含您之前定义的模式。新类型 resourceX 定义了对 resource 元素的限制,并将固定值 Xvariant 相关联。最后,resources 元素被重新定义为 resourceX 类型元素的集合,而不是 resource

resourceY.xsd 的定义方式与 resourceX.xsd 类似。

关于xml - 如何从另一个 xml 模式扩展一个 xml 模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8776894/

相关文章:

c# - xml c# 使用 Linq 添加元素到 Xml

javascript - 如何使用 jQuery 将 XML 数据格式从浏览器发送到服务器?

php - 扩展布局 .xml 文件在与父主题不同的 <vendor> 子主题中不起作用 : Magento 2. 0.4

Java JAXB 解码器链接异常

java - 动态生成 XML Schema

xml - vb.net-从XSD创建类并生成xml

xml - 向位于 XML 架构中多个子元素内的元素添加唯一 id

xml - 将 KML 导入 Google map 后,点显示在南极洲

java - Doclava - 如何生成版本控制的 xml?

java - <xs :any> inside an <xs:all> XSD 1. 0 解决方法?