2010-03-16 17 views
1

整理我的项目清单:XSLT:如何反向输出不受内容

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

,我想作为输出

z 
c 
x 
a 

我在文件中没有秩序的信息和我想要扭转线条。源文件中的最后一行应该是输出中的第一行。我怎样才能解决这个问题与XSLT没有按项目的内容排序,这会给出错误的结果?

+0

@mads汉森:感谢和重新格式化你的答案。 您对aSeptiks解决方案有何看法? – prometoys

回答

1

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 =“降序”,则非数字值最后出现。

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

也,你可能会发现有用的阅读本:

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

+0

这一个寻找我容易,只是工程。这个有问题吗?我认为有趣的是。其他解决方案的优点是什么? – prometoys

+1

但是你自己以这样的方式定义了问题,你不想分类! '我怎样才能解决这个问题与XSLT没有排序' –

+0

嗨, 我不想排序的内容,没错,但我没有与xsl:排序本身的问题。我明白这一点,它是按“行号”排序 - 使用position() - 但不是数值本身。我试着用我们的数据和xsl:与位置排序完美 – prometoys

1

不知道完整的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>&#10;</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。

3

我会提出了两种解决方案,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()], '&#xA;')"/> 

     <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="&#xA;"/> 
    </xsl:template> 
</xsl:stylesheet> 

当该变换被应用在相同的XML文档同一正确的结果被产生。

+0

我喜欢第二个xslt2.0解决方案,但出现此错误: saxon-xslt test.xml x2.xsl xsl:value-of的第8行文件的错误:/tmp/x2.xsl: 错误(*)/ string(。):未知系统函数:反向 x2.xsl与您的解决方案 – prometoys

+0

@prometoys相同,这意味着您运行的是仅为XSLT 1.0的Saxon版本(Pre-Saxon 7 )。从saxonica下载Saxon9 HE,或使用非撒克逊XSLT 2.0处理器,如AltovaXML。 –

+0

@prometoys,或者您使用的是古董撒克逊,在XSLT 2.0之前开发,F&O成为W3C推荐。 –

-1

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="'&#x0a;'"/> 
     </xsl:for-each> 

    </xsl:template> 
</xsl:stylesheet> 

而结果:

z 
c 
x 
a