草庐IT

xml - 选择有条件的节点

coder 2024-06-30 原文

我正在尝试转换 XML 文件,但被阻止了。这个想法是聚合来自 <ore:Aggregation> 的每个元素节点直到下一个。这是某种分项。但我不能得到超过 1 edm:WebResource每创建 dc:item .

XML :

<rdf:RDF>
    <ore:Aggregation rdf:about="id1">
        <some:crap/>
    </ore:Aggregation>
    <edm:ProvidedCHO rdf:about="id1">
        <some:crap/>
    </edm:ProvidedCHO>
    <edm:WebResource rdf:about="some/random/url"> 
        <some:crap/>
    </edm:WebResource>
            ...
               (n 'edm:WebResource' nodes)
            ...
    <edm:WebResource rdf:about="some/random/url">
        <some:crap/>    
    </edm:WebResource>

    <ore:Aggregation rdf:about="id2">
        <some:crap/>
    </ore:Aggregation>
    <edm:ProvidedCHO rdf:about="id2">
        <some:crap/>
    </edm:ProvidedCHO>
    <edm:WebResource rdf:about="some/random/url"> 
        <some:crap/>
    </edm:WebResource>
            ...
               (n 'edm:WebResource' nodes)
            ...
    <edm:WebResource rdf:about="some/random/url">
        <some:crap/>    
    </edm:WebResource>

        ... and on and on ...
</rdf:RDF>

XSL

<xsl:template match="/">
    <xsl:apply-templates select="/rdf:RDF/ore:Aggregation"/>
</xsl:template>

<xsl:template match="/rdf:RDF/ore:Aggregation">
    <rdf:RDF>
    <xsl:for-each select=".">
            <dc:item>
                <xsl:attribute name="rdf:about">
                    <xsl:value-of select="concat($fileName, '_item', position())"/>
                </xsl:attribute>

                <xsl:copy-of select="."/>
                <xsl:copy-of select="following-sibling::edm:ProvidedCHO[1]"/>
                <xsl:copy-of select="following-sibling::edm:WebResource[1]"/>

                <!-- WHERE IT SUCKS -->
                <xsl:if test="local-name(following-sibling::*[3]) = 'edm:WebResource'">
                    <xsl:copy-of select="following-sibling::*[3]"/>
                </xsl:if>                    
                <!-- ./WHERE IT SUCKS -->


            </dc:item>
    </xsl:for-each>
    </rdf:RDF>
</xsl:template>

另一个带来太多节点的尝试:

<!-- WHERE IT SUCKS -->
<xsl:copy-of select="following-sibling::*[local-name (preceding::*[1]) = 'ore:Aggregation']"/>
<!-- ./WHERE IT SUCKS -->

预期输出

<!-- ITEM N1 -->
<rdf:RDF>
    <dc:item rdf:about="some.concat.string"/>
    <ore:Aggregation rdf:about="id1">
        <some:crap/>
    </ore:Aggregation>
    <edm:ProvidedCHO rdf:about="id1">
        <some:crap/>
    </edm:ProvidedCHO>
    <edm:WebResource rdf:about="some/random/url"> 
        <some:crap/>
    </edm:WebResource>
</rdf:RDF>

<!-- ITEM N2 -->
<rdf:RDF>
     <dc:item rdf:about="some.concat.string"/>
     <ore:Aggregation rdf:about="id1">
     <etc/>

最佳答案

