XSLT 1.0 - 从变量中删除重复节点 [英] XSLT 1.0 - Remove Duplicate Nodes From Variable
问题描述
我看过很多关于这个的帖子,但没有一个能帮助我解决我的问题.
I've seen many posts on this, but none of them have helped me figure out my problem.
Test1.xml
<table>
<row>
<col1>A</col1>
</row>
<row>
<col1>B</col1>
</row>
<row>
<col1>C</col1>
</row>
</table>
Test2.xml
<table>
<row>
<col1>A</col1>
<col2>ABC</col2>
</row>
<row>
<col1>B</col1>
<col2>ABC</col2>
</row>
<row>
<col1>A</col1>
<col2>ABC</col2>
</row>
<row>
<col1>C</col1>
<col2>ABC</col2>
</row>
<row>
<col1>A</col1>
<col2>DEF</col2>
</row>
</table>
Test.xsl (XSLT 1.0)
Test.xsl (XSLT 1.0)
<xsl:variable name="input" select="document('test1.xml')/>
<xsl:template match="/">
<xsl:apply-templates select="$input" mode="special"/>
</xsl:template>
<xsl:template match="node()|@" mode="special">
<xsl:copy>
<xsl:apply-templates select="node()|@" mode="special"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Row" mode="special">
<xsl:variable name="cols" select="document('test2.xml')/Table/Row[current()/Col1 = Col1]/Col2"/>
<xsl:variable name="unique_cols" select="$cols[not(. = preceding-sibling::*)]"/>
<!-- Debug -->
<xsl:for-each select="$unique_cols">
<xsl:copy-of select="."/>
</xsl:for-each>
----
</xsl:template>
预期输出:
<col2>ABC</col2>
<col2>DEF</col2>
----
<col2>ABC</col2>
----
<col2>ABC</col2>
电流输出:
<col2>ABC</col2>
<col2>ABC</col2>
<col2>DEF</col2>
----
<col2>ABC</col2>
----
<col2>ABC</col2>
$unique_cols 中的 col2 值应该与每个 col1 值不同.如果可以在 $cols 中选择唯一的 col2 值,那就更好了.
The col2 values in $unique_cols should be distinct per col1 values. If unique col2 values could be selected in $cols, even better.
推荐答案
我想我可能已经根据另一篇文章 (https://groups.google.com/forum/?fromgroups#!主题/microsoft.public.xsl/i8FwJUD0r8U).
I think I may have figured this out based on an answer provided on another post (https://groups.google.com/forum/?fromgroups#!topic/microsoft.public.xsl/i8FwJUD0r8U).
<xsl:variable name="cols" select=
"document('test2.xml')/table/row[current()/col1 = col1]/col2[not(. = ../preceding-sibling::*/col2[current()/col1 = ../col1])]"/>
这似乎将唯一的 col2 值选择到 $cols 中,从而无需第二个变量.
This appears to select unique col2 values into $cols eliminating the need for the second variable.
这篇关于XSLT 1.0 - 从变量中删除重复节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!