通过在XSLT中对节点值进行硬编码对节点进行分组 [英] Grouping Nodes by hardcoding node values in XSLT
问题描述
<root>
<Entry>
<ID>1</ID>
<Details>
<Code>A1</Code>
<Value>1000</Value>
</Details>
</Entry>
<Entry>
<ID>2</ID>
<Details>
<Code>A2</Code>
<Value>2000</Value>
</Details>
</Entry>
<Entry>
<ID>3</ID>
<Details>
<Code>B1</Code>
<Value>3000</Value>
</Details>
</Entry>
<Entry>
<ID>4</ID>
<Details>
<Code>B2</Code>
<Value>4000</Value>
</Details>
</Entry>
</root>
我有此输入XML,希望通过XSLT进行分组,其中分组是通过硬编码节点值进行的.让我详细解释一下:
I have this input XML which I am looking to group via XSLT wherein the grouping happens by hardcoding node values. Let me explain that in detail:
需要根据出现在节点<Code>
中的Code参数进行分组,如下所示:
The grouping needs to happen based on the Code parameter appearing in the node <Code>
as follows:
- 代码"A1"和"A2"需要分组在一起
- 代码"B1"和"B2"需要分组在一起
我最终将这些组中<Value>
个节点的值相加.因此输出如下:
I am eventually summing the values coming out of <Value>
nodes in these groups. So the Output would be as follows:
<Output>
<Code-group> A </Code-group>
<Sum> 3000 </Sum>
<Code-group> B </Code-group>
<Sum> 7000 </Sum>
</Output>
对于此要求,需要对分组值进行硬编码(将A1,A2分组为A,将B1,B2分组为B).我之所以使用硬编码"一词,是因为代码(A1,A2,B1,B2)可以以任何顺序排列,所以我想对值进行硬编码以寻找分组,而不是寻找节点索引.
For this requirement there needs to be hardcoding of the grouping values (to group A1,A2 as A and B1, B2 as B). I am using the word 'hardcoded' because the Codes (A1,A2,B1,B2) can come in any order so I want to rather hardcode the values to look for grouping than looking for Node indices.
我研究了for-each-group方法以及Muenchian分组方法,但是无法实现上述组映射.任何帮助表示赞赏!
I looked at for-each-group method as well as the Muenchian Grouping method but wasn't able to achieve the above group mapping. Any help appreciated!
预先感谢
映射A1,A2-> & B1,B2-> B是一个通用示例,实际节点值与此完全不同,因此子字符串解决方案将不起作用.这就是为什么我一直专注于硬编码以实现该映射的原因.
The mapping A1,A2 --> A & B1,B2 --> B is a generic example, the actual node values are entirely different than this, so a substring solution wouldn't work. That is why I was focusing on hardcoding to achieve that mapping.
推荐答案
硬编码要求很难理解.也许您想做类似的事情:
The hardcoding requirement is difficult to understand. Perhaps you want to do something like:
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="Entry" use="Details/Code"/>
<xsl:template match="/root">
<Output>
<Code-group> A </Code-group>
<Sum>
<xsl:value-of select="sum(key('entry', ('A1', 'A2'))/Details/Value)" />
</Sum>
<Code-group> B </Code-group>
<Sum>
<xsl:value-of select="sum(key('entry', ('B1', 'B2'))/Details/Value)" />
</Sum>
</Output>
</xsl:template>
</xsl:stylesheet>
这篇关于通过在XSLT中对节点值进行硬编码对节点进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!