草庐IT

xml - 更改 XML 元素顺序,同时保持结构层次和属性

coder 2024-06-29 原文

我想改变一些 XML 元素的顺序。 XML 很复杂并且由单独的过程生成 - 我不需要更改它的费用,所以我希望使用 XSLT 来更正元素顺序。

我不是 XSLT 专家 (!),所以我查找了一些片段并发现了一些东西,根据我的情况进行了一些小的改动,几乎 可以工作。我目前拥有的最佳版本以正确的顺序输出元素,但删除了所有属性。

我用我的问题的相关特征创建了一个更简单的 xml 和相应的 xsl。

这是(虚拟的)示例 xml:

<?xml version="1.0" encoding="UTF-8"?>
<Companies xmlns="company:fruit:ns" Version="1.0">
  <Description>Some example companies and fruit shipments</Description>
  <Company CompanyId="Acme">
    <Description>Some example shipments</Description>
    <Shipment Id="ABC">
      <Description>Some apples</Description>
      <Fruit>
        <Apples>10</Apples>
      </Fruit>
    </Shipment>
    <Shipment Id="DEF">
      <Description>Some oranges and pears</Description>
      <Fruit>
        <Oranges>20</Oranges>
        <Pears>20</Pears>
      </Fruit>
    </Shipment>
    <Shipment Id="JKL">
      <Description>Empty</Description>
      <Fruit/>
    </Shipment>
    <Fruit/>
  </Company>
  <Fruit/>
</Companies>

问题是在 Company-Description 元素之后应该有一个 Company-Fruit 元素(而不是在所有 Shipment 元素之后)并且在 Companies-Description 元素之后应该有一个 Companies-Fruit 元素(而不是在所有 Shipment 元素之后)公司-公司元素)。我使用以下 xsl 转换来更正元素排序:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xpath-default-namespace="company:fruit:ns">
  <!-- See http://xsltbyexample.blogspot.com/2008/02/re-arrange-order-of-elements-in-xml.html -->
  <xsl:output omit-xml-declaration="no" indent="yes" method="xml" encoding="utf-8"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="*">
    <xsl:apply-templates select="self::*" mode="copy"/>
  </xsl:template>
  <xsl:template match="Company/Description">
    <xsl:message>Matched Company Description</xsl:message>
    <xsl:apply-templates select="self::*" mode="copy"/>
    <xsl:apply-templates select="../Fruit" mode="copy"/>
  </xsl:template>
  <xsl:template match="Companies/Description">
    <xsl:message>Matched Companies Description</xsl:message>
    <xsl:apply-templates select="self::*" mode="copy"/>
    <xsl:apply-templates select="../Fruit" mode="copy"/>
  </xsl:template>
  <xsl:template match="Company/Fruit"/>
  <xsl:template match="Companies/Fruit"/>
  <xsl:template match="*" mode="copy">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="text()">
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>

生成的 xml 具有正确的顺序,但大部分属性已被删除:

<?xml version="1.0" encoding="utf-8"?>
<Companies xmlns="company:fruit:ns">
   <Description>Some example companies and fruit shipments</Description>
   <Fruit/>
   <Company>
      <Description>Some example shipments</Description>
      <Fruit/>
      <Shipment>
         <Description>Some apples</Description>
         <Fruit>
            <Apples>10</Apples>
         </Fruit>
      </Shipment>
      <Shipment>
         <Description>Some oranges and pears</Description>
         <Fruit>
            <Apples>20</Apples>
            <Pears>20</Pears>
         </Fruit>
      </Shipment>
      <Shipment>
         <Description>Empty</Description>
         <Fruit/>
      </Shipment>
   </Company>
</Companies>

我欢迎 XSLT 专家的任何建议!

最佳答案

应保持几乎所有输入不变的转换最好从身份模板开始。

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

然后您相应地覆盖该模板。

<!-- throw away <Fruit> elements, initially - they are handled separately -->
<xsl:template match="Company/Fruit | Companies/Fruit" />

<!-- re-build <Company> and <Companies> in the correct order -->
<xsl:template match="Company | Companies">
  <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:copy-of select="Fruit" />
    <xsl:apply-templates select="node()" />
  </xsl:copy>
</xsl:template>

然后你就完成了。

关于xml - 更改 XML 元素顺序,同时保持结构层次和属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19316865/

有关xml - 更改 XML 元素顺序,同时保持结构层次和属性的更多相关文章

  1. ruby-on-rails - Ruby on Rails 迁移,将表更改为 MyISAM - 2

    如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设

  2. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  3. 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代码修改为

  4. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  5. ruby-on-rails - 在混合/模块中覆盖模型的属性访问器 - 2

    我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah

  6. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  7. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  8. ruby - Chef 执行非顺序配方 - 2

    我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul

  9. ruby - Capistrano 3 在任务中更改 ssh_options - 2

    我尝试使用不同的ssh_options在同一阶段运行capistranov.3任务。我的production.rb说:set:stage,:productionset:user,'deploy'set:ssh_options,{user:'deploy'}通过此配置,capistrano与用户deploy连接,这对于其余的任务是正确的。但是我需要将它连接到服务器中配置良好的an_other_user以完成一项特定任务。然后我的食谱说:...taskswithoriginaluser...task:my_task_with_an_other_userdoset:user,'an_othe

  10. ruby - Nokogiri 剥离所有属性 - 2

    我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog

随机推荐