0

The scenario is this: I have a shop that sells furniture from two producers, CompanyA and CompanyB, and also imported furniture from one importer, CompanyC.

My file looks like this

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

I want to reduce my stock, so I need a list of similar products from CompanyA and CompanyC. I don't want to list similar products from CompanyA and CompanyB, nor do I want a list of similar products from CompanyB and CompanyC. Desired output is this

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

I have copy-pasted an old stylesheet that gives me a list af all similar products - so my problem is: how can I filter the list so it only gives pairs where one comes from CompanyA and the other from CompanyC?

I can only use XSL 1.0 without extensions

<?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>
Klas Blomberg
  • 115
  • 1
  • 9

1 Answers1

0

One way to do this is to nest your existing xsl:for-each in an xsl:if condition

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

You could reduce the size of the expressions using variables, perhaps. For example

<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>
Tim C
  • 70,053
  • 14
  • 74
  • 93