使用 XSLT 排序 [英] Sorting with XSLT

查看:29
本文介绍了使用 XSLT 排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新 - 底部的新代码

我想弄清楚如何使用排序函数从一些 XML 数据中提取最新的记录.我对使用 XSLT 非常陌生,并且遇到了很多问题.这是我的数据示例...

I'm trying to figure out how to use the sort function to pull the most recent record from some XML data. I'm very new to using XSLT and am running into a bunch of problems. Here's an example of my data...

<content date="1/13/2011 1:21:00 PM">
    <collection vo="promotion">
        <data vo="promotion" promotionid="64526" code="101P031" startdate="1/7/2011 12:00:00 AM"/>
        <data vo="promotion" promotionid="64646" code="101P046" startdate="1/9/2011 12:00:00 AM"/>
    </collection>
</content>

我想要做的是按降序排列的promotionid 对数据进行排序,然后仅通过HTML 输出最大的promotionid.这是我正在尝试的内容

What I want to do is sort the data by promotionid in decsending order and then ONLY ouput via HTML the promotionid that is greatest. Here is along the lines of what I was trying

更新 - 这是仍存在问题的最新版本代码.

<html><body>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"  encoding="UTF-8" />
    <xsl:template match="content/collection/data">
        <xsl:apply-templates>
            <xsl:sort select="promotionid" order="descending" data-type="number" />
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="content/collection">
        <xsl:value-of select="data/@promotionid" />
    </xsl:template> 
</xsl:stylesheet>
</body></html>

虽然这确实返回了结果,但我返回的是64526"而不是64646".

While this does return results what I am getting back is '64526' and NOT '64646'.

有人可以帮忙吗?我也在网上看到过一些示例,您可以在其中按多个字段进行排序.现在可能值得注意,而不是稍后询问,我们可能希望最终按 startdate 而不是promotionid 排序.我设法想出了代码来按 YYYY、MM 和 DD 划分日期,但是除了将它们用作此类选择参数之外,我什至不知道如何开始使用它,但我不知道知道这是否真的有效.

Can anyone help? Also I've seen examples online where you can sort by multiple fields. It may be worth noting now, rather then asking later, that we may want to end up sorting by startdate rather than promotionid. I have managed to come up with code to break out the date by YYYY, MM, and DD, but have no idea how I would even begin to use that aside from using those as my select paramater of the sort, but I don't know if that actually works or not.

Year
<xsl:value-of select="substring(substring-after(substring-after(data/@startdate,'/'),'/'),1,4)" />

Month
<xsl:value-of select="substring-before(data/@startdate,'/')" />

Day
<xsl:value-of select="substring-before(substring-after(data/@startdate,'/'),'/')" />

提前致谢,我对我的 XSLT 技能不够熟练表示歉意.

Thank in advance and I appologize my less than novice XSLT skills.

------------------------------------------------------

经过这里的一些帮助后,代码已更改,但仍无法按预期工作.这是代码...

After some help here the code has changed, but is still not working as intended. Here is the code...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"  encoding="UTF-8" />
    <xsl:template match="content/collection/">
            <xsl:apply-templates>
                <xsl:sort select="@promotionid" order="descending" data-type="number" />
            </xsl:apply-templates>
        </xsl:template>

 <xsl:template match="content/collection/data">
        <xsl:if test="position()=1">
                   <xsl:value-of select="@promotionid"/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

而且我仍然看到较小的输出值而不是较大的输出值.也许有另一种方法可以做到这一点而不进行排序?因为我也愿意接受这种可能性.

And I am still seeing the lesser value ouput rather than the greater. Perhaps there is another way to do this with out sorting? As I am open to that possibility as well.

1/14/11 10:37 更新*-------------------------------------------------------------------*好的,现在使用此代码确实对数据进行了排序并输出了最高的促销 ID 编号.非常感谢!

1/14/11 10:37 Update *-------------------------------------------------------------------* Okay using this code now does indeed sort the data and output the highest promotionid number. Thanks a Ton!

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="node()|@*">
      <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="collection">
      <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="data">
         <xsl:sort select="@promotionid" data-type="number" order="descending"/>
        </xsl:apply-templates>
      </xsl:copy>
    </xsl:template>
<xsl:template match="content/collection/data">
        <xsl:if test="position()=1">
                   <xsl:value-of select="@promotionid"/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

现在忽略promtionid,你能告诉我我将如何按日期降序排序吗?我尝试删除不幸的是我知道日期应该有一个静态长度,但我们无法控制我们收到的数据:-(

Ignoring the promtionid now can you show me how I would sort, descending, by JUST the date? I tried removing Unfortunately I know the dates should have a static length, but we have no control of the data we receive :-(

你也可以推荐一本书开始,真的可以更好地理解这一切吗?你帮了我很大的忙!

Also can you recommend a book to start with to really can some better understanding of all this? You've been a tremendous help!

推荐答案

<xsl:sort select="promotionid" order="descending" data-type="number"

/>

这里有一个明显的错误:promotionid 是一个属性,而不是一个元素.

There is an obvious error here: promotionid is an attribute, not an element.

解决方案:

select="@promotionid" order="descending" data-type="number" />

另一个错误:

<xsl:template match="content/collection/data">
    <xsl:apply-templates>
        <xsl:sort select="promotionid" order="descending" data-type="number" />
    </xsl:apply-templates>
</xsl:template>

和排序执行得太晚了.

The <xsl:apply-templates> and sorting is performed too-late.

你想要:

<xsl:template match="content/collection/">
            <xsl:apply-templates>
                <xsl:sort select="@promotionid" order="descending" data-type="number" />
            </xsl:apply-templates>
        </xsl:template>

    <xsl:template match="content/collection/data">
        <xsl:if test="position()=1">
                   <xsl:value-of select="@promotionid"/>
        </xsl:if>
    </xsl:template>

至于你的扩展问题:

这种转变:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="node()|@*">
      <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="collection">
      <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="data">
         <xsl:sort select="@promotionid" data-type="number" order="descending"/>
         <xsl:sort select=
          "concat(
             substring-after(substring-after(substring-before(@startdate,' ')
                                             ,'/'
                                             ),
                               '/'
                               ),
             substring-before(substring-after(substring-before(@startdate,' ')
                                             ,'/'
                                             ),
                              '/'
                            ),
             substring-before(substring-after(substring-before(@startdate,' ')
                                             ,'/'
                                             ),
                               '/'
                               )
                )"/>
        </xsl:apply-templates>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<content date="1/13/2011 1:21:00 PM">
    <collection vo="promotion">
        <data vo="promotion" promotionid="64526" code="101P031" startdate="1/7/2011 12:00:00 AM"/>
        <data vo="promotion" promotionid="64646" code="101P046" startdate="1/9/2011 12:00:00 AM"/>
    </collection>
</content>

产生想要的结果:

<content date="1/13/2011 1:21:00 PM">

   <collection vo="promotion">
      <data vo="promotion" promotionid="64646" code="101P046" startdate="1/9/2011 12:00:00 AM"/>
      <data vo="promotion" promotionid="64526" code="101P031" startdate="1/7/2011 12:00:00 AM"/>
   </collection>

</content>

但是,请注意这不能很好地处理日期组件的可变长度.最好使用定长格式:mm/dd/yyyy

However, note that this well not handle the variable length for the date components. It is better to use fixed length format: mm/dd/yyyy

这篇关于使用 XSLT 排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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