取所有元素不同而不重复 xslt 1.0 [英] Take all element distinct without repetition xslt 1.0
问题描述
从我的 XML 代码中,我想一次获取所有项目而不重复.我的 XML 代码是:
From My XML code I want to take all of the items at once without repetition. My Xml code is:
<NTC_LIGHTLISTPRODUCT>
<IMMUTABLE_ID>9814</IMMUTABLE_ID>
<LIGHT_DESCRIPTION_LIST>
<LIGHT_DESCRIPTION>
<LIGHT_SUPPORT_HEIGHT>198</LIGHT_SUPPORT_HEIGHT>
</LIGHT_DESCRIPTION>
<LIGHT_DESCRIPTION>
<LIGHT_SUPPORT_HEIGHT>166</LIGHT_SUPPORT_HEIGHT>
</LIGHT_DESCRIPTION>
<LIGHT_DESCRIPTION>
<LIGHT_SUPPORT_HEIGHT>122</LIGHT_SUPPORT_HEIGHT>
</LIGHT_DESCRIPTION>
<LIGHT_DESCRIPTION>
<LIGHT_SUPPORT_HEIGHT>76</LIGHT_SUPPORT_HEIGHT>
</LIGHT_DESCRIPTION>
<LIGHT_DESCRIPTION>
<LIGHT_SUPPORT_HEIGHT>31</LIGHT_SUPPORT_HEIGHT>
</LIGHT_DESCRIPTION>
<LIGHT_DESCRIPTION>
<LIGHT_SUPPORT_HEIGHT>31</LIGHT_SUPPORT_HEIGHT>
</LIGHT_DESCRIPTION>
</LIGHT_DESCRIPTION_LIST>
<NTC_LIGHTLISTPRODUCT>
我想要序列:198 166 122 76 31.
I want the sequence: 198 166 122 76 31.
我写了这段代码 xslt 1.0,但我无法得到正确的结果:
I wrote this code xslt 1.0, but I can not get a correct result:
<xsl:for-each select="LIGHT_DESCRIPTION">
<xsl:for-each select="LIGHT_SUPPORT_HEIGHT">
<xsl:if test=".=not(preceding::LIGHT_SUPPORT_HEIGHT[1][preceding::IMMUTABLE_ID=$EF])">
<span style="font-size:9pt; text-align:center; ">
<xsl:value-of select="."/>
</span>
</xsl:if>
</xsl:for-each>
EF 是当前节点的变量(不可变 ID),我用它来保持在这个节点内.
EF is a variable of current node (IMMUTABLE ID) and I use this to remain inside this node.
你"能给我更多的指示吗?谢谢
Can "you" give me more instructions? THANKS
推荐答案
在 XSLT 1.0 中对此类问题进行分组的标准方法称为Muenchian 分组"——本质上您定义了一个对节点进行分组的 键应该被视为相同",然后使用带有 generate-id
的技巧来处理每个组中的第一个节点.
The standard approach to grouping problems like this in XSLT 1.0 is called "Muenchian grouping" - essentially you define a key that groups nodes that should be treated as "the same", and then use a trick with generate-id
to process just the first node in each group.
在这种情况下,分组约束是 LIGHT_SUPPORT_HEIGHT
值本身和包含 NTC_LIGHTLISTPRODUCT
的 IMMUTABLE_ID
的值的组合:
In this case the grouping constraint is a combination of the LIGHT_SUPPORT_HEIGHT
value itself, and the value of the containing NTC_LIGHTLISTPRODUCT
's IMMUTABLE_ID
:
<xsl:key name="supportHeightKey" match="LIGHT_SUPPORT_HEIGHT"
use="concat(., '|', ancestor::NTC_LIGHTLISTPRODUCT[1]/IMMUTABLE_ID)" />
现在您可以使用
<!-- assuming the current context is LIGHT_DESCRIPTION_LIST -->
<xsl:for-each select="LIGHT_DESCRIPTION/LIGHT_SUPPORT_HEIGHT[
generate-id() = generate-id(
key('supportHeightKey', concat(., '|', current()/../IMMUTABLE_ID))[1])]">
<span style="font-size:9pt; text-align:center; ">
<xsl:value-of select="."/>
</span>
</xsl:for-each>
这篇关于取所有元素不同而不重复 xslt 1.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!