XSL将值乘以选择值中的值

时间:2018-06-29 08:38:35

标签: xml xslt

我有这段代码:

<xsl:value-of 
select="sum(objects/object/items/item[not(product_type='configurable' and 
count(custom_options/custom_option[option_id='2686']) &gt; 
0)]/custom_options/custom_option[option_id='2686']/value)" />

可以正确输出我想要的整数,但是现在在另一个字段中,我想通过qty_ordered的值来明确指出这个整数。所以我尝试的是这样:

<xsl:value-of 
select="sum(objects/object/items/item[not(product_type='configurable' and 
count(custom_options/custom_option[option_id='2686']) &gt; 
0)]/custom_options/custom_option[option_id='2686']/value * qty_ordered)" />

不幸的是,这行不通,我也不知道该怎么做,有人能帮助我生成一行输出id2686/value * qty_ordered的代码吗? qty_ordered也在项/项循环中。

这是输入:

<items>
<item>
<qty_ordered>5.0000</qty_ordered>

<custom_option>
<name>Aantal pagina's in PDF</name>
<value>1</value>
<sku/>
<option_id>2686</option_id>
</custom_option>

1 个答案:

答案 0 :(得分:0)

在XSLT 1.0中,有两种方法可以做到这一点。一种方法是创建一个临时结果树,为每个项目保存qty_orderedvalue的各个值,如下所示:

<xsl:variable name="items">
  <xsl:for-each select="item[not(product_type='configurable')]">
    <item>
      <xsl:value-of select="qty_ordered * custom_options/custom_option/value" />
    </item>
  </xsl:for-each>
</xsl:variable>

但是,在XSLT 1.0中,您将需要使用扩展功能来访问变量中的节点(因为items变量的类型为“结果树片段”,需要将其转换为与sum函数一起使用的节点集)。最常用的一种是exslt。

尝试使用此XSLT(为简便起见,我简化了xpath表达式)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:exslt="http://exslt.org/common"
                exclude-result-prefixes="exslt"
                version="1.0">

  <xsl:output method="xml" indent="yes" />

  <xsl:template match="items">
    <xsl:variable name="items">
      <xsl:for-each select="item[not(product_type='configurable')]">
        <item>
          <xsl:value-of select="qty_ordered * custom_options/custom_option/value" />
        </item>
      </xsl:for-each>
    </xsl:variable>

    <result>
      <xsl:value-of select="sum(exslt:node-set($items)/item)" />
    </result>
  </xsl:template>
</xsl:stylesheet>

将其应用于此XML

<items>
   <item>
      <qty_ordered>5.0000</qty_ordered>
      <custom_options>
         <custom_option>
            <name>Aantal pagina's in PDF</name>
            <value>1</value>
            <sku/>
            <option_id>2686</option_id>
         </custom_option>
      </custom_options>
   </item>
   <item>
      <qty_ordered>2.0000</qty_ordered>
      <custom_options>
         <custom_option>
            <name>Aantal pagina's in PDF - Vol 2</name>
            <value>3</value>
            <sku/>
            <option_id>2687</option_id>
         </custom_option>
      </custom_options>
   </item>
</items>

结果是这个...

<result>11</result>

编辑:由于您对现有XSLT的展示不够多,很难解释如何将其与现有代码集成

但是您目前在哪里执行此操作...

 <xsl:value-of 
select="sum(objects/object/items/item[not(product_type='configurable' and 
count(custom_options/custom_option[option_id='2686']) &gt; 
0)]/custom_options/custom_option[option_id='2686']/value)" />

您可能想将其更改为此...

<xsl:variable name="items">
  <xsl:for-each select="objects/object/items/item[not(product_type='configurable' and count(custom_options/custom_option[option_id='2686']) &gt; 0)]">
    <item>
      <xsl:value-of select="qty_ordered * custom_options/custom_option[option_id='2686']/value" />
    </item>
  </xsl:for-each>
</xsl:variable>
<xsl:value-of select="sum(exslt:node-set($items)/item)" />