xsl-list
[Top] [All Lists]

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

2018-06-25 17:46:10
On 25/06/18 19:52, Mark Anderson mark(_dot_)anderson(_at_)sg360(_dot_)com 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 🇮🇪
Ireland | ☎ +353 86 824 5333 | ✉ peter(_at_)silmaril(_dot_)ie | 🌍
blogs.silmaril.ie/peter
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>