jjzjj

xml - 为什么 XSL 函数 `ends-with` 和 `matches` 都抛出错误?

coder 2024-07-04 原文



我正在转换一些 XML,重命名名为 alt-title 的每个元素至 Running_Head ,前提是属性 alt-title-type等于“running-head”。


因此,下面的代码使用了 <xsl:when test="starts-with(@alt-title-type, 'running-head')"> 行这工作正常。但是,当我将其更改为其中之一时:

  • <xsl:when test="ends-with(@alt-title-type, 'running-head')">
  • <xsl:when test="matches(@alt-title-type, 'running-head')">

...抛出此错误:

Error:XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]: xmlXPathCompiledEval: 2 objects left on the stack.

所以,似乎函数starts-with正在工作,在哪里 ends-withmatches不是。


这是我的 XSL,使用 starts-with ,这似乎工作正常:

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

    <!-- Running_Head -->
    <xsl:template match="@*|node()">
        <xsl:choose>

            <xsl:when test="starts-with(@alt-title-type, 'running-head')">
                <xsl:element name="Running_Head">
                    <xsl:apply-templates select="@*|node()"/>
                </xsl:element>
            </xsl:when>

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

        </xsl:choose>
    </xsl:template> <!-- end of Running_Head -->

</xsl:stylesheet>

...这是正在转换的 XML:

<root-node>
    <alt-title alt-title-type="running-head">
        This is working
    </alt-title>
    <alt-title alt-title-type="asdfng-head">
        asdfasdf
    </alt-title>
    <alt-title>
        asdfasdf
    </alt-title>
    <alt-title alt-title-type="running-head">
        This is also working
    </alt-title>
</root-node>


我正在 http://xslt.online-toolz.com/tools/xslt-transformation.php 测试这个, 和 http://www.xsltcake.com/ .

最佳答案

正如其他人指出的那样,XSLT 1.0 处理器不支持大多数 XPath 2.0 函数(例如 matches()ends-with())。

此外,在实现当前要求的转换中根本不需要这些函数:

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

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

 <xsl:template match="alt-title[@alt-title-type='running-head']">
  <Running_Head>
    <xsl:apply-templates select="@*|node()"/>
  </Running_Head>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<root-node>
    <alt-title alt-title-type="running-head">
      This is working
  </alt-title>
    <alt-title alt-title-type="asdfng-head">
     asdfasdf
  </alt-title>
    <alt-title>
     asdfasdf
  </alt-title>
    <alt-title alt-title-type="running-head">
     This is also working
  </alt-title>
</root-node>

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

<root-node>
   <Running_Head alt-title-type="running-head">
      This is working
  </Running_Head>
   <alt-title alt-title-type="asdfng-head">
     asdfasdf
  </alt-title>
   <alt-title>
     asdfasdf
  </alt-title>
   <Running_Head alt-title-type="running-head">
     This is also working
  </Running_Head>
</root-node>

解释:

正确使用模板、匹配模式和覆盖 identity rule .

关于xml - 为什么 XSL 函数 `ends-with` 和 `matches` 都抛出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11712884/

有关xml - 为什么 XSL 函数 `ends-with` 和 `matches` 都抛出错误?的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - rails : keeping DRY with ActiveRecord models that share similar complex attributes - 2

    这似乎应该有一个直截了当的答案,但在Google上花了很多时间,所以我找不到它。这可能是缺少正确关键字的情况。在我的RoR应用程序中,我有几个模型共享一种特定类型的字符串属性,该属性具有特殊验证和其他功能。我能想到的最接近的类似示例是表示URL的字符串。这会导致模型中出现大量重复(甚至单元测试中会出现更多重复),但我不确定如何让它更DRY。我能想到几个可能的方向...按照“validates_url_format_of”插件,但这只会让验证干给这个特殊的字符串它自己的模型,但这看起来很像重溶液为这个特殊的字符串创建一个ruby​​类,但是我如何得到ActiveRecord关联这个类模型

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

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

  4. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

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

  6. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

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

  8. ruby-on-rails - Ruby on Rails with Haml - 如何从 erb 切换 - 2

    我正在从erb文件切换到HAML。我将hamlgem添加到我的系统中。我创建了app/views/layouts/application.html.haml文件。我应该只删除application.html.erb文件吗?此外,仍然有/public/index.html文件被呈现为默认页面。我想创建自己的默认index.html.haml页面。我应该把它放在哪里以及如何使系统呈现该文件而不是默认索引文件?谢谢! 最佳答案 是的,您可以删除任何已转换为HAML的View的ERB版本。至于你的另一个问题,删除public/index/h

  9. ruby - ruby 中的 TOPLEVEL_BINDING 是什么? - 2

    它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput

  10. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

随机推荐