XSL从属性中获取单个值

时间:2015-04-08 18:41:17

标签: xml xslt xslt-1.0

这是我的输入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>

但是我缺少指令“distinct-values”,比如使列表包含从路径属性中提取的ROOT-x子串的单个值。 确实这就是我得到的:

1 - ROOT1
2 - ROOT1
3 - ROOT1
4 - ROOT2
5 - ROOT3

任何帮助将不胜感激。 感谢

1 个答案:

答案 0 :(得分:1)

你想要xsl:key;这适用于XSLT 1.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