草庐IT

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

coder 2024-07-01 原文

我有一个如下所示的 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/

有关xml - 如何从另一个 xml 模式扩展一个 xml 模式?的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  4. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  5. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  6. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  7. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  8. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  9. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  10. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

随机推荐