Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > xslt help

Reply
Thread Tools

xslt help

 
 
WideBoy
Guest
Posts: n/a
 
      03-30-2008
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/UMLataType/@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>
....
<xsltherwise/>
</xsl:choose>
</xsl:for-each>
</xsl:template>

Any help or advice would be much appreciated.

Naran
 
Reply With Quote
 
 
 
 
none
Guest
Posts: n/a
 
      03-30-2008
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.
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-31-2008
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/UMLataType/@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
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xslutput 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/
 
Reply With Quote
 
WideBoy
Guest
Posts: n/a
 
      03-31-2008
On Mar 31, 11:17 am, Martin Honnen <(E-Mail Removed)> 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/UMLataType/@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
> xmlnssl="http://www.w3.org/1999/XSL/Transform"
> version="2.0">
>
> <xslutput 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Including XSLT/XML document within a XSLT document dar_imiro@hotmail.com XML 4 12-13-2005 02:26 AM
Multiple XSLT Transforms using a Controller XSLT sneill@mxlogic.com XML 2 10-19-2005 11:00 AM
ANN: New low-cost XML Editor, XSLT Editor, XSLT Debugger, DTD/Schema Editor Stylus Studio Java 0 08-03-2004 03:53 PM
xslt alone or xslt/java for static site? ted XML 1 01-26-2004 10:41 AM
[XSLT]Passing values from Javascript to a XSLT variable Benjamin Hillsley XML 3 09-25-2003 04:50 AM



Advertisments