xml - XSL get single value from attribute -
xml - XSL get single value from attribute -
this 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 output list this, beingness expected result:
1 - root1 2 - root2 3 - root3 total number of root is: 3
i tried xsl 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 instruction "distinct-values" create list contain single value of root-x substring extracted path attributes. indeed get:
1 - root1 2 - root1 3 - root1 4 - root2 5 - root3
any help appreciated. thanks
you want xsl:key
; work 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
xml xslt xslt-1.0
Comments
Post a Comment