Reputation: 1535
this is my input xml:
<ns2:resources>
<ns2:resource path="/ROOT1/path/">
<ns2:resource path="/function1/param">
....
</ns2:resource>
<ns2:resource path="/function2/param">
....
</ns2:resource>
<ns2:resource path="/function3/param">
....
</ns2:resource>
</ns2:resource>
<ns2:resource path="/ROOT1/path2/">
<ns2:resource path="/function1/param">
....
</ns2:resource>
<ns2:resource path="/function2/param">
....
</ns2:resource>
<ns2:resource path="/function3/param">
....
</ns2:resource>
</ns2:resource>
<ns2:resource path="/ROOT2/pathN/">
<ns2:resource path="/function1/param">
....
</ns2:resource>
<ns2:resource path="/function2/param">
....
</ns2:resource>
<ns2:resource path="/function3/param">
....
</ns2:resource>
</ns2:resource>
<ns2:resource path="/ROOT1/path5/">
<ns2:resource path="/function1/param">
....
</ns2:resource>
<ns2:resource path="/function2/param">
....
</ns2:resource>
<ns2:resource path="/function3/param">
....
</ns2:resource>
</ns2:resource>
<ns2:resource path="/ROOT3/pathM/">
<ns2:resource path="/function1/param">
....
</ns2:resource>
</ns2:resource>
I'd like to get as output a list like this, being my expected result:
1 - ROOT1
2 - ROOT2
3 - ROOT3
TOTAL NUMBER OF ROOT IS: 3
I tried with some xsl like this:
<ul>
<xsl:for-each xmlns:ns2="http://wadl.dev.java.net/2009/02"
select="node()/ns2:resources/ns2:resource">
<xsl:sort xmlns:ns2="http://wadl.dev.java.net/2009/02" select="@path" />
<li>
<xsl:value-of select="substring-before(substring(@path,2),'/')"/>
</li>
</xsl:for-each>
</ul>
But i'm missing the instruction "distinct-values" like to make the list to contain single value of the ROOT-x substring extracted from the path attributes. Indeed this is what i get:
1 - ROOT1
2 - ROOT1
3 - ROOT1
4 - ROOT2
5 - ROOT3
Any help would be appreciated. Thanks
Upvotes: 0
Views: 376
Reputation: 21641
You want an xsl:key
; this will work with XSLT 1.0
Like so:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:ns2="http://wadl.dev.java.net/2009/02" exclude-result-prefixes="ns2">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:key name='res' match='/ns2:resources/ns2:resource' use="substring-before(substring(@path,2), '/')" />
<xsl:template match="/ns2:resources">
<ul>
<xsl:for-each select="ns2:resource[count(. | key('res', substring-before(substring(@path,2), '/'))[1]) = 1]">
<li>
<xsl:value-of select="substring-before(substring(@path,2),'/')"/>
</li>
</xsl:for-each>
<li>
TOTAL NUMBER OF ROOT IS: <xsl:value-of select="count(ns2:resource[count(. | key('res', substring-before(substring(@path,2),'/'))[1]) = 1])" />
</li>
</ul>
</xsl:template>
</xsl:transform>
output:
<!DOCTYPE html
PUBLIC "XSLT-compat">
<ul>
<li>ROOT1</li>
<li>ROOT2</li>
<li>ROOT3</li>
<li>TOTAL NUMBER OF ROOT IS: 3</li>
</ul>
http://xsltransform.net/6qVRKwr/2
Upvotes: 1