XSLT 1.0 - 从变量中删除重复节点

时间:2012-11-02 20:42:42

标签: xslt xslt-1.0

我已经看过很多这方面的帖子,但没有一个能帮助我弄清楚我的问题。

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)

<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值,甚至更好。

2 个答案:

答案 0 :(得分:0)

只需替换

    <xsl:variable name="unique_cols" select="$cols[not(. =  preceding-sibling::*)]"/>

<强>与

    <xsl:variable name="unique_cols" select=
         "$cols[not(../col1 =  ../preceding-sibling::*/col1)]"/>

完全转换(最初纠正以修复大量词汇错误)现在变为(也使用我自己的文件-Uris):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:variable name="input" select=
      "document('file:///c:/temp/delete/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('file:///c:/temp/delete/test2.xml')
             /table
               /row[current()/col1 = col1]
                 /col2"/>
        <xsl:variable name="unique_cols" select=
             "$cols[not(../col1 =  ../preceding-sibling::*/col1)]"/>

        <!-- Debug -->
        <xsl:for-each select="$unique_cols">
            <xsl:copy-of select="."/>
        </xsl:for-each>
        ----
    </xsl:template>
</xsl:stylesheet>

,文件是:

C:/temp/delete/test1.xml:

<table>
    <row>
        <col1>A</col1>
    </row>
    <row>
        <col1>B</col1>
    </row>
    <row>
        <col1>C</col1>
    </row>
</table>

和:c:/temp/delete/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>
</table>

对任何XML文档(未使用)执行转换时,会生成所需的正确结果

<table>

   <col2>ABC</col2>
        ----

    <col2>ABC</col2>
        ----

    <col2>ABC</col2>
        ----

</table>

答案 1 :(得分:0)

我想我可能已根据另一篇文章(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])]"/>

这似乎在$ cols中选择了唯一的col2值,从而消除了对第二个变量的需求。