XSLT Select 对两个不同列表上的数据进行分组 [英] XSLT Select with grouping data over two differend lists
问题描述
这是另一个令人兴奋的人,也许对你来说很容易.
Here's another mindblower, maybe just easy for you.
我有两个列表,一个是将 item-ids 连接到 group-ids 的映射,第二个是具有简单值的 item 列表.我需要将项目值的数量累积到分组总数.最初这两个列表基于不同的 XML 文件.
I have two lists, one is a map that connects item-ids to group-ids, the second one is the item list with simple values. I need to accumulate the number of item-values to group-totals. Originally the two lists are based in different XML-Files.
<!-- List 1 mapping-->
<mapping>
<sub id="1" item="a" group="a">
<sub id="2" item="b" group="a">
<sub id="3" item="d" group="b">
<sub id="4" item="e" group="b">
<sub id="5" item="f" group="c">
</mapping>
<!-- List 2 items -->
<items>
<item id="a" val="OK"/>
<item id="b" val="ERROR"/>
<item id="c" val="OK"/>
<item id="d" val="OK"/>
<item id="e" val="OK"/>
<item id="f" val="OK"/>
</items>
我目前的方法:
<xsl:variable name="failed-total">
<xsl:variable name="groups-total">
<xsl:value-of select="count(mapping//sub[not(@group=preceding-sibling::sub/@group)])"/>
</xsl:variable>
<!--Selecting the unique groups of the first list:-->
<xsl:for-each select="mapping//sub[not(@group=preceding-sibling::sub/@group)]">
<xsl:variable name="failed">
<xsl:value-of select="items/item[@id=current()/@item and val='ERROR']"/>
</xsl:variable>
<!-- TODO: add value of failed to failed-total value -->
</xsl:for-each>
需要的输出:
Number of Groups: 3
Groups failed: 1
将列表 2 更改为以下内容:
Changing list 2 to the following:
<!-- List 2 items -->
<items>
<item id="a" val="ERROR"/>
<item id="b" val="ERROR"/>
<item id="c" val="OK"/>
<item id="d" val="OK"/>
<item id="e" val="OK"/>
<item id="f" val="OK"/>
</items>
那么应该输出相同,因为项目a和be在同一个组中:
Should then output the same, cause item a and be are in the same group:
Number of Groups: 3
Groups failed: 1
欢迎提供任何提示.
推荐答案
假设您提供的所有 XML 都在单个输入 XML 文档中(从您的问题中并不完全清楚),这应该可行:
Assuming all of the XML you provided is in a single input XML document (which isn't entirely clear from your question) this should work:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>
<xsl:key name="kGroup" match="mapping/sub" use="@group" />
<xsl:key name="kFailedItem" match="items/item[@val = 'ERROR']" use="@id" />
<xsl:template match="/*">
<xsl:variable name="distinctGroups"
select="mapping/sub[generate-id() =
generate-id(key('kGroup', @group)[1])]" />
<xsl:variable name="failedGroups"
select="$distinctGroups[key('kGroup', @group)
[key('kFailedItem', @item)]
]" />
<xsl:value-of select="concat('Number of Groups: ', count($distinctGroups))"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="concat('Groups Failed: ', count($failedGroups))"/>
</xsl:template>
</xsl:stylesheet>
在此输入上运行时:
<root>
<!-- List 1 mapping-->
<mapping>
<sub id="1" item="a" group="a"/>
<sub id="2" item="b" group="a"/>
<sub id="3" item="d" group="b"/>
<sub id="4" item="e" group="b"/>
<sub id="5" item="f" group="c"/>
</mapping>
<!-- List 2 items -->
<items>
<item id="a" val="OK"/>
<item id="b" val="ERROR"/>
<item id="c" val="OK"/>
<item id="d" val="OK"/>
<item id="e" val="OK"/>
<item id="f" val="OK"/>
</items>
</root>
结果是:
Number of Groups: 3
Groups Failed: 1
当 mapping
和 items
元素存储在来自不同 XML 文档的不同变量中时,您可以使用以下方法:
Here is an approach you can use when the mapping
and items
elements are stored in separate variables from different XML documents:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslt="http://exslt.org/common">
<xsl:output method="text" indent="yes"/>
<xsl:key name="kGroup" match="mapping/sub" use="@group" />
<xsl:variable name="failedItems" select="$items/items/item[@val = 'ERROR']" />
<xsl:template match="/">
<!-- Jump into the $mappings variable context -->
<xsl:apply-templates select="$mappings/mapping" />
</xsl:template>
<xsl:template match="mapping">
<xsl:variable name="distinctGroups"
select="sub[generate-id() =
generate-id(key('kGroup', @group)[1])]" />
<xsl:variable name="failedGroups"
select="$distinctGroups
[key('kGroup', @group)/@item = $failedItems/@id]" />
<xsl:value-of select="concat('Number of Groups: ', count($distinctGroups))"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="concat('Groups Failed: ', count($failedGroups))"/>
</xsl:template>
<!-- NOTE: The definitions for the four variables below are just for
demonstration purposes. In reality, I believe that your
$mappings and $items variables would be populated some
other way. -->
<xsl:variable name="mappingsNF">
<mapping>
<sub id="1" item="a" group="a"/>
<sub id="2" item="b" group="a"/>
<sub id="3" item="d" group="b"/>
<sub id="4" item="e" group="b"/>
<sub id="5" item="f" group="c"/>
</mapping>
</xsl:variable>
<xsl:variable name="mappings" select="exslt:node-set($mappingsNF)" />
<xsl:variable name="itemsNF">
<items>
<item id="a" val="OK"/>
<item id="b" val="ERROR"/>
<item id="c" val="OK"/>
<item id="d" val="OK"/>
<item id="e" val="OK"/>
<item id="f" val="OK"/>
</items>
</xsl:variable>
<xsl:variable name="items" select="exslt:node-set($itemsNF)" />
</xsl:stylesheet>
这篇关于XSLT Select 对两个不同列表上的数据进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!