问题详见:http://community.csdn.net/Expert/topic/3325/3325780.xml?temp=.8528101
源格式:
<?xml version="1.0" encoding="gb2312" ?>
<star>
<info>
<ch>A</ch>
<name>阿杜</name>
<url>001.xml</url>
<created>2004-7-31 12:46:19</created>
</info>
<info>
<ch>C</ch>
<name>陈百强</name>
<url>002.xml</url>
<created>2004-8-31 12:46:19</created>
</info>
<info>
<ch>B</ch>
<name>学亮</name>
<url>003.xml</url>
<created>2004-6-31 12:46:19</created>
</info>
<info>
<ch>B</ch>
<name>学亮2</name>
<url>004.xml</url>
<created>2004-6-31 12:46:19</created>
</info>
<info>
<ch>D</ch>
<name>刀朗</name>
<url>005.xml</url>
<created>2004-6-31 12:46:19</created>
</info>
</star>
目标格式:
A 阿杜
B 学亮 学亮2
C 陈百强(new)
D 刀朗
说明:
The <xsl:key>
element declares a named key—that is, a name-value pair assigned to a specified element in an XML document. The key is used with the key()
function in XPath expressions to help you access the assigned elements in a complex XML document efficiently.
示例代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="http://dotnet.aspx.cc/" exclude-result-prefixes="msxsl user">
<msxsl:script language="JavaScript" implements-prefix="user"><![CDATA[
function xmlDateTime(nodelist) {
return (Date.parse(new Date()) - Date.parse(nodelist.replace(/-/g,"/"))<2*7*24*60*60*1000).toString();
}
]]>
</msxsl:script>
<xsl:key name="sortCH" match="info" use="ch"/>
<xsl:template match="/">
<xsl:for-each select="//info">
<xsl:sort order="ascending" select="ch"/>
<xsl:if test="position()=1 or ch != preceding-sibling::info[1]/ch">
<xsl:value-of select="ch"/>  
<xsl:for-each select="key('sortCH',ch)">
<xsl:value-of select="name"/>  <xsl:if test="user:xmlDateTime(string(created)) = 'true'">[new]</xsl:if>
</xsl:for-each>
<br/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>