这是我的输入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>
我想得到一个这样的列表作为输出,这是我的预期结果:
1 - ROOT1
2 - ROOT2
3 - ROOT3
TOTAL NUMBER OF ROOT IS: 3
我尝试了一些xsl:
<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>
但我缺少"不同值"指令,比如让列表包含从路径属性中提取的ROOT-x子字符串的单个值。事实上,这就是我得到的:
1 - ROOT1
2 - ROOT1
3 - ROOT1
4 - ROOT2
5 - ROOT3
如有任何帮助,我们将不胜感激。感谢
您想要一个xsl:key
;这将适用于XSLT1.0
像这样:
<?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>
输出:
<!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