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

Subject: Re: [xsl] XSLT1.0 distinct list of attributes across several nodes
From: "Mark Anderson mark.anderson@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 26 Jun 2018 21:00:24 -0000
Thanks for reply Peter, unfortunately I can't use anything other than XSL
(with MSXML), as it's a 3rd party app that invokes the transform. All we can
do is provide our own XSL

By distinct attributes, I did, in fact, mean the distinct values of the number
attribute. Apologies of that


a Segerdahl company
Mark Anderson
Director of ERP Systems
Phone: 847-419-3329
Mobile: 13125764332
Email: mark.anderson@xxxxxxxxx
www.sg360.com
-----Original Message-----
From: Peter Flynn peter@xxxxxxxxxxx
[mailto:xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx]
Sent: Monday, June 25, 2018 5:46 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] XSLT1.0 distinct list of attributes across several nodes

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

[demime 1.01d removed an attachment of type image/jpeg which had a name of image689031.jpg]

Current Thread