Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
xsl:使用多个元素对XML文件排序_Xml_Xslt_Xpath - Fatal编程技术网

xsl:使用多个元素对XML文件排序

xsl:使用多个元素对XML文件排序,xml,xslt,xpath,Xml,Xslt,Xpath,我正在尝试对XML文件中的一组记录进行排序。诀窍是我需要为不同的节点使用不同的元素进行排序。举一个最简单的例子,我想这样做:给定一个xml文件 <?xml version="1.0" encoding="utf-8" ?> <buddies> <person> <nick>Jim</nick> <last>Zulkin</last> </person> <person> <fi

我正在尝试对XML文件中的一组记录进行排序。诀窍是我需要为不同的节点使用不同的元素进行排序。举一个最简单的例子,我想这样做:给定一个xml文件

<?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>
也就是说,一个人可以按名字、姓氏和尼克的任何子集列出。 排序键是姓氏(如果有),否则是昵称(如果有),否则是名

我在这里遇到了一些困难,因为使用变量作为xsl:sort键是非常困难的

我目前最好的方法是进行两步转换:使用此样式表为每条记录添加一个特殊标记

<?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进行排序

<?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>


虽然这两步转换有效,但我想知道是否有更优雅的方法可以一次性完成

如果此模板提供了预期的结果,您可以尝试一下吗?对于您的简单示例,它给出了正确的答案,但可能有一些极端情况不起作用。如果缺少一个元素,这里使用Normalize space来删除前导空格和尾随空格

<xsl:template match="/buddies">
    <xsl:for-each select="person">
        <xsl:sort select="normalize-space(concat(last, ' ', nick, ' ', first))"/>

        <xsl:if test="first">
            <xsl:value-of select="first" />
            <xsl:text> </xsl:text>
        </xsl:if>

        <xsl:if test="nick">
            <xsl:value-of select="nick" />
            <xsl:text> </xsl:text>
        </xsl:if>

        <xsl:value-of select="last" />
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>




您可以使用
concat
XPath函数:

<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)),
                                     '&#xA;')"/>
    </xsl:template>
</xsl:stylesheet>


如果在排序键中根本不使用空格,则更简单(我认为仍然正确)。非常感谢concat()函数!顺便说一句,原来svick的变体(完全不使用空格)也可以使用。@svick在本例中是正确的,但在包含整数的元素的类似情况下,省略分隔符会导致排序错误。
fn:concat
answer的+1。我已编辑,因为您的答案输出不是所需的output@Zhenya:请注意,这在很大程度上依赖于资本化。如果由于输入而无法使用,则应使用空格分隔符串联。
<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)),
                                     '&#xA;')"/>
    </xsl:template>
</xsl:stylesheet>