题
我有项的列表:
<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 CODE:强>
<?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>
注意:如果您正在使用的数据类型=“数字”,并且任何值不是数字,这些非数字值将在数值之前排序。这意味着,如果你使用的顺序=“升序”,非数值出现第一;如果使用顺序=“下降”时,非数字值最后出现。
<强>通知强>的是,非数字值没有排序;他们只是出现在它们被遇到的次序在输出文档中
此外,您可能会发现有用的阅读这样的:
其他提示
<强>我将有两个解决方案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
<强> II。 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文档应用下,的相同的正确的结果被产生。强>
不知道完整的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
您可能需要调整的XPath,以符合您实际的XML。
考虑这个XML输入:
<?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
不隶属于 StackOverflow