整理我的项目清单:XSLT:如何反向输出不受内容
<item>a</item>
<item>x</item>
<item>c</item>
<item>z</item>
,我想作为输出
z
c
x
a
我在文件中没有秩序的信息和我想要扭转线条。源文件中的最后一行应该是输出中的第一行。我怎样才能解决这个问题与XSLT没有按项目的内容排序,这会给出错误的结果?
整理我的项目清单:XSLT:如何反向输出不受内容
<item>a</item>
<item>x</item>
<item>c</item>
<item>z</item>
,我想作为输出
z
c
x
a
我在文件中没有秩序的信息和我想要扭转线条。源文件中的最后一行应该是输出中的第一行。我怎样才能解决这个问题与XSLT没有按项目的内容排序,这会给出错误的结果?
XML代码:
<?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>
注:如果你使用的数据类型= “数字”,并且任意值不是数字,那些非数字值将在数字值之前排序。这意味着如果您使用order =“ascending”,则非数字值首先出现;如果您使用order =“降序”,则非数字值最后出现。
注意非数字值未被排序;它们只是按照遇到的顺序出现在输出文档中。
也,你可能会发现有用的阅读本:
不知道完整的XML是什么样子,所以我裹在<doc>
元素,使其很好地形成:
<doc>
<item>a</item>
<item>x</item>
<item>c</item>
<item>z</item>
</doc>
运行该示例XML对这个样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:call-template name="reverse">
<xsl:with-param name="item" select="doc/item[position()=last()]" />
</xsl:call-template>
</xsl:template>
<xsl:template name="reverse">
<xsl:param name="item" />
<xsl:value-of select="$item" />
<!--Adds a line feed-->
<xsl:text> </xsl:text>
<!--Move on to the next item, if we aren't at the first-->
<xsl:if test="$item/preceding-sibling::item">
<xsl:call-template name="reverse">
<xsl:with-param name="item" select="$item/preceding-sibling::item[1]" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
主要生产的要求输出:
z
c
x
a
您可能需要调整xpa以匹配您的实际XML。
我会提出了两种解决方案,XSLT:
一XSLT 1。0递归注意,该解决方案适用于任何节点集,不仅在当节点是兄弟情况:
这种转变:
<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="/*">
<xsl:call-template name="reverse">
<xsl:with-param name="pList" select="*"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="reverse">
<xsl:param name="pList"/>
<xsl:if test="$pList">
<xsl:value-of
select="concat($pList[last()], '
')"/>
<xsl:call-template name="reverse">
<xsl:with-param name="pList"
select="$pList[not(position() = last())]"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
当这个XML应用文件:
<t>
<item>a</item>
<item>x</item>
<item>c</item>
<item>z</item>
</t>
产生想要的结果:
z
c
x
a
二, XSLT 2.0溶液:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text"/>
<xsl:template match="/*">
<xsl:value-of select="reverse(*)/string(.)"
separator="
"/>
</xsl:template>
</xsl:stylesheet>
当该变换被应用在相同的XML文档,同一正确的结果被产生。
我喜欢第二个xslt2.0解决方案,但出现此错误: saxon-xslt test.xml x2.xsl xsl:value-of的第8行文件的错误:/tmp/x2.xsl: 错误(*)/ string(。):未知系统函数:反向 x2.xsl与您的解决方案 – prometoys
@prometoys相同,这意味着您运行的是仅为XSLT 1.0的Saxon版本(Pre-Saxon 7 )。从saxonica下载Saxon9 HE,或使用非撒克逊XSLT 2.0处理器,如AltovaXML。 –
@prometoys,或者您使用的是古董撒克逊,在XSLT 2.0之前开发,F&O成为W3C推荐。 –
Consider this XML input:
<?xml version="1.0" encoding="utf-8" ?>
<items>
<item>a</item>
<item>x</item>
<item>c</item>
<item>z</item>
</items>
的XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/items[1]">
<xsl:variable name="items-list" select="." />
<xsl:variable name="items-count" select="count($items-list/*)" />
<xsl:for-each select="item">
<xsl:variable name="index" select="$items-count+1 - position()"/>
<xsl:value-of select="$items-list/item[$index]"/>
<xsl:value-of select="'
'"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
而结果:
z
c
x
a
@mads汉森:感谢和重新格式化你的答案。 您对aSeptiks解决方案有何看法? – prometoys