xslt help

W

WideBoy

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
 
M

Martin Honnen

WideBoy said:
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
 
W

WideBoy

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,

Thank you very much for this neat solution.

Best regards,

Naran
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top