通过考虑基于条件的多个节点进行 XSLT 密钥分组

XSLT Key Grouping by considering multiple nodes based on a Condition

<root>
   <Entry>
      <ID>1</ID>
      <Details>
         <Code>A1</Code>
         <Value>1000</Value>
         <Indicator>1</Indicator>
      </Details>
   </Entry>

   <Entry>
      <ID>2</ID>
      <Details>
         <Code>A2</Code>
         <Value>2000</Value>
         <Indicator>2</Indicator>
      </Details>
   </Entry>

   <Entry>
      <ID>3</ID>
      <Details>
         <Code>A3</Code>
         <Value>2500</Value>
         <Indicator>3</Indicator>
      </Details>
   </Entry>

   <Entry>
      <ID>4</ID>
      <Details>
         <Code>B1</Code>
         <Value>3000</Value>
         <Indicator>0</Indicator>
      </Details>
   </Entry>

   <Entry>
      <ID>5</ID>
      <Details>
         <Code>B2</Code>
         <Value>4000</Value>
         <Indicator>5</Indicator>
      </Details>
   </Entry>

   <Entry>
      <ID>6</ID>
      <Details>
         <Code>B3</Code>
         <Value>4500</Value>
         <Indicator>7</Indicator>
      </Details>
   </Entry>
</root>

我有这个输入 XML,这是我 的延续。基本上我希望根据 <Code> 节点的值对节点进行分组。映射如下:

  1. 代码 'A1'、'A2' 和 'A3' 需要组合在一起(比如说代码组 'A')
  2. 代码 'B1'、'B2' 和 'B3' 需要组合在一起(比如说代码组 'B')

[这些代码是通用的,仅作为示例,实际代码有所不同,并不那么简单,请不要按字面意思理解]

我最终对来自这些组中 <Value> 个节点的值求和,但除了我之前的问题之外,我只在 <Indicator> 节点值 更大时求和大于或等于 2

我目前使用这个 XSLT(来源:michael.hor257k):

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

<xsl:key name="entry" match="Entry" use="Details/Code"/>

<xsl:template match="/root">
    <Output>
        <Code-group> A </Code-group>
        <Sum>
            <xsl:value-of select="sum(key('entry', ('A1', 'A2', 'A3'))/Details/Value)" />
        </Sum>
        <Code-group> B </Code-group>
        <Sum>
            <xsl:value-of select="sum(key('entry', ('B1', 'B2', 'B3'))/Details/Value)" />
        </Sum>
    </Output>
</xsl:template>

</xsl:stylesheet>

产生如下输出:

<Output>
  <Code-group> A </Code-group>
  <Sum> 5500 </Sum>

  <Code-group> B </Code-group>
  <Sum> 11500 </Sum>
</Output>

但是我需要这个场景的输出:

<Output>
  <Code-group> A </Code-group>
  <Sum> 4500 </Sum>

  <Code-group> B </Code-group>
  <Sum> 8500 </Sum>
</Output>

即仅当<Indicator>大于或等于2

时,才对各个Code-groups中的<Value>节点值求和

有没有办法在定义键时包含这个关系运算符,还是应该实现为 2 个键?感谢任何输入

提前致谢!

只需使用 XPath 谓词

<xsl:template match="/root">
    <Output>
        <Code-group> A </Code-group>
        <Sum>
            <xsl:value-of select="sum(key('entry', ('A1', 'A2', 'A3'))/Details[Indicator >= 2 ]/Value)" />
        </Sum>
        <Code-group> B </Code-group>
        <Sum>
            <xsl:value-of select="sum(key('entry', ('B1', 'B2', 'B3'))/Details[Indicator >= 2 ]/Value)" />
        </Sum>
    </Output>
</xsl:template>

看来你只需要一个小修改:

XSLT 2.0

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

<xsl:key name="entry" match="Details[Indicator >= 2]" use="Code"/>

<xsl:template match="/root">
    <Output>
        <Code-group> A </Code-group>
        <Sum>
            <xsl:value-of select="sum(key('entry', ('A1', 'A2', 'A3'))/Value)" />
        </Sum>
        <Code-group> B </Code-group>
        <Sum>
            <xsl:value-of select="sum(key('entry', ('B1', 'B2', 'B3'))/Value)" />
        </Sum>
    </Output>
</xsl:template>

</xsl:stylesheet>