XSLT 1.0 - Remove Duplicate Nodes From Variable

2019-08-28 05:32发布

问题:

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)

<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>

Expected Output:

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

Current Output:

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

The col2 values in $unique_cols should be distinct per col1 values. If unique col2 values could be selected in $cols, even better.

回答1:

Just replace:

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

with:

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

The full transformation (initially corrrected to fix a multitude of lexical errors) now becomes (also using my own file-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>

and the files are:

c:/temp/delete/test1.xml:

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

and: 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>

When the transformation is performed on any XML document (not used), the wanted, correct result is produced:

<table>

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

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

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

</table>


回答2:

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])]"/>

This appears to select unique col2 values into $cols eliminating the need for the second variable.



标签: xslt xslt-1.0