如何排序元素并存储在变量XSLT中 [英] How sort elements and store them in a variable, XSLT

查看:181
本文介绍了如何排序元素并存储在变量XSLT中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以首先对某些元素进行排序并将它们(已排序)存储在变量中。我需要参考他们认为XSLT,这就是为什么我想把它们存储在一个变量中。

I was wondering whether it's possible to sort some elements first and store them (already sorted) in a variable. I would need to refer to them thought XSLT that's why I'd like to store them in a variable.

我试图做以下的事情,但是它不似乎工作

I was trying to do the following, but it doesn't seem to work

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

<xsl:variable name="deposits">
  <xsl:for-each select="/BookingCostings/MultiDeposits">
    <xsl:sort select="substring(@DepositDate, 1, 4)" />
    <xsl:sort select="substring(@DepositDate, 6, 2)" />
    <xsl:sort select="substring(@DepositDate, 9, 2)" />
 </xsl:for-each>
</xsl:variable>

我试图通过 @DepositDate $ deposit 变量中。所以以后,我可以使用 $存款[1] 访问它们。

I was trying to sort the elements by @DepositDate in the format 'yyyy-mm-dd' and store them all in the $deposits variable. So that later, I could access them using $deposits[1].

我会感谢任何帮助和提示

I would appreciate any help and tips!

非常感谢!

推荐答案

声明,你需要做一些事情来创建新的节点。严格来说,你不是对他们进行排序,而是按照给定的顺序阅读它们。我想你需要添加一些xsl:copy命令。

Firstly, in your variable declaration, you do need to do something to create new nodes. Strictly speaking, you are not sorting them, but just reading through them in a given order. I think you need to add some sort of xsl:copy command.

<xsl:variable name="deposits"> 
  <xsl:for-each select="/BookingCostings/MultiDeposits"> 
    <xsl:sort select="substring(@DepositDate, 1, 4)" /> 
    <xsl:sort select="substring(@DepositDate, 6, 2)" /> 
    <xsl:sort select="substring(@DepositDate, 9, 2)" /> 
    <xsl:copy-of select=".|@*" />
 </xsl:for-each> 
</xsl:variable> 

创建的是一个节点集,但要访问它,您将需要使用的XSLT中的扩展功能。您使用哪一个取决于您使用的XSLT处理器。在我即将给的例子中,我正在使用微软。

What this creates is a 'node-set', but to access it you will need to make use of an extension function in XSLT. Which one you use depends on the XSLT processor you are using. In the example I am about to give, I am using the Microsoft one.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="urn:schemas-microsoft-com:xslt" version="1.0"> 

然后,要访问变量中的节点,可以执行这样的操作

Then, to access the nodes in your variable, you can do something like this

<xsl:value-of select="ms:node-set($deposits)/MultiDeposits[1]/@DepositDate" />

这是一个很好的文章阅读节点集

Here is a good article to read up on node-sets

Xml.com关于节点集的文章

这篇关于如何排序元素并存储在变量XSLT中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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