如何根据不同的节点排序? [英] How to sort based on different nodes?

查看:35
本文介绍了如何根据不同的节点排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据多个不同的节点对 XML 文件中的标签进行排序.例如:考虑以下 XML:

I need to sort my tags in a XML file based on multiple different nodes. For example: Consider the following XML:

<root>
    <a>
        <b>12</b>
        <e>hello</e>
    </a> 
    <a>
        <b>11</b>
        <e>how</e>
    </a>
    <a>
        <c>13</c>
        <f>are</f>
    </a>
    <a>
        <b>21</b>
        <f>you</f>
    </a>
    <a>
        <d>22</d>
        <e>hello</e>
    </a>
    <a>
        <c>14</c>
        <f>hi</f>
    </a>
</root>

现在我需要从 a 内的所有节点中找到最大数量.我试过这样做:

Now I need to find the maximum number from inside all the nodes inside a. I tried doing this:

<xsl:template match="root">
    <xsl:for-each select="a">
        <xsl:sort select="b | c | d" data-type="number" order="descending"/>   <!-- this gives me error-->
            <xsl:if test="position() = 1">
                <!-- how to access my node -->
            </xsl:if>
    </xsl:for-each>
</xsl:template>

如何进行排序并从排序后的第一个节点获取值?

How can I do my sorting and get the value form the first node after sorting?

提前谢谢!!

注意:我使用的是 XSLT 1.0.

Note: I am using XSLT 1.0.

推荐答案

此样式表:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="root">
        <xsl:for-each select="a/*[string(number(.))!='NaN']">
            <xsl:sort select="." order="descending"/>
            <xsl:if test="position() = 1">
                <highest><xsl:copy-of select="."/></highest>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

当应用于上面编辑过的输入 XML 时,输出

when applied to the edited input XML above, outputs

<highest>
   <d>22</d>
</highest>

这篇关于如何根据不同的节点排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