在 XSLT 2.0 中,这看起来像是 xsl:for-each-group 的工作(参见 http://www.xml.com/pub/a/2003/11/05/tr.html)。特别是,将其与 group-starting-with

一起使用
 <xsl:for-each-group select="*" group-starting-with="ore:Aggregation">

这将在定位到父 rdf:RDF 元素时完成,并将所有子元素分组,以 ore:Aggregration 开始每组。 xsl:for-each-group 中的代码随后会针对每个 ore:Aggregation 元素调用一次,然后您可以使用 current-group() 访问组内所有元素的函数。

初学者试试这个 XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:rdf="rdf" xmlns:ore="ore" xmlns:dc="dc">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />

    <xsl:template match="rdf:RDF">
        <xsl:for-each-group select="*" group-starting-with="ore:Aggregation">
            <rdf:RDF xmlns:edm="edm" xmlns:ore="ore" xmlns:some="some">
                <dc:item rdf:about="{concat('item', position())}" />
                <xsl:apply-templates select="current-group()" />
            </rdf:RDF>
        </xsl:for-each-group>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet> 

请注意,此生成的输出 XML 格式不正确,因为它缺少单个根元素。如果添加一个会更好,不仅仅是为了使其格式正确,而且命名空间声明也会放在一个地方:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:rdf="rdf" xmlns:ore="ore" xmlns:dc="dc">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />

    <xsl:template match="rdf:RDF">
        <rdf:root xmlns:edm="edm" xmlns:ore="ore" xmlns:some="some">
            <xsl:for-each-group select="*" group-starting-with="ore:Aggregation">
                <rdf:RDF>
                    <dc:item rdf:about="{concat('item', position())}" />
                    <xsl:apply-templates select="current-group()" />
                </rdf:RDF>
            </xsl:for-each-group>
        </rdf:root>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet> 

还要注意 Attribute Value Templates 的使用在创建 rdf:about 时进一步减少了所需的代码量。

关于xml - 选择有条件的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30897224/

有关xml - 选择有条件的节点的更多相关文章

  1. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  2. ruby - 如何根据特征实现 FactoryGirl 的条件行为 - 2

    我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden

  3. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  4. ruby - 定义方法参数的条件 - 2

    我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano

  5. ruby - Rails 3 的 RGB 颜色选择器 - 2

    状态:我正在构建一个应用程序,其中需要一个可供用户选择颜色的字段,该字段将包含RGB颜色代码字符串。我已经测试了一个看起来很漂亮但效果不佳的。它是“挑剔的颜色”,并托管在此存储库中:https://github.com/Astorsoft/picky-color.在这里我打开一个关于它的一些问题的问题。问题:请建议我在Rails3应用程序中使用一些颜色选择器。 最佳答案 也许页面上的列表jQueryUIDevelopment:ColorPicker为您提供开箱即用的产品。原因是jQuery现在包含在Rails3应用程序中,因此使用基

  6. ruby-on-rails - 使用包含多个关联和单独的条件 - 2

    我的Gallery模型中有以下查询:media_items.includes(:photo,:video).rank(:position_in_gallery)我的图库模型有_许多媒体项,每个都有一个照片或视频关联。到目前为止,一切正常。它返回所有media_items包括它们的photo或video关联,由media_item的position_in_gallery属性排序。但是我现在需要将此查询返回的照片限制为仅具有is_processing属性的照片,即nil。是否可以进行相同的查询,但条件是返回的照片等同于:.where(photo:'photo.is_processingIS

  7. ruby-on-rails - 在 haml View 中重构条件 - 2

    除了可访问性标准不鼓励使用这一事实指向当前页面的链接,我应该怎么做重构以下View代码?#navigation%ul.tabbed-ifcurrent_page?(new_profile_path)%li{:class=>"current_page_item"}=link_tot("new_profile"),new_profile_path-else%li=link_tot("new_profile"),new_profile_path-ifcurrent_page?(profiles_path)%li{:class=>"current_page_item"}=link_tot("p

  8. ruby - 我正在学习编程并选择了 Ruby。我应该升级到 Ruby 1.9 吗? - 2

    我完全不是程序员,正在学习使用Ruby和Rails框架进行编程。我目前正在使用Ruby1.8.7和Rails3.0.3,但我想知道我是否应该升级到Ruby1.9,因为我真的没有任何升级的“遗留”成本。缺点是什么?我是否会遇到与普通gem的兼容性问题,或者甚至其他我不太了解甚至无法预料的问题? 最佳答案 你应该升级。不要坚持从1.8.7开始。如果您发现不支持1.9.2的gem,请避免使用它们(因为它们很可能不被维护)。如果您对gem是否兼容1.9.2有任何疑问,您可以在以下位置查看:http://www.railsplugins.or

  9. ruby-on-rails - Rails 单选按钮 - 模型中多列的一种选择 - 2

    我希望用户从一个模型的三个选项中选择一个。即我有一个模型视频,可以被评为正面/负面/未知目前我有三列bool值(pos/neg/unknown)。这是处理这种情况的最佳方式吗?为此,表单应该是什么样的?目前我有类似的东西但显然它允许多项选择,而我试图将它限制为只有一个..怎么办? 最佳答案 如果要使用字符串列,让我们说rating。然后在你的表单中:#...#...它只允许一个选择编辑完全相同但使用radio_button_tag: 关于ruby-on-rails-Rails单选按钮-模

  10. ruby-on-rails - CarrierWave - PDF - 只选择第一页 - 2

    我的Rails应用程序中安装了carrierwave。但是,当用户上传多页pdf时,我只希望应用程序获取文档中的第一页并将其转换为jpeg。这可能吗?用什么命令?这是我的uploader。#encoding:utf-8classImageUploader[200,300]##defscale(width,height)##dosomething#end#Createdifferentversionsofyouruploadedfiles:version:thumbdoprocess:resize_to_fill=>[150,210]process:convert=>:jpgdefful

随机推荐