从xml中删除重复的子类别块项

时间:2014-10-29 13:26:22

标签: xml xslt

    <category link-id="681" link-handle="package-products" value="Package products">
        <entry id="1077" images="1" products="1" brands="1">
            <sub-category>
                <item handle="pens">Pens</item>
                <item handle="refills-pens">Refills : Pens</item>
            </sub-category>
        </entry>
        <entry id="1075" images="1" products="1" brands="1">
            <sub-category>
                <item handle="pencil">Pencil</item>
                <item handle="refills-pencil">Refills : Pencil</item>
            </sub-category>
        </entry>
        <entry id="1073" images="1" products="1" brands="1">
            <sub-category>
                <item handle="pencil">Pencil</item>
                <item handle="refills-pencil">Refills : Pencil</item>
            </sub-category>
        </entry>
        <entry id="1050" images="1" products="1" brands="1">
            <sub-category>
                <item handle="marker">Marker</item>
                <item handle="refills-marker">Refills : Marker</item>
            </sub-category>
        </entry>

我想从子类别中删除第3个块的重复以进行输出。 请帮帮我。 我希望输出为:

Pens

Refills : Pens

Pencil

Refills : Pencils

Marker

Refills : Marker

3 个答案:

答案 0 :(得分:3)

这是一个使用XSLT 2.0分组(xsl:for-each-group)的选项。 Rudramuni TP提出的选项实际上可以用作XSLT 1.0选项。

XML输入

<category link-id="681" link-handle="package-products" value="Package products">
    <entry id="1077" images="1" products="1" brands="1">
        <sub-category>
            <item handle="pens">Pens</item>
            <item handle="refills-pens">Refills : Pens</item>
        </sub-category>
    </entry>
    <entry id="1075" images="1" products="1" brands="1">
        <sub-category>
            <item handle="pencil">Pencil</item>
            <item handle="refills-pencil">Refills : Pencil</item>
        </sub-category>
    </entry>
    <entry id="1073" images="1" products="1" brands="1">
        <sub-category>
            <item handle="pencil">Pencil</item>
            <item handle="refills-pencil">Refills : Pencil</item>
        </sub-category>
    </entry>
    <entry id="1050" images="1" products="1" brands="1">
        <sub-category>
            <item handle="marker">Marker</item>
            <item handle="refills-marker">Refills : Marker</item>
        </sub-category>
    </entry>
</category>

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/*">
        <xsl:for-each-group select="entry/sub-category" group-by="item[1]">
            <xsl:apply-templates select="current-group()[1]"/>                
        </xsl:for-each-group>
    </xsl:template>

    <xsl:template match="item">
        <xsl:value-of select="concat(.,'&#xA;&#xA;')"/>
    </xsl:template>

</xsl:stylesheet>

<强>输出

Pens

Refills : Pens

Pencil

Refills : Pencil

Marker

Refills : Marker

答案 1 :(得分:1)

试试这个:(XSLT第2版)

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="kItemHandle" match="entry" use="sub-category/item"/>

<xsl:template match="node()|@*">
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>

<xsl:template match="category">
    <xsl:for-each select="entry[count(. | key('kItemHandle', sub-category/item[1])[1])=1]">
        <xsl:value-of select="sub-category/item[1]"/>
        <xsl:text>&#xa;</xsl:text>
        <xsl:value-of select="sub-category/item[2]"/><xsl:text>&#xa;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

答案 2 :(得分:0)

这适用于多个&#39; catogory&#39;:

输入XML:

<root>
<category link-id="681" link-handle="package-products" value="Package products">
    <entry id="1077" images="1" products="1" brands="1">
        <sub-category>
            <item handle="pens">Pens</item>
            <item handle="refills-pens">Refills : Pens</item>
        </sub-category>
    </entry>
    <entry id="1075" images="1" products="1" brands="1">
        <sub-category>
            <item handle="pencil">Pencil</item>
            <item handle="refills-pencil">Refills : Pencil</item>
        </sub-category>
    </entry>
    <entry id="1073" images="1" products="1" brands="1">
        <sub-category>
            <item handle="pencil">Pencil</item>
            <item handle="refills-pencil">Refills : Pencil</item>
        </sub-category>
    </entry>
    <entry id="1050" images="1" products="1" brands="1">
        <sub-category>
            <item handle="marker">Marker</item>
            <item handle="refills-marker">Refills : Marker</item>
        </sub-category>
    </entry>
</category>

<category link-id="681" link-handle="package-products" value="Package products">
    <entry id="1077" images="1" products="1" brands="1">
        <sub-category>
            <item handle="pens">Pens</item>
            <item handle="refills-pens">Refills : Pens</item>
        </sub-category>
    </entry>
    <entry id="1075" images="1" products="1" brands="1">
        <sub-category>
            <item handle="pencil">Pencil</item>
            <item handle="refills-pencil">Refills : Pencil</item>
        </sub-category>
    </entry>
    <entry id="1073" images="1" products="1" brands="1">
        <sub-category>
            <item handle="pencil">Pencil</item>
            <item handle="refills-pencil">Refills : Pencil</item>
        </sub-category>
    </entry>
    <entry id="1050" images="1" products="1" brands="1">
        <sub-category>
            <item handle="marker">Marker</item>
            <item handle="refills-marker">Refills : Marker</item>
        </sub-category>
    </entry>
    <entry id="1052" images="1" products="1" brands="1">
        <sub-category>
            <item handle="paper">Paper</item>
            <item handle="refills-paper">Refills : Paper</item>
        </sub-category>
    </entry>
</category>
</root>

XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="kItemHandle" match="entry" use="sub-category/item"/>

<xsl:template match="node()|@*">
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>

<xsl:template match="root">
    <xsl:for-each select="category/entry[count(. | key('kItemHandle', sub-category/item)[1])=1]">
        <xsl:value-of select="sub-category/item[1]"/>
        <xsl:text>&#xa;</xsl:text>
        <xsl:value-of select="sub-category/item[2]"/><xsl:text>&#xa;</xsl:text>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

输出:


  笔芯:笔杆   铅笔
  笔芯:铅笔
  标记
  笔芯:标记
  纸
  笔芯:纸张