xslt help

Discussion in 'XML' started by WideBoy, Mar 30, 2008.

  1. WideBoy

    WideBoy Guest

    Hi,

    I would like to sort and unique a list of elements that I extract from
    an XMI file.
    My xslt script successfully sorts the data but I need help in creating
    a unique list.

    A fragment of my xslt looks like this:
    ...
    <xsl:template name="ExtractDTs">
    <xsl:for-each select="/XMI/XMI.content/UML:Model/
    UML:Namespace.ownedElement/UML:DataType/@name">
    <xsl:sort data-type="text" order="ascending"/>
    <xsl:choose>
    <xsl:when test="fn:ends-with(.,'Amount')">
    <xs:simpleType name="{.}">
    <xs:restriction base="global.Amount"/>
    </xs:simpleType>
    </xsl:when>
    <xsl:when test="fn:ends-with(.,'List')">
    <xs:simpleType name="{.}">
    <xs:restriction base="global.CV"/>
    </xs:simpleType>
    </xsl:when>
    ....
    <xsl:eek:therwise/>
    </xsl:choose>
    </xsl:for-each>
    </xsl:template>

    Any help or advice would be much appreciated.

    Naran
    WideBoy, Mar 30, 2008
    #1
    1. Advertising

  2. WideBoy

    none Guest

    There are examples of unique-results filtering available on the XSLT FAQ
    website. Start at http://www.dpawson.co.uk/xsl/sect2/sect21.html, and
    you should probably glance at both the Sorting and Sorting And Grouping
    pages since there's some overlap between them and a lot of useful tricks
    there.
    none, Mar 31, 2008
    #2
    1. Advertising

  3. WideBoy wrote:

    > I would like to sort and unique a list of elements that I extract from
    > an XMI file.
    > My xslt script successfully sorts the data but I need help in creating
    > a unique list.
    >
    > A fragment of my xslt looks like this:
    > ...
    > <xsl:template name="ExtractDTs">
    > <xsl:for-each select="/XMI/XMI.content/UML:Model/
    > UML:Namespace.ownedElement/UML:DataType/@name">
    > <xsl:sort data-type="text" order="ascending"/>
    > <xsl:choose>
    > <xsl:when test="fn:ends-with(.,'Amount')">


    ends-with suggests you are using XSLT 2.0 so there you have
    xsl:for-each-group <URL:http://www.w3.org/TR/xslt20/#xsl-for-each-group>
    as an instruction and the function distinct-values
    <URL:http://www.w3.org/TR/xpath-functions/#func-distinct-values> to help
    you to find unique values.

    Here is an example using distinct-values, assuming the XML input looks
    like this:

    <root>
    <item>1</item>
    <item>0</item>
    <item>5</item>
    <item>2</item>
    <item>1</item>
    <item>4</item>
    <item>0</item>
    </root>

    then this stylesheet

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

    <xsl:eek:utput method="text"/>

    <xsl:template match="root">
    <xsl:for-each select="distinct-values(item)">
    <xsl:sort select="." data-type="number"/>
    <xsl:value-of select="."/>
    <xsl:text>
    </xsl:text>
    </xsl:for-each>
    </xsl:template>

    </xsl:stylesheet>

    outputs

    0
    1
    2
    4
    5



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Mar 31, 2008
    #3
  4. WideBoy

    WideBoy Guest

    On Mar 31, 11:17 am, Martin Honnen <> wrote:
    > WideBoy wrote:
    > > I would like to sort and unique a list of elements that I extract from
    > > an XMI file.
    > > My xslt script successfully sorts the data but I need help in creating
    > > a unique list.

    >
    > > A fragment of my xslt looks like this:
    > > ...
    > > <xsl:template name="ExtractDTs">
    > > <xsl:for-each select="/XMI/XMI.content/UML:Model/
    > > UML:Namespace.ownedElement/UML:DataType/@name">
    > > <xsl:sort data-type="text" order="ascending"/>
    > > <xsl:choose>
    > > <xsl:when test="fn:ends-with(.,'Amount')">

    >
    > ends-with suggests you are using XSLT 2.0 so there you have
    > xsl:for-each-group <URL:http://www.w3.org/TR/xslt20/#xsl-for-each-group>
    > as an instruction and the function distinct-values
    > <URL:http://www.w3.org/TR/xpath-functions/#func-distinct-values> to help
    > you to find unique values.
    >
    > Here is an example using distinct-values, assuming the XML input looks
    > like this:
    >
    > <root>
    > <item>1</item>
    > <item>0</item>
    > <item>5</item>
    > <item>2</item>
    > <item>1</item>
    > <item>4</item>
    > <item>0</item>
    > </root>
    >
    > then this stylesheet
    >
    > <xsl:stylesheet
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > version="2.0">
    >
    > <xsl:eek:utput method="text"/>
    >
    > <xsl:template match="root">
    > <xsl:for-each select="distinct-values(item)">
    > <xsl:sort select="." data-type="number"/>
    > <xsl:value-of select="."/>
    > <xsl:text>
    </xsl:text>
    > </xsl:for-each>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    >
    > outputs
    >
    > 0
    > 1
    > 2
    > 4
    > 5
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/


    Martin,

    Thank you very much for this neat solution.

    Best regards,

    Naran
    WideBoy, Mar 31, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    634
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,658
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    610
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    704
    Henry S. Thompson
    Oct 19, 2005
  5. Replies:
    4
    Views:
    652
Loading...

Share This Page