xsl:使用多个元素对 XML 文件进行排序 [英] xsl:sort an XML file using multiple elements
问题描述
我正在尝试对 XML 文件中的一堆记录进行排序.诀窍是我需要对不同的节点使用不同的元素进行排序.举一个最简单的例子,我想这样做:给定一个xml文件
I'm trying to sort a bunch of records in an XML file. The trick is that I need to sort using different elements for different nodes. To give a simplest example, I want to do this: given an xml file
<?xml version="1.0" encoding="utf-8" ?>
<buddies>
<person>
<nick>Jim</nick>
<last>Zulkin</last>
</person>
<person>
<first>Joe</first>
<last>Bumpkin</last>
</person>
<person>
<nick>Pumpkin</nick>
</person>
<person>
<nick>Andy</nick>
</person>
</buddies>
我想把它转换成
Andy
Joe Bumpkin
Pumpkin
Jim Zulkin
也就是说,可以按名字、姓氏和昵称的任何子集列出一个人.排序键是姓氏(如果存在),否则是昵称(如果存在),否则是名字.
That is, a person may be listed by any subset of the first name, last name and a nick. The sorting key is the last name if it's present, otherwise it's the nickname if it's present and a firstname otherwise.
我在这里遇到困难,因为使用变量作为 xsl:sort 键是 显然不允许.
I'm having difficulties here since using of variables as xsl:sort keys is apparently not allowed.
我目前最好的办法是进行两步转换:使用此样式表为每条记录添加一个特殊标签
My current best shot is to have a two-step transformation: Add a special tag to each record using this stylesheet
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" indent="yes"/>
<!-- *** convert each person record into a person2 record w/ the sorting key *** -->
<xsl:template match="/buddies">
<buddies>
<xsl:for-each select="person">
<person2>
<xsl:copy-of select="*"/>
<!-- add the sort-by tag -->
<sort-by>
<xsl:choose>
<xsl:when test="last"> <xsl:value-of select="last"/>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="nick"> <xsl:value-of select="nick"/> </xsl:when>
<xsl:otherwise> <xsl:value-of select="first"/> </xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</sort-by>
</person2>
</xsl:for-each>
</buddies>
然后对生成的xml进行排序
And then sort the resulting xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/buddies">
<xsl:apply-templates>
<xsl:sort select="sort-by"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="person2">
<xsl:value-of select="first"/>
<xsl:value-of select="nick"/>
<xsl:value-of select="last"/><xsl:text>
</xsl:text>
</xsl:template>
虽然这种两步转换有效,但我想知道是否有更优雅的一次性转换方式?
While this two-step transform works, I'm wondering if there is more elegant way of doing it in just one go?
推荐答案
可以使用 concat
XPath 函数:
You can use the concat
XPath function:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/buddies">
<xsl:apply-templates>
<xsl:sort select="concat(last,nick,first)"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="person">
<xsl:value-of select="concat(normalize-space(concat(first,
' ',
nick,
' ',
last)),
'
')"/>
</xsl:template>
</xsl:stylesheet>
这篇关于xsl:使用多个元素对 XML 文件进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!