使用 XSL 按字母顺序对 XML 节点进行排序 [英] Sort XML nodes in alphabetical order using XSL

查看:21
本文介绍了使用 XSL 按字母顺序对 XML 节点进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何使用 XSL 按姓氏的字母顺序对员工的 XML 列表进行排序.现在它只是按照与 XML 中相同的顺序显示 XML 信息.我不认为我完全理解如何使用 <xsl:sort> 函数,因为我是 XSL 的新手.我还尝试将 order-by="+ Lastname"<xsl:for-each> 一起放入,但我也无法使其正常工作.

I am trying to figure out how to sort the XML list of employees alphabetically by lastname using XSL. Right now it just displays the XML information in the same order as it is in the XML. I don't think I fully understand how to use the <xsl:sort> function as I am new to XSL. I also tried putting order-by="+ Lastname" in with <xsl:for-each> and I couldn't get that to work either.

这是我的 xml:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="Company1.xsl"?>
<Company>
    <Employee>
        <Firstname>John</Firstname>
        <Lastname>Smith</Lastname>
        <ssn>635-35-7463</ssn>
        <doh>February 3, 2011</doh>
        <Age>34</Age>
    </Employee>
    <Employee>
        <Firstname>Brad</Firstname>
        <Lastname>Roberts</Lastname>
        <ssn>789-65-4568</ssn>
        <doh>February 13, 2012</doh>
        <Age>25</Age>
    </Employee>
    <Employee>
        <Firstname>Karen</Firstname>
        <Lastname>Smith</Lastname>
        <ssn>369-12-7415</ssn>
        <doh>March 24, 2011</doh>
        <Age>28</Age>
    </Employee>
    <Employee>
        <Firstname>Eli</Firstname>
        <Lastname>Smith</Lastname>
        <ssn>489-32-8525</ssn>
        <doh>September 14, 2010</doh>
        <Age>38</Age>
    </Employee>
    <Employee>
        <Firstname>Bill</Firstname>
        <Lastname>Joel</Lastname>
        <ssn>689-67-7634</ssn>
        <doh>February 29, 2012</doh>
        <Age>24</Age>
    </Employee>
    <Employee>
        <Firstname>Kelly</Firstname>
        <Lastname>Greene</Lastname>
        <ssn>927-82-6873</ssn>
        <doh>December 3, 2010</doh>
        <Age>34</Age>
    </Employee>
</Company>

这是我的 XSL:

<?xml version="1.0" ?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
    <HTML>
    <HEAD>
    <TITLE>Company Employees</TITLE> 
    </HEAD>
    <BODY>
    <H2>Company Employees</H2> 

    <xsl:for-each select="Company/Employee">
        <xsl:sort select="Employee/Lastname" data-type="text" order="ascending"/>
        <xsl:sort select="Employee/Firstname" data-type="text" order="ascending"/>

        <SPAN STYLE="font-weight:bold">FirstName: </SPAN>
            <xsl:value-of select="Lastname" />
        <BR />
        <SPAN STYLE="font-weight:bold">LastName: </SPAN>
            <xsl:value-of select="Firstname" />
        <BR /> 
        <SPAN STYLE="font-weight:bold">SSN: </SPAN> 
        <xsl:value-of select="ssn" /> 
        <BR /> 
        <SPAN STYLE="font-weight:bold">Date of Hire: </SPAN> 
        <xsl:value-of select="doh" /> 
        <BR /> 
        <SPAN STYLE="font-weight:bold">Age: </SPAN> 
        <xsl:value-of select="Age" /> 
        <P/>
    </xsl:for-each>
    </BODY>
    </HTML>
    </xsl:template>
</xsl:stylesheet>

推荐答案

一目了然,

    <xsl:sort select="Employee/Lastname" data-type="text" order="ascending"/>
    <xsl:sort select="Employee/Firstname" data-type="text" order="ascending"/>

应该是

    <xsl:sort select="Lastname" data-type="text" order="ascending"/>
    <xsl:sort select="Firstname" data-type="text" order="ascending"/>

for-eachselect 设置上下文节点,因此根据 Employee 节点计算表达式.

for-each sets the context node for the select, so the expression is evaluated against the Employee nodes.

另外,textascending 是默认值,所以你可以直接写

Also, text and ascending are defaults, so you could just write

    <xsl:sort select="Lastname"/>
    <xsl:sort select="Firstname"/>

这篇关于使用 XSL 按字母顺序对 XML 节点进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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