xsl合并元素并按日期排序 [英] xsl merge elements and sort by date

查看:117
本文介绍了xsl合并元素并按日期排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下xml

<Values>
 <New>
    <value>110</value>
    <date>2009-10-15</date>
  </New>
  <Previous>
    <value>100</value>
    <date>2010-10-15</date>
  </Previous>
  <Previous>
    <value>130</value>
    <date>2008-10-15</date>
  </Previous>
</Values>

我正在使用以下xsl

I am using the following xsl

 <xsl:variable name="mergedData">
       <xsl:for-each select="//Values/New">
             <xsl:copy-of select="."/>
       </xsl:for-each>
       <xsl:for-each select="//Values/Previous">
             <xsl:copy-of select="."/>
       </xsl:for-each>
    </xsl:variable>


<xsl:for-each select="msxsl:node-set($mergedData)">
     <xsl:sort order="descending" select="substring(date,1,4)"/>
     <xsl:sort order="descending" select="substring(date,6,2)"/>
     <xsl:sort order="descending" select="substring(date,9,2)"/>
         <xsl:if test="position()=1">
              <xsl:value-of select="."/>
          </xsl:if>
</xsl:for-each>

然后我得到以下信息.

110 2009-10-15 100 2010-10-15 130 2008-10-15

它似乎没有按日期排序的功能,并且给了我大量的代码,我需要按日期对它们进行排序,并且能够操作数据,以便将它们放在表行中.

It does no seems to sort by date and its giving me back a lump of code I need to be sorted by date and been able to manipulate data so I can put them in table rows.

喜欢这个.

110 2009-10-15 
100 2010-10-15 
130 2008-10-15

推荐答案

似乎没有按日期和 它给了我一大堆代码 需要按日期排序并 能够操纵数据,所以我可以放 它们放在表格行中.

It does no seems to sort by date and its giving me back a lump of code I need to be sorted by date and been able to manipulate data so I can put them in table rows.

喜欢这个.

110 2009-10-15  
100 2010-10-15  
130 2008-10-15

,代码(如果正确的话)将输出最大为dateNewPrevious元素之一的字符串值.

No, the code (if it were correct) would output the string value of one of the New or Previous elements with maximum date.

这是您代码中的主要问题:

<xsl:for-each select="msxsl:node-set($mergedData)"> 

msxsl:node-set()扩展功能将返回文档树-不是顶部元素或XML片段.换句话说,它返回此文档树的根节点:/.

the msxsl:node-set() extension function returns a document tree -- not a top element or an XML fragment. To put it in other words, it returns the root node: / of this document tree.

因此,上面的<xsl:for-each>仅选择单个节点,并且该节点具有仅是NewPrevious元素的子级.因此,没有排序的方法,因为对单个节点进行排序的结果始终是同一节点.

Therefore, the <xsl:for-each> above selects a single node only, and this node has children that are only New or Previous elements. Therefore, there is no sort, because the result of sorting a single node is always this same node.

然后在代码后面:

<xsl:value-of select="."/>

因为.是临时树的根节点,所以上面的xslt指令会生成整个临时树的字符串值-即,按文档顺序串联此临时树中所有文本节点.这正是您抱怨得到的.

Because . is the root node of the temporary tree, the above xslt instruction produces the string value of the whole temporary tree -- that is, the concatenation, in document order, of all text nodes in this temporary tree. This is exactly what you complain of getting.

解决方案:

替换:

<xsl:for-each select="msxsl:node-set($mergedData)"> 

使用:

<xsl:for-each select="msxsl:node-set($mergedData)/*"> 

现在,xsl:for-each的select属性可以很明显地选择树中的所有NewPrevious顶部元素.

Now, the select attribute of xsl:for-each selects all New and Previous top elements in the tree, as obviously was desired.

这篇关于xsl合并元素并按日期排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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