草庐IT

.net - 您如何在 XSLT 中对调用模板的结果求和?

coder 2024-07-01 原文

我有一些 XML 如下;

<risk>
  <driver driverId="2">
    <vehicleUse>M</vehicleUse>
  </driver>
  <driver driverId="3">
    <vehicleUse>F</vehicleUse>
  </driver>
  <driver driverId="4">
    <vehicleUse>I</vehicleUse>
  </driver>
</risk>

我正在使用 XSLT(v1.0,.NET 实现)将每个 vehicleUse 转换为一个数字,然后得到这些数字的总和。 vehicleUses 翻译为 M=3、F=2 和 I=1。一个额外的复杂性是,对于 ID 为 3 的驱动程序,这些值乘以 10,对于驱动程序 4 乘以 100。因此在上面的示例中,总数将为 3 + 20 + 100 = 123。

我在我的 XSLT 文件中定义了这样一个模板;

<xsl:template name="getVehicleUseScore">
  <xsl:param name="driverId" />
  <xsl:param name="vehicleUse" />
  <!-- Implementation left out for brevity -->
</xsl:template>

然后 XSLT 文件的其余部分调用模板;

<xsl:template match="risk">
  <vehicleUseScore>
    <xsl:for-each select="driver">
      <xsl:call-template name="getVehicleUseScore">
        <xsl:with-param name="driverId" select="@driverId" />
        <xsl:with-param name="vehicleUse" select="vehicleUse" />
      </xsl:call-template>
    </xsl:for-each>
  </vehicleUseScore>
</xsl:template>

<xsl:template match="/">
  <xsl:apply-templates />
</xsl:template>

结果是我得到的文本“320100”只是将 3、20 和 100 连接在一起,这至少证明 getVehicleUseScore 模板有效。

我想将 getVehicleUseScore 的结果传递给 sum() 函数,但我不知道如何传递。我尝试了以下;

<xsl:value-of select="sum(getVehicleUseScore(@driverId, vehicleUse))" />

但是 XSLT 编译器指出“getVehicleUseScore() 是一个未知的 XSLT 函数”。

有什么办法吗?

最佳答案

这是一个简短的 XSLT 1.0 方法来完成此操作:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

 <xsl:template match="risk">
  <vehicleUseScore>
   <xsl:variable name="vrtfResult">
        <xsl:for-each select="driver">
          <xsl:call-template name="getVehicleUseScore">
            <xsl:with-param name="pdriverId" select="@driverId" />
            <xsl:with-param name="pvehicleUse" select="vehicleUse" />
          </xsl:call-template>
        </xsl:for-each>
    </xsl:variable>

    <xsl:value-of select="sum(msxsl:node-set($vrtfResult)/*)"/>
  </vehicleUseScore>
 </xsl:template>

 <xsl:template name="getVehicleUseScore">
  <xsl:param name="pdriverId" />
  <xsl:param name="pvehicleUse" />
  <score>
   <xsl:variable name="vValue" select=
   "(($pvehicleUse='M')*3 + ($pvehicleUse='F')*2 + ($pvehicleUse='I')*1)"/>
   <xsl:variable name="vFactor" select=
   "1 +(9*($pdriverId=3)) + (99*($pdriverId=4))"/>

   <xsl:value-of select="$vValue*$vFactor"/>
  </score>
 </xsl:template>
</xsl:stylesheet>

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

<risk>
    <driver driverId="2">
        <vehicleUse>M</vehicleUse>
    </driver>
    <driver driverId="3">
        <vehicleUse>F</vehicleUse>
    </driver>
    <driver driverId="4">
        <vehicleUse>I</vehicleUse>
    </driver>
</risk>

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

<vehicleUseScore>123</vehicleUseScore>

解释:

当捕获变量中模板的输出时,此变量的类型为 RTF(结果树片段),除非其内容包含任何节点(文本节点除外),否则其内容无法通过 XPath 表达式导航。

为此,需要对该变量调用依赖于供应商的扩展函数 xxx:node-set(),以便将 RTF 转换为常规树。

这里我们还使用了这样一个事实,即每当在算术表达式中遇到 bool 值时,它都会被转换为数字,并且根据定义:

number(true()) = 1

number(false()) = 0

关于.net - 您如何在 XSLT 中对调用模板的结果求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14530762/

有关.net - 您如何在 XSLT 中对调用模板的结果求和?的更多相关文章

  1. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  2. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  3. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  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-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  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 - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  8. ruby - 如何模拟 Net::HTTP::Post? - 2

    是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou

  9. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  10. ruby - 如何在 Lion 上安装 Xcode 4.6,需要用 RVM 升级 ruby - 2

    我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121

随机推荐