XSL 按具有多个相似未知值的多个相似兄弟节点对节点进行分组 [英] XSL Grouping nodes by multiple similar siblings with multiple similar unknown values
问题描述
好的,我正在尝试构建一个表,但是我没有正确地执行此操作,我没有 XSL 示例,因为我尝试的任何内容都没有接近我需要的内容.(我曾尝试使用 xsl:apply-templates 循环,即使使用模式,甚至 xsl:for-each 和 key() 但无法获得正确的过滤器.
Ok I am trying to build a table, But I'm not doing this correctly, I have no XSL example as nothing I tried came close to what I need. (I have tried using xsl:apply-templates loops, even with modes, and even xsl:for-each and key() but cannot get the right filters.
这是我将使用的 XML 示例.(我使用的真实 xml 比下面这个更复杂)
Here is an example of the XML I would be using. (the real xml I am using is more complex then this one below)
<report>
<item>
<vertical>
<component>
<partname>Left Side</partname>
<parttype>Side</parttype>
<partlocation>Outside</partlocation>
<material>Wood</material>
<thickness>20mm</thickness>
<colour>White</colour>
</component>
</vertical>
<vertical>
<component>
<partname>Right Side</partname>
<parttype>Side</parttype>
<partlocation>Outside</partlocation>
<material>Wood</material>
<thickness>20mm</thickness>
<colour>White</colour>
</component>
</vertical>
<vertical>
<component>
<partname>Back</partname>
<parttype>Back</parttype>
<partlocation>Inside</partlocation>
<material>Plastic</material>
<thickness>3mm</thickness>
<colour>Black</colour>
</component>
</vertical>
</item>
</report>
所以我想做的任务是,对于每个
我需要开始制作一个表格,并且在该表格中我需要评估每个
查找有多少具有相同的
、
和
.然后我需要列出材料名称和详细信息.接下来,我需要所有具有相同
和
的
>
和
,以显示它们的
每一行.没有我可以预期的固定数量的材料,一次我可能会得到 1,另一次我可能会得到 3.而且我不会总是知道节点将包含哪些值.同样在每个
我可以有 1-3 个不同的
和
(虽然它们成对工作- 我知道这些节点的值是多少)
So the task I am wanting to do is, for-each <item>
I need to start making a table, and inside that table I need to assess each <component>
to find how many have the same <material>
, <thickness>
and <colour>
.
Then I need to list the Material name and details.
Next I need all the <components>
that have the same <parttypes>
and <partlocation>
that are of the same <material>
, <thickness>
and <colour>
, to show their <partname>
in a row for each one.
There is no fixed amount of materials I can expect, one time I might get 1, another time I could get 3. And I wont always know what values the nodes will contain.
Also in each <item>
I can have 1-3 different <parttype>
and <partlocation>
(althought they work in pairs - and I know what the values of those nodes will be)
鉴于上面的代码非常简单,这是格式化完成的示例...
Here is a sample of the formatted finish given the really simple above code...
Wood, 20mm, White
Left Side
Right Side
Plastic, 3mm, Black
Back
这个问题非常相似,但不完全...通过 xslt1 中的 xml 元素对重复节点进行 xsl 分组一个>
this question is VERY similar, but not quite... xsl grouping of repetitive nodes by xml element in xslt1
推荐答案
您可以尝试使用这个基于密钥的解决方案作为第一个想法:
You may try to use this key based solution as a first idea:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" />
<!-- Define keys -->
<xsl:key name="kmaterial" match="component"
use="concat (generate-id(../..), '|', material, '|', thickness, '|', colour)"/>
<xsl:template match="text()" />
<xsl:template match="item">
<xsl:for-each
select="vertical/component[
generate-id(.) =
generate-id(key('kmaterial', concat (generate-id(../..), '|', material, '|', thickness, '|', colour) )[1]) ]">
<xsl:variable name="this" select="."/>
<xsl:value-of select="material"/>, <xsl:value-of select="thickness"/> , <xsl:value-of select="colour"/>
<xsl:text> </xsl:text>
<xsl:for-each
select="key('kmaterial', concat (generate-id(../..), '|', $this/material, '|', $this/thickness, '|', $this/colour) )" >
<xsl:value-of select="partname"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
输出如下:
Wood, 20mm , White
Left Side
Right Side
Plastic, 3mm , Black
Back
这篇关于XSL 按具有多个相似未知值的多个相似兄弟节点对节点进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!