如何对元素进行排序并将它们存储在变量中,XSLT

发布于 2024-08-21 21:32:51 字数 721 浏览 6 评论 0原文

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

我试图执行以下操作,但似乎不起作用

<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>

我试图按“yyyy-mm-dd”格式按 @DepositDate 对元素进行排序,并将它们全部存储在 < code>$deposits 变量。这样以后我就可以使用 $deposits[1] 访问它们。

我将不胜感激任何帮助和提示!

多谢!

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>

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!

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

注定孤独终老 2024-08-28 21:32:51
  1. 使用 XSLT 版本 2.0,您可以使用 perform-sort 并使用 as 告诉您的变量属于 MultiDeposits 序列的类型keywords (as="element(MultiDeposits)+")
  2. 由于您的数据已经为 yyyy-mm-dd 您可以避免使用子字符串来获取日期的每个部分并使用 直接在字段上排序

使用此示例 xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<BookingCostings>
  <MultiDeposits depositDate="2001-10-09">1</MultiDeposits>
  <MultiDeposits depositDate="1999-10-09">2</MultiDeposits>
  <MultiDeposits depositDate="2010-08-09">3</MultiDeposits>
  <MultiDeposits depositDate="2010-07-09">4</MultiDeposits>
  <MultiDeposits depositDate="1998-01-01">5</MultiDeposits>
</BookingCostings>

并使用 XSLT 版本 2.0 工作表:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:template match="/">
 <html>
  <body>

  <xsl:variable name="deposits" as="element(MultiDeposits)+">
   <xsl:perform-sort select="BookingCostings/MultiDeposits">
    <xsl:sort select="@depositDate"/>
   </xsl:perform-sort>
  </xsl:variable>

  first date:<xsl:value-of select="$deposits[1]/@depositDate"/>,
  last date:<xsl:value-of select="$deposits[last()]/@depositDate"/>

  </body>
 </html>
 </xsl:template>

</xsl:stylesheet>

输出将是:

first date:1998-01-01, last date:2010-08-09
  1. Using XSLT version 2.0 you could use perform-sort and tell that your variable is of type of a sequence of MultiDeposits using the as keyword (as="element(MultiDeposits)+")
  2. Since your data is already as yyyy-mm-dd you can avoid to use the subtring to get each part of the date and use the sort directly on the field

with this sample xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<BookingCostings>
  <MultiDeposits depositDate="2001-10-09">1</MultiDeposits>
  <MultiDeposits depositDate="1999-10-09">2</MultiDeposits>
  <MultiDeposits depositDate="2010-08-09">3</MultiDeposits>
  <MultiDeposits depositDate="2010-07-09">4</MultiDeposits>
  <MultiDeposits depositDate="1998-01-01">5</MultiDeposits>
</BookingCostings>

and using the XSLT version 2.0 sheet:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:template match="/">
 <html>
  <body>

  <xsl:variable name="deposits" as="element(MultiDeposits)+">
   <xsl:perform-sort select="BookingCostings/MultiDeposits">
    <xsl:sort select="@depositDate"/>
   </xsl:perform-sort>
  </xsl:variable>

  first date:<xsl:value-of select="$deposits[1]/@depositDate"/>,
  last date:<xsl:value-of select="$deposits[last()]/@depositDate"/>

  </body>
 </html>
 </xsl:template>

</xsl:stylesheet>

the ouput will be:

first date:1998-01-01, last date:2010-08-09
旧故 2024-08-28 21:32:51

首先,在变量声明中,您确实需要执行一些操作来创建新节点。严格来说,你并不是对它们进行排序,而只是按照给定的顺序阅读它们。我认为您需要添加某种 xsl:copy 命令。

<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 处理器。在我即将给出的示例中,我使用的是 Microsoft 的。

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

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

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

这是一篇关于节点集的好文章

有关节点集的 Xml.com 文章

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> 

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 article on Node-Sets

注定孤独终老 2024-08-28 21:32:51

猜测(手头没有开发环境):

添加

结束前

Guess (don't have dev env to hand):

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

Before the closing </xsl:for-each>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文