XSLT:如何在不按内容排序的情况下反转输出 [英] XSLT: How to reverse output without sorting by content

查看:43
本文介绍了XSLT:如何在不按内容排序的情况下反转输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目清单:

<item>a</item>
<item>x</item>
<item>c</item>
<item>z</item>

我想要作为输出

z
c
x
a

文件中没有订单信息,我只想反转行.源文件中的最后一行应该是输出中的第一行.如何在不按项目内容排序的情况下使用 XSLT 解决此问题,这会给出错误的结果?

I have no order information in the file and I just want to reverse the lines. The last line in the source file should be first line in the output. How can I solve this problem with XSLT without sorting by the content of the items, which would give the wrong result?

推荐答案

XML CODE:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<device>
<element>a</element>
<element>x</element>
<element>c</element>
<element>z</element>
</device>

XSLT 代码:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="//device">
<xsl:for-each select="element">

<xsl:sort select="position()" data-type="number" order="descending"/>

<xsl:text> </xsl:text>
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>

注意:如果您使用的是 data-type="number",并且任何值都不是数字,那么这些非数字值将排在数字值之前.这意味着如果您使用 order="ascending",则首先出现非数字值;如果使用 order="descending",则非数字值最后出现.

note: if you're using data-type="number", and any of the values aren't numbers, those non-numeric values will sort before the numeric values. That means if you're using order="ascending", the non-numeric values appear first; if you use order="descending", the non-numeric values appear last.

注意非数字值未排序;它们只是按照它们出现的顺序出现在输出文档中.

Notice that the non-numeric values were not sorted; they simply appear in the output document in the order in which they were encountered.

此外,您可能会发现阅读此内容很有用:

also, you may find usefull to read this:

http://docstore.mik.ua/orelly/xml/xslt/ch06_01.htm

这篇关于XSLT:如何在不按内容排序的情况下反转输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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