当前位置: 代码迷 >> XML/SOAP >> XSLT 截取唯一的属性值有关问题
  详细解决方案

XSLT 截取唯一的属性值有关问题

热度:794   发布时间:2012-02-28 13:06:35.0
XSLT 截取唯一的属性值问题
有如下 XML 文件:
XML code

<?xml version="1.0" encoding="UTF-8"?>
<score>
    <record n="20050001">
        <total>342</total>
    </record>

    <record n="20050002">
        <total>263</total>
    </record>

    <record n="20050003">
        <total>210</total>
    </record>

    <record n="20060009">
        <total>120</total>
    </record>

    <record n="20060010">
        <total>285</total>
    </record>

    <record n="20060011">
        <total>215</total>
    </record>

    <record n="20070001">
        <total>170</total>
    </record>

    <record n="20070002">
        <total>88</total>
    </record>

    <record n="20070003">
        <total>400</total>
    </record>

    <record n="20090006">
        <total>330</total>
    </record>

    <record n="20090007">
        <total>315</total>
    </record>
</score>



我想输出:
<select>
<option>2005</option>
<option>2006</option>
<option>2007</option>
<option>2009</option>
</select>

XSLT 应该怎样写?
关键在于提取 record 的 n 的前四位年份。数据量有近万条记录。
如果先提取所有记录的年份后再过滤,运算量很大,速度很慢,有什么好办法吗?有没有直接的办法提取唯一的年份?

------解决方案--------------------
XML code

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
<select>
    <xsl:for-each select="score/record[substring(@n,0,5)!=substring(following::record/@n,0,5)]">
        <option><xsl:value-of select="substring(@n,0,5)"/></option>
    </xsl:for-each>
</select>
    </xsl:template>
</xsl:stylesheet>