XSL Muenchian分组 - 和过滤

时间:2017-02-22 16:28:59

标签: xml xslt muenchian-grouping

场景是这样的: 我有一家商店,出售两家生产商,CompanyA和CompanyB的家具,还有一家进口商,CompanyC的进口家具。

我的文件看起来像这样

<shop>
    <product>
        <name>Chair</name>
        <colour>Green</colour>
        <producer>CompanyA</producer>
    </product>
    <product>
        <name>Chair</name>
        <colour>Green</colour>
        <producer>CompanyB</producer>
    </product>
    <product>
        <name>Chair</name>
        <colour>Blue</colour>
        <producer>CompanyA</producer>
    </product>
    <product>
        <name>Chair</name>
        <colour>Blue</colour>
        <importer>CompanyC</importer>
    </product>
    <product>
        <name>Table</name>
        <colour>Green</colour>
        <importer>CompanyC</importer>
    </product>
    <product>
        <name>Table</name>
        <colour>Green</colour>
        <producer>CompanyA</producer>
    </product>
    <product>
        <name>Table</name>
        <colour>Blue</colour>
        <producer>CompanyB</producer>
    </product>
    <product>
        <name>Table</name>
        <colour>Blue</colour>
        <importer>CompanyC</importer>
    </product>
</shop>

我想减少我的库存,所以我需要一份来自CompanyA和CompanyC的类似产品清单。我不想列出CompanyA和CompanyB的类似产品,也不想要公司B和CompanyC的类似产品清单。 期望的输出是这个

<shop>
    <product>
        <name>Chair</name>
        <colour>Blue</colour>
        <producer>CompanyA</producer>
    </product>
    <product>
        <name>Chair</name>
        <colour>Blue</colour>
        <importer>CompanyC</importer>
    </product>
    <product>
        <name>Table</name>
        <colour>Green</colour>
        <importer>CompanyC</importer>
    </product>
    <product>
        <name>Table</name>
        <colour>Green</colour>
        <producer>CompanyA</producer>
    </product>
</shop>

我已经复制粘贴了一个旧的样式表,它给了我所有类似产品的列表 - 所以我的问题是:如何过滤列表以便它只给出一对来自CompanyA而另一个来自CompanyC?

我只能使用没有扩展名的XSL 1.0

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="no"/>

<xsl:key name="duplo" match="product" use="concat(name,colour)"/>

<xsl:template match="/">
    <shop>
        <xsl:for-each select="//product[generate-id(.)=generate-id(key('duplo', concat(name,colour))[2])]">
            <xsl:for-each select="key('duplo', concat(name,colour))">
                <product>
                    <name><xsl:value-of select="name"/></name>
                    <colour><xsl:value-of select="colour"/></colour>
                    <producer><xsl:value-of select="producer"/></producer>
                    <importer><xsl:value-of select="importer"/></importer>
                </product>
            </xsl:for-each>
        </xsl:for-each>
    </shop>
</xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

执行此操作的一种方法是将现有xsl:for-each嵌套在xsl:if条件中

<xsl:if test="key('duplo', concat(name,colour))[producer = 'CompanyA'] and key('duplo', concat(name,colour))[importer = 'CompanyC']">
    <xsl:for-each select="key('duplo', concat(name,colour))[producer = 'CompanyA' or importer = 'CompanyC']">
        <product>
            <name><xsl:value-of select="name"/></name>
            <colour><xsl:value-of select="colour"/></colour>
            <producer><xsl:value-of select="producer"/></producer>
            <importer><xsl:value-of select="importer"/></importer>
        </product>
    </xsl:for-each>
</xsl:if>

您可以使用变量来减小表达式的大小。例如

<xsl:variable name="group" select="key('duplo', concat(name,colour))" />
<xsl:variable name="A" select="$group[producer = 'CompanyA']" />
<xsl:variable name="B" select="$group[importer = 'CompanyC']" />
<xsl:if test="$A and $B">
    <xsl:for-each select="$A|$B">
        <product>
            <name><xsl:value-of select="name"/></name>
            <colour><xsl:value-of select="colour"/></colour>
            <producer><xsl:value-of select="producer"/></producer>
            <importer><xsl:value-of select="importer"/></importer>
        </product>
    </xsl:for-each>
</xsl:if>