Re: [xsl] XSLT1.0 distinct list of attributes across several nodes

Subject: Re: [xsl] XSLT1.0 distinct list of attributes across several nodes
From: "Peter Flynn peter@xxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 25 Jun 2018 22:46:04 -0000
On 25/06/18 19:52, Mark Anderson mark.anderson@xxxxxxxxx wrote:
> I'm restricted to XSLT1.0 with no extensions.

Are you allowed to use other computer utilities?

> I need to get a list of distinct attributes across a set of nodes.

I think there is a terminological problem here, because I'm not clear
what you mean by "distinct attributes".

> From the simplified XML below, the result should be 1,2,3,5 for the
> first post_press_version (25) and 1,2,3,6 for the second (26)

By deduction, what I *think* you mean is "the number attribute of hopper
elements which have content in either sequence". The simple answer is a
direct query:

$ lxprintf -e
'hopper[ancestor::post_press_version/post_press_version_id="25"][.!=""]'
"%s\n" @number test.xml | sort | uniq
1
2
3
5
$ lxprintf -e
'hopper[ancestor::post_press_version/post_press_version_id="26"][.!=""]'
"%s\n" @number test.xml | sort | uniq
1
2
3
6

The lxprintf utility uses XPath 1.0; the above solution relies on the
standard (UNIX and GNU/Linux) sort and uniq utilities which may not be
acceptable if you are required to implement the solution *entirely*
within an XSLT 1.0 script. If that is the case, then you were very close
to an answer:

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

  <xsl:output method="text"/>

  <xsl:key name="hoppers" match="hopper"
    use="concat(ancestor::post_press_version/
                post_press_version_id,'/',@number)"/>

  <xsl:template match="/">
    <xsl:apply-templates select="order/post_press_version"/>
  </xsl:template>

  <xsl:template match="post_press_version">
    <xsl:variable name="vid" select="post_press_version_id"/>
    <xsl:value-of select="$vid"/>
    <xsl:for-each
      select="hopper_allocations/descendant::hopper
              [count(.|key('hoppers',concat($vid,'/',@number))[1])=1]">
      <xsl:sort select="@number"/>
      <xsl:if test="key('hoppers',concat($vid,'/',@number))[.!='']">
        <xsl:text>,</xsl:text>
        <xsl:value-of select="@number"/>
      </xsl:if>
    </xsl:for-each>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>

</xsl:stylesheet>


///Peter
-- 
Peter Flynn | Principal Consultant | Silmaril Consultants | Cork p.p*
Ireland | b +353 86 824 5333 | b	 peter@xxxxxxxxxxx | p
blogs.silmaril.ie/peter

Current Thread