众所周知,我们可以使用xslt对xml数据进行格式化输出,xslt中有一种for-each标记可以实现对xml中重复标记的循环遍历,如果你对xslt还不太了解,可以去W3school网站上了解下。使用for-each标记可以非常方便地实现节点的遍历,而且在遍历的同时还可以对节点进行排序。
例如我们有这样一个xml文本:
< Users >
< User >
< Name > Jaxu </ Name >
< ID > 1 </ ID >
< Professional > .NET,PHP Design </ Professional >
</ User >
< User >
< Name > Bruce </ Name >
< ID > 2 </ ID >
< Professional > Project Manager </ Professional >
</ User >
< User >
< Name > Tony </ Name >
< ID > 3 </ ID >
< Professional > Developer </ Professional >
</ User >
< User >
< Name > John </ Name >
< ID > 4 </ ID >
< Professional > Dev lead </ Professional >
</ User >
< User >
< Name > Eric </ Name >
< ID > 5 </ ID >
< Professional > Tester </ Professional >
</ User >
< User >
< Name > Owen </ Name >
< ID > 6 </ ID >
< Professional > Designer </ Professional >
</ User >
< User >
< Name > Bruce </ Name >
< ID > 7 </ ID >
< Professional > Project Manager </ Professional >
</ User >
</ Users >
按照Name升序,ID降序进行排序输出,下面是对应的xslt代码:
< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl ="urn:schemas-microsoft-com:xslt" exclude-result-prefixes ="msxsl"
>
< xsl:output method ="html" indent ="yes" />
< xsl:template match ="*" >
< table border ="1" cellpadding ="2" cellspacing ="4" width ="100%" >
< xsl:for-each select ="//User" >
< xsl:sort select ="Name" order ="ascending" />
< xsl:sort select ="ID" order ="descending" />
< tr >
< td >
< div >
ID: < xsl:value-of select ="ID" />
</ div >
< div >
Name: < xsl:value-of select ="Name" />
</ div >
< div >
Professional: < xsl:value-of select ="Professional" />
</ div >
</ td >
</ tr >
</ xsl:for-each >
</ table >
</ xsl:template >
</ xsl:stylesheet >
下面是输出的结果:
ID: 7
Name: Bruce
Professional: Project Manager
|
ID: 2
Name: Bruce
Professional: Project Manager
|
ID: 5
Name: Eric
Professional: Tester
|
ID: 1
Name: Jaxu
Professional: .NET,PHP Design
|
ID: 4
Name: John
Professional: Dev lead
|
ID: 6
Name: Owen
Professional: Designer
|
ID: 3
Name: Tony
Professional: Developer
|
xsl:sort语句可以对于用xsl:for-each或xsl:apply-templates匹配的节点进行排序,xsl:sort语句必须写在它所应用的标记的第一行(之前不允许有其它任何标记),如:
按大小写排序
<xsl:sort case-order="upper-first" select="@id"/> 以id为关键字按大写优先排序
<xsl:sort case-order="lower-first" select="@id"/> 以id为关键字按小写优先排序
按字母顺序排序
<xsl:sort order="ascending" select="@id "/> 以id为关键字按字母升序排序
<xsl:sort order="descending" select="@id "/> 以id为关键字按字母降序排序
按数据类型排序
<xsl:sort data-type="text" select="@id"/> 以id为关键字按文本类型排序,如对于一组id数据101,2,44,305 来说,排序结果是101,2,305,44
<xsl:sort data-type="number" select="@id"/> 以id为关键字按数据类型排序,上面一组数据的排序结果是2,44,101,305
另外,我们还可以在for-each语句中使用position()函数实现节点的位置排序,如<xsl:sort select="position()" order="descending"/>。这里还有一种方法可以实现节点的位置倒序排序,没有使用xsl:sort语句,完全按照position()函数的位置来定位节点。
< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl ="urn:schemas-microsoft-com:xslt" exclude-result-prefixes ="msxsl"
>
< xsl:output method ="html" indent ="yes" />
< xsl:template match ="*" >
< table border ="1" cellpadding ="2" cellspacing ="4" width ="100%" >
< xsl:variable name ="countUser" select ="count(//User)" />
< xsl:for-each select ="//User" >
< xsl:variable name ="pos" select ="position()" />
< tr >
< td >
< div >
ID: < xsl:value-of select ="../User[position() = $countUser - $pos + 1]/ID" />
</ div >
< div >
Name: < xsl:value-of select ="../User[position() = $countUser - $pos + 1]/Name" />
</ div >
< div >
Professional: < xsl:value-of select ="../User[position() = $countUser - $pos + 1]/Professional" />
</ div >
</ td >
</ tr >
</ xsl:for-each >
</ table >
</ xsl:template >
</ xsl:stylesheet >
xslt在编写方面还有很多的技巧,需要我们在使用的过程中不断去发现,而且最新的xslt 2.0标准中还增加了很多新的属性和标记,今后我们就可以使用xslt编写出更多逻辑复杂的代码了。