草庐IT

xml - 通过 XSLT 插入和删除元素的最佳实践

coder 2024-06-29 原文

我是 XSLT 的新手,现在我遇到了一个相当复杂的问题......我的输入文件看起来像

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doc>
  <outerElement>
    <first>
      <textElement>Some Text</textElement>
    </first>
    <second>
      <textElement>Some Text</textElement>
    </second>
    <third>
      <textElement>Some Text</textElement>
    </third>
  </outerElement>
</doc>

“第二个”元素出现问题。在输入文件中,它可以有 3 种形式之一:

MISSING

<second>
  <textElement>Some Text</textElement>
</second>

<second missingCause="" />

在输出文件中,它应该像第二种形式一样被插入。如果它在 textElement 应该指示它是由模板插入之前丢失,这里重要的是它必须位于第二个位置,因为我想根据 xsd 模式验证它...

这是我当前的 XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <!-- remove Elements with attribute deleteme -->
  <xsl:template match="outerElement/second[@missingCause='*']" />

  <!-- look if second is there. If not insert -->
  <xsl:template match="outerElement">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
      <xsl:if test="not(second)">
        <second>
        </second>
      </xsl:if>
    </xsl:copy>    
  </xsl:template>

  <!-- Insert Element second -->
  <xsl:template match="outerElement/second">
    <xsl:apply-templates select="node()|@*"/>
    <xsl:copy>
      <xsl:if test="not(textElement)">
        <textElement>Inserted by Template</textElement>
      </xsl:if>
    </xsl:copy>    
  </xsl:template>

</xsl:stylesheet> 

如果缺少“second”,我的输出文件只会得到一个元素“”,但它是空的,并且不会应用最后一个转换。其他一切看起来都很好,而当我在文档中收到警告时...

有人可以帮我将元素移动到它必须在的位置,以便它可以验证架构并使其在所有三种情况下都能正常工作吗?

我认为我的方式似乎不太好,最终会变得困惑,因为我有几个类似的元素要像这样插入/删除。

感谢阅读;)

最佳答案

这是一个通用解决方案,适用于 outerElement 的任意数量的不同名称的子级以及它们之间的任何首选顺序:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pUncertainElName" select="'second'"/>

 <xsl:param name="pOrderedNames" select="'|first|second|third|'"/>

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

 <xsl:template match="outerElement">
  <xsl:variable name="vrtfFirstPass">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
       <xsl:apply-templates select=
         "self::*[not(*[name() = $pUncertainElName])
                or
                 *[name()=$pUncertainElName and @missing-cause]]"
         mode="missing"/>
      </xsl:copy>
  </xsl:variable>

  <xsl:apply-templates select="ext:node-set($vrtfFirstPass)/*" mode="pass2"/>
 </xsl:template>

 <xsl:template match="*[@missing-cause]"/>

 <xsl:template match="*" mode="missing">
    <xsl:element name="{$pUncertainElName}">
        <textElement>Some Text</textElement>
    </xsl:element>
 </xsl:template>

 <xsl:template match="outerElement" mode="pass2">
  <xsl:copy>
   <xsl:apply-templates>
     <xsl:sort data-type="number" select=
     "string-length(substring-before($pOrderedNames,
                                     concat('|', name(), '|')
                                     )
                   )"/>
   </xsl:apply-templates>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时:

<doc>
    <outerElement>
        <first>
            <textElement>Some Text</textElement>
        </first>
        <second missing-cause="">
            <textElement>Some Text</textElement>
        </second>
        <third>
            <textElement>Some Text</textElement>
        </third>
    </outerElement>
</doc>

产生了想要的、正确的结果:

<doc>
   <outerElement>
      <first>
         <textElement>Some Text</textElement>
      </first>
      <second>
         <textElement>Some Text</textElement>
      </second>
      <third>
         <textElement>Some Text</textElement>
      </third>
   </outerElement>
</doc>

应用于此 XML 文档时:

<doc>
    <outerElement>
        <first>
            <textElement>Some Text</textElement>
        </first>
        <third>
            <textElement>Some Text</textElement>
        </third>
    </outerElement>
</doc>

再次产生了想要的、正确的结果:

<doc>
   <outerElement>
      <first>
         <textElement>Some Text</textElement>
      </first>
      <second>
         <textElement>Some Text</textElement>
      </second>
      <third>
         <textElement>Some Text</textElement>
      </third>
   </outerElement>
</doc>

最后,当对这个 XML 文档应用相同的转换时:

<doc>
    <outerElement>
        <first>
            <textElement>Some Text</textElement>
        </first>
        <second>
            <textElement>Some Text</textElement>
        </second>
        <third>
            <textElement>Some Text</textElement>
        </third>
    </outerElement>
</doc>

同样想要,产生了正确的结果:

<doc>
   <outerElement>
      <first>
         <textElement>Some Text</textElement>
      </first>
      <second>
         <textElement>Some Text</textElement>
      </second>
      <third>
         <textElement>Some Text</textElement>
      </third>
   </outerElement>
</doc>

请注意:

outerElement 可以有任意数量的不同名称的子代(不只是三个),并且它们的顺序可能无法预先知道。

例如:

给定这个 XML 文档:

<doc>
    <outerElement>
        <first>
            <textElement>Some Text</textElement>
        </first>
        <second missing-cause="">
            <textElement>Some Text</textElement>
        </second>
        <third>
            <textElement>Some Text</textElement>
        </third>
        <fourth>
            <textElement>Some Text</textElement>
        </fourth>
    </outerElement>
</doc>

还有这个顺序:第四,第二,第三,第一

我们只需替换:

 <xsl:param name="pOrderedNames" select="'|first|second|third|'"/>

:

 <xsl:param name="pOrderedNames" select="'|fourth|second|third|first|'"/>

现在产生了新的想要的结果:

<doc>
   <outerElement>
      <fourth>
         <textElement>Some Text</textElement>
      </fourth>
      <second>
         <textElement>Some Text</textElement>
      </second>
      <third>
         <textElement>Some Text</textElement>
      </third>
      <first>
         <textElement>Some Text</textElement>
      </first>
   </outerElement>
</doc>

解释:

  1. 这是一个两次转换。

  2. 在外部/全局参数 $pUncertainElName 中指定可能存在或不存在的元素名称。为了便于解释,我们将此元素称为 second

  3. 在第一遍中,outerElement 的所有子元素(具有 missing-cause 属性的 second 除外)是“按原样”复制。如果 second 元素不存在或具有 missing-cause 属性,我们将输出 outerElement 的新子元素——正是想要的 第二个元素。

  4. 在第二遍中,我们根据优先级对第一遍生成的 outerElement 的子项进行排序,如另一个名为 $pOrderedNames 的外部/全局参数中指定的那样(该字符串中另一个名字左边的名字,优先级高)

关于xml - 通过 XSLT 插入和删除元素的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11193443/

有关xml - 通过 XSLT 插入和删除元素的最佳实践的更多相关文章

  1. ruby-on-rails - 使用 Ruby on Rails 进行自动化测试 - 最佳实践 - 2

    很好奇,就使用ruby​​onrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提

  2. 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

  3. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

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

  5. ruby - 我可以使用 Ruby 从 CSV 中删除列吗? - 2

    查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html

  6. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  7. ruby - 通过 ruby​​ 进程共享变量 - 2

    我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是

  8. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  9. ruby - 我可以使用 aws-sdk-ruby 在 AWS S3 上使用事务性文件删除/上传吗? - 2

    我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的

  10. ruby-on-rails - Enumerator.new 如何处理已通过的 block ? - 2

    我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m

随机推荐