Re: [xsl] Selecting non-duplicate nodes

Subject: Re: [xsl] Selecting non-duplicate nodes
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Wed, 12 Oct 2011 16:53:41 +0100
On 12/10/2011 16:33, Mark wrote:
I posted a similar problem last week that required a somewhat more complex answer but received no responses to it, so I have reduced my requirements and simplified the problem substantially. My question now is: how can I change this stylesheet so that for the listed input, no <FormatButtons> in the output contains more than one copy of a <Format> with the same attribute name and value? That is, where the current output is, for example:

<FormatPage souvenir-sheet="365">
<FormatButtons>
<Formats se-tenant="365"/>
<Formats se-tenant="365"/>
<Formats coupon="367"/>
<Formats coupon="368"/>
</FormatButtons>
</FormatPage>

where the <Formats se-tenant="365" appears twice. What I desire is:

<FormatPage souvenir-sheet="365">
<FormatButtons>
<Formats se-tenant="365"/>
<Formats coupon="367"/>
<Formats coupon="368"/>
</FormatButtons>
</FormatPage>

Thanks,
Mark
------------------------------------------------------
[Input file and stylesheet]
---------------------------------------------
<Input>
<Set>
<Stamp>
<CatNumbers pofis-number="105"/>
<Formats souvenir-sheet="105"/>
<Formats se-tenant="105"/>
</Stamp>
<Stamp>
<CatNumbers pofis-number="106"/>
<Formats souvenir-sheet="105"/>
<Formats se-tenant="105"/>
</Stamp>
<Stamp>
<CatNumbers pofis-number="107"/>
<Formats souvenir-sheet="105"/>
<Formats se-tenant="107"/>
</Stamp>
<Stamp>
<CatNumbers pofis-number="108"/>
<Formats souvenir-sheet="105"/>
<Formats se-tenant="107"/>
</Stamp>
</Set>
<Set>
<Stamp>
<CatNumbers pofis-number="146"/>
<Formats souvenir-sheet="146"/>
<Formats se-tenant="146"/>
</Stamp>
<Stamp>
<CatNumbers pofis-number="147"/>
<Formats souvenir-sheet="146"/>
<Formats se-tenant="146"/>
</Stamp>
<Stamp>
<CatNumbers pofis-number="148"/>
<Formats souvenir-sheet="146"/>
<Formats se-tenant="146"/>
</Stamp>
</Set>
<Set>
<Stamp>
<CatNumbers pofis-number="244"/>
<Formats coupon="244"/>
</Stamp>
<Stamp>
<CatNumbers pofis-number="245"/>
<Formats souvenir-sheet="245"/>
</Stamp>
</Set>
<Set>
<Stamp>
<CatNumbers pofis-number="365"/>
<Formats souvenir-sheet="365"/>
<Formats se-tenant="365"/>
</Stamp>
<Stamp>
<CatNumbers pofis-number="366"/>
<Formats souvenir-sheet="365"/>
<Formats se-tenant="365"/>
</Stamp>
<Stamp>
<CatNumbers pofis-number="367"/>
<Formats souvenir-sheet="365"/>
<Formats coupon="367"/>
</Stamp>
<Stamp>
<CatNumbers pofis-number="368"/>
<Formats souvenir-sheet="365"/>
<Formats coupon="368"/>
</Stamp>
</Set>
</Input>

--------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xs="http://www.w3.org/2001/XMLSchema";
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"; exclude-result-prefixes="xs
xd"
version="2.0">


<xsl:strip-space elements="*"/>
<xsl:template match="Input">
<xsl:element name="Output">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

<xsl:template match="Set">
<!-- <xsl:apply-templates mode="stamp"/> -->
<xsl:apply-templates mode="formats"/>
</xsl:template>

<xsl:template match="Stamp" mode="formats">
<xsl:apply-templates mode="formats"/>
</xsl:template>

<xsl:template match="Formats" mode="formats">
<xsl:if test="@* eq ../CatNumbers/@pofis-number">
<xsl:element name="FormatPage">
<xsl:copy-of select="@*"/>
<xsl:variable name="format" select="name(@*)"/>
<xsl:variable name="number" select="@*"/>
<xsl:element name="FormatButtons">
<xsl:for-each-group select="../../Stamp" group-by="Formats">
<xsl:for-each select="current-group()">
<xsl:if test="Formats/@*[name(.)=$format]=$number">
<Formats>
<xsl:copy-of select="Formats/@*[not(name(.)=$format)]"/>
</Formats>
</xsl:if>
</xsl:for-each>
</xsl:for-each-group>
</xsl:element>
</xsl:element>
</xsl:if>
</xsl:template>

</xsl:stylesheet


If you know that each Formats element has exactly one attribute, you can do

<xsl:for-each-group select="Formats" group-by="string-join(@*/concat(name(), '=', .))">
<xsl:copy-of select="current-group()[1]"/>
</xsl:for-each-group>


But your sample input file bears little relationship to the problem you say you want to solve, and your sample stylesheet even less relationship, so you may have succeeded in confusing me.

Michael Kay
Saxonica

Current Thread