0

I need to do two things:

  1. Remove duplicate notes from source XML
  2. Generate HTML document with values from the source after the transformation in step 1 (duplicates has been removed)

I have a working solution for each of the two steps in separate XSLT-files but I cannot figure out how to combine the operations in one XSLT-file - so the output of step 1 is used as the input of step 2.

Source data (XML):

<products>

    <product>
        <price>200EUR</price>
        <size>XL</size>
        <skuid>453</skuid>
    </product>

    <product>
        <price>200EUR</price>
        <size>XL</size>
        <skuid>453</skuid>
    </product> 

</products>

Step 1 file for taking only unique products based on id (XSLT)

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

    <xsl:key name="skukey" match="product" use="skuid"/>

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

    <xsl:template match=
        "product[not(generate-id() = generate-id(key('skukey', skuid)[1]))]"
    />
</xsl:stylesheet>

Step 2 file for generating HTML (XSLT)

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

<xsl:template match="/">

    <html>
        <body>

            <xsl:for-each select="products/product">

            <p>
                <xsl:value-of select="price"/>
                <xsl:value-of select="size"/>
            </p>

                </xsl:for-each>

        </body>
    </html>
</xsl:template>
</xsl:stylesheet>
  • Maybe this could help? https://stackoverflow.com/questions/34794072/calling-a-function-in-xslt – Isolin May 16 '20 at 16:05

1 Answers1

0

Why not simply:

XSLT 1.0

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

<xsl:key name="skukey" match="product" use="skuid"/>

<xsl:template match="/products">
    <html>
        <body>
            <xsl:for-each select="product[generate-id() = generate-id(key('skukey', skuid)[1])]">
                <p>
                    <xsl:value-of select="price"/>
                    <xsl:value-of select="size"/>
                </p>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

You probably want to insert some kind of separator between price and size.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Much more elegant. I tried something along the lines of this but just couldn´t get it to work. Thank you very much. Never mind the missing separator - I just whipped up some random HTML since the actual code is more complex. – becoming-media May 17 '20 at 07:05