Re: [xsl] Selecting non-duplicate nodes

Subject: Re: [xsl] Selecting non-duplicate nodes
From: Brandon Ibach <brandon.ibach@xxxxxxxxxxxxxxxxxxx>
Date: Wed, 12 Oct 2011 13:37:17 -0400
The following seems to work to generate the contents of the
"FormatButtons" element, leaving everything else in your XSLT the
same.  The "select" grabs all of the Formats in Stamps having the
current Formats, the "group-by" filters out the duplicates and the
"select" on the "copy-of" filters out the current Formats.

        <xsl:for-each-group select="../../Stamp[Formats/@*[name(.) =
$format] = $number]/Formats"
                            group-by="concat(name(@*), '=', @*)">
          <xsl:copy-of select="self::*[not(@*[name(.) = $format] =
$number)]"/>
        </xsl:for-each-group>

-Brandon :)


On Wed, Oct 12, 2011 at 11:33 AM, Mark <mark@xxxxxxxxxxxx> 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

Current Thread