XSLT: How to reverse output without sorting by content

青春壹個敷衍的年華 提交于 2019-12-24 16:24:43

问题


I have a list of items:

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

and I want as output

z
c
x
a

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?


回答1:


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

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




回答2:


I will present two XSLT solutions:

I. XSLT 1.0 with recursion Note that this solution works for any node-set, not only in the case when the nodes are siblings:

This transformation:

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

when applied on this XML document:

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

produces the wanted result:

z
c
x
a

II. XSLT 2.0 solution :

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

When this transformation is applied on the same XML document, the same correct result is produced.




回答3:


Not sure what the full XML looks like, so I wrapped in a <doc> element to make it well formed:

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

Running that example XML against this stylesheet:

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

Produces the requested output:

z
c
x
a

You may need to adjust the xpath to match your actual XML.




回答4:


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>

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

And the result:

z
c
x
a


来源:https://stackoverflow.com/questions/2451396/xslt-how-to-reverse-output-without-sorting-by-content

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!