split in xslt 1.0 with the delimiter & - xslt

Hello I have the tag below with & separated and I need to get each separate field in different tags to write later in separate fields in database, anyone with any examples in xslt 1.0?
<StatusMsg>
transfer-encoding=chunked&Server=nginx%2F1.13.12&MeusPedidosURL=http%3A%2F%2Fintegracao.meuspedidos.com.br%2Fapi%2Fv1%2Fclientes4118343%2F&Connection=keep-alive&MEUSPEDIDOS_QTDE_TOTAL_REGISTROS=0&Vary=Authorization%2C+Cookie&MeusPedidosID=4118343&Date=Tue%2C+31+Dec+2019+22%3A16%3A46+GMT&Content-Type=text%2Fplain
</StatusMsg>
Example of what I need ...
<MeusPedidosID>4118343</MeusPedidosID>
<MeusPedidosURL>http%3A%2F%2Fintegracao.meuspedidos.com.br%2Fapi%2Fv1%2Fclientes4118343%2F</MeusPedidosURL>

With a well-formed input such as:
XML
<StatusMsg>transfer-encoding=chunked&Server=nginx%2F1.13.12&MeusPedidosURL=http%3A%2F%2Fintegracao.meuspedidos.com.br%2Fapi%2Fv1%2Fclientes4118343%2F&Connection=keep-alive&MEUSPEDIDOS_QTDE_TOTAL_REGISTROS=0&Vary=Authorization%2C+Cookie&MeusPedidosID=4118343&Date=Tue%2C+31+Dec+2019+22%3A16%3A46+GMT&Content-Type=text%2Fplain</StatusMsg>
you can use the substring-before() and substring-after() functions to extract the wanted values - for example, the following stylesheet:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="statusMsg" select="concat(StatusMsg, '&')" />
<output>
<MeusPedidosID>
<xsl:value-of select="substring-before(substring-after($statusMsg, 'MeusPedidosID='), '&')"/>
</MeusPedidosID>
<MeusPedidosURL>
<xsl:value-of select="substring-before(substring-after($statusMsg, 'MeusPedidosURL='), '&')"/>
</MeusPedidosURL>
</output>
</xsl:template>
</xsl:stylesheet>
will produce:
Result
<?xml version="1.0" encoding="UTF-8"?>
<output>
<MeusPedidosID>4118343</MeusPedidosID>
<MeusPedidosURL>http%3A%2F%2Fintegracao.meuspedidos.com.br%2Fapi%2Fv1%2Fclientes4118343%2F</MeusPedidosURL>
</output>
If you want to extract all the fields encoded in the given string, you can use:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<output>
<xsl:call-template name="split">
<xsl:with-param name="string" select="concat(StatusMsg, '&')"/>
</xsl:call-template>
</output>
</xsl:template>
<xsl:template name="split">
<xsl:param name="string"/>
<xsl:if test="contains($string, '=')">
<xsl:element name="{substring-before($string, '=')}">
<xsl:value-of select="substring-before(substring-after($string, '='), '&')" />
</xsl:element>
<!-- recursive call -->
<xsl:call-template name="split">
<xsl:with-param name="string" select="substring-after($string, '&')" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
to get:
Result
<?xml version="1.0" encoding="UTF-8"?>
<output>
<transfer-encoding>chunked</transfer-encoding>
<Server>nginx%2F1.13.12</Server>
<MeusPedidosURL>http%3A%2F%2Fintegracao.meuspedidos.com.br%2Fapi%2Fv1%2Fclientes4118343%2F</MeusPedidosURL>
<Connection>keep-alive</Connection>
<MEUSPEDIDOS_QTDE_TOTAL_REGISTROS>0</MEUSPEDIDOS_QTDE_TOTAL_REGISTROS>
<Vary>Authorization%2C+Cookie</Vary>
<MeusPedidosID>4118343</MeusPedidosID>
<Date>Tue%2C+31+Dec+2019+22%3A16%3A46+GMT</Date>
<Content-Type>text%2Fplain</Content-Type>
</output>

Related

XSLT - extract all text but last subsection

Looking to parse out a namespace from a full class name in xml.
Data example:
<results>
<test-case name="Co.Module.Class.X">
</results>
End result (going to csv format):
,Co.Module.Class
Stylesheet:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output method="text" indent="yes" encoding="ISO-8859-1"/>
<xsl:param name="delim" select="','" />
<xsl:param name="quote" select="'"'" />
<xsl:param name="break" select="'
'" />
<xsl:template match="/">
FullTestName, Namespace
<xsl:apply-templates select="//test-case" />
</xsl:template>
<xsl:template match="test-case">
<xsl:apply-templates />
<xsl:value-of select="#name" />
<xsl:value-of select="$delim" />
<xsl:value-of select="function to go here for nameWithJustNamespace" />
<xsl:value-of select="$break" />
</xsl:template>
I understand the process would need a last index of "." to be called once, yet I'm not finding XSLT to have that function. How to best accomplish this?
To do this in pure XSLT 1.0, you need to call a named recursive template, e.g.:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/results">
<xsl:call-template name="remove-last-token">
<xsl:with-param name="text" select="test-case/#name"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="remove-last-token">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="'.'"/>
<xsl:value-of select="substring-before($text, $delimiter)"/>
<xsl:if test="contains(substring-after($text, $delimiter), $delimiter)">
<xsl:value-of select="$delimiter"/>
<xsl:call-template name="remove-last-token">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
This pure XSLT 1.0 transformation (shorter, no conditional XSLT operations, single template):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="test-case[contains(#name, '.')]">
<xsl:param name="pDotIndex" select="0"/>
<xsl:variable name="vNextToken"
select="substring-before(substring(#name, $pDotIndex+1), '.')"/>
<xsl:value-of select="concat(substring('.', 2 - ($pDotIndex > 0)),$vNextToken)"/>
<xsl:variable name="vNewDotIndex" select="$pDotIndex+string-length($vNextToken)+1"/>
<xsl:apply-templates
select="self::node()[contains(substring(#name,$vNewDotIndex+1), '.')]">
<xsl:with-param name="pDotIndex" select="$vNewDotIndex"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<results>
<test-case name="Co.Module.Class.X"/>
</results>
produces the wanted, correct result:
Co.Module.Class
Part 2
With a slight modification the following transformation produces the complete CSV:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="test-case[contains(#name, '.')]">
<xsl:param name="pDotIndex" select="0"/>
<xsl:variable name="vNextToken"
select="substring-before(substring(#name, $pDotIndex+1), '.')"/>
<xsl:value-of select="concat(substring(',', 2 - (position() > 1)),
substring('.', 2 - ($pDotIndex > 0)), $vNextToken)"/>
<xsl:variable name="vNewDotIndex" select="$pDotIndex+string-length($vNextToken)+1"/>
<xsl:apply-templates
select="self::node()[contains(substring(#name,$vNewDotIndex+1), '.')]">
<xsl:with-param name="pDotIndex" select="$vNewDotIndex"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
When applied on this XML document:
<results>
<test-case name="Co.Module.Class.X"/>
<test-case name="Co2.Module2.Class2.Y"/>
<test-case name="Co3.Module3.Class3.Z"/>
</results>
the wanted, correct (CSV) result is produced:
Co.Module.Class,Co2.Module2.Class2,Co3.Module3.Class3

XSLT 2.0 XPATH expression with variable

I'm working on a Java based xsl-transformation (XSLT 2.0, could also be XSLT 3.0 if there is a free processor for java) with different input xml files. one input file could look like this:
<?xml version="1.0" encoding="UTF-8"?>
<TEST>
<MyElement>
<CHILD>A</CHILD>
<CHILDBEZ>ABEZ</CHILDBEZ>
<NotInteresting></NotInteresting>
</MyElement>
<MyElement>
<CHILD>B</CHILD>
<CHILDBEZ>BBEZ</CHILDBEZ>
<NotInteresting2></NotInteresting2>
</MyElement>
</TEST>
I want to copy all elements but "NotInteresting" and rename the two nodes CHILD and CHILDBEZ based on two parameters that I get from a mapping file:
the xpath expression that tells me where the text of interest is placed (in this case: TEST/MyFirstElement/CHILD and TEST/MyFirstElement/CHILDBEZ)
and the names of the elements what they should have in the output file (in this case: childName and childBez)
the mapping file:
<?xml version="1.0" encoding="UTF-8"?>
<element root="TEST">
<childName>TEST/MyElement/CHILD</childName>
<childBez>TEST/MyElement/CHILDBEZ</childBez>
</element>
desired output:
<TEST>
<MyElement>
<childName>A</childName>
<childBez>ABEZ</childBez>
</MyElement>
<MyElement>
<childName>B</childName>
<childBez>BBEZ</childBez>
</MyElement>
</TEST>
what I have so far:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="2.0 "
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.apoverlag.at"
xmlns:apo="http://www.apoverlag.at">
<xsl:variable name="vars" select="document('mapping.xml')"/>
<xsl:param name="src" />
<xsl:variable name="path" xpath-default-namespace="" select="$src/path"/> <!-- = TEST/*-->
<xsl:template match="/">
<xsl:for-each select="$path">
<xsl:call-template name="buildNode">
<xsl:with-param name="currentNode" select="current()"></xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="buildNode">
<xsl:param name="currentNode" />
<xsl:element name="test">
<xsl:for-each select="$vars/element/#root">
<xsl:for-each select="$vars/element/*">
<xsl:element name="{name(.)}"> <xsl:value-of select="concat($currentNode,'/',current())" />
</xsl:element>
</xsl:for-each>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:transform>
My problem is that:
<xsl:value-of select="concat($currentNode,'/',current())" />
gives me "/TEST/MyFirstElement/CHILD" when I try it hardcoded with:
<xsl:value-of select="$currentNode/CHILD" />
I receive my desired output. Can anyone give me a hint how to solve this problem?
I would suggest a radically different approach. To simplify, I have used a mapping document with full paths (starting with the / root node):
mapping xml
<element root="TEST">
<childName>/TEST/MyElement/CHILD</childName>
<childBez>/TEST/MyElement/CHILDBEZ</childBez>
</element>
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="mapping" select="document('mapping.xml')"/>
<xsl:key name="map" match="*" use="." />
<xsl:template match="/">
<xsl:variable name="first-pass">
<xsl:apply-templates mode="first-pass"/>
</xsl:variable>
<xsl:apply-templates select="$first-pass/*"/>
</xsl:template>
<xsl:template match="*" mode="first-pass">
<xsl:param name="parent-path"/>
<xsl:variable name="path" select="concat($parent-path, '/', name())" />
<xsl:variable name="replacement" select="key('map', $path, $mapping)" />
<xsl:element name="{if ($replacement) then name($replacement) else name()}">
<xsl:attribute name="original" select="not($replacement)"/>
<xsl:apply-templates mode="first-pass">
<xsl:with-param name="parent-path" select="$path"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[#original='true' and not(descendant::*/#original='false')]"/>
</xsl:stylesheet>
Result, when applied to the provided input:
<?xml version="1.0" encoding="UTF-8"?>
<TEST>
<MyElement>
<childName>A</childName>
<childBez>ABEZ</childBez>
</MyElement>
<MyElement>
<childName>B</childName>
<childBez>BBEZ</childBez>
</MyElement>
</TEST>

find number in a string xslt

I need to be able to find the last occurrance of a number within an element.
For example,
<DATA1>States45bx3.33</DATA1>
<DATA2>States45bx33</DATA2>
From this string, I want to get the last number value in XSLT i.e 3.33 in 1st case and 33 in 2nd case.
Any help will be appreciated.
OK here's a solution in XSLT 1.0
<?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" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<output>
<xsl:for-each select="root/*">
<xsl:element name="{name()}">
<xsl:call-template name="getEndNumber">
<xsl:with-param name="input" select="normalize-space(.)"/>
<xsl:with-param name="dotIncluded" select="true()"/>
</xsl:call-template>
</xsl:element>
</xsl:for-each>
</output>
</xsl:template>
<xsl:template name="getEndNumber">
<xsl:param name="input"/>
<xsl:param name="dotIncluded"/>
<xsl:variable name="len" select="string-length($input)"/>
<xsl:choose>
<xsl:when test="substring($input, $len, 1) >= '0' and substring($input, $len, 1) <= '9'">
<xsl:call-template name="getEndNumber">
<xsl:with-param name="input" select="substring($input, 1, $len - 1)"/>
<xsl:with-param name="dotIncluded" select="$dotIncluded"/>
</xsl:call-template>
<xsl:value-of select="substring($input, $len, 1)"/>
</xsl:when>
<xsl:when test="substring($input, $len, 1) = '.' and $dotIncluded">
<xsl:call-template name="getEndNumber">
<xsl:with-param name="input" select="substring($input, 1, $len - 1)"/>
<xsl:with-param name="dotIncluded" select="false()"/>
</xsl:call-template>
<xsl:value-of select="substring($input, $len, 1)"/>
</xsl:when>
<xsl:otherwise/>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Applied to
<?xml version="1.0" encoding="UTF-8"?>
<root>
<a>States45bx3.33</a>
<b>States45bx33</b>
<c>asdfs12310.13.0sads23.23</c>
<d>asdfs12310.13.0sads23.23z</d>
<e>asdfs12310.13.0sads23.23.34</e>
</root>
the result is
<?xml version="1.0" encoding="UTF-8"?>
<output>
<a>3.33</a>
<b>33</b>
<c>23.23</c>
<d/>
<e>23.34</e>
</output>
If you are able to apply XSLT 2.0, then make use the following:
<xsl:value-of select="replace(., '.*[^.\d](\d*\.?\d+)$', '$1')"/>
This will return a number at the end of the current node, either an integer or a floating point one.
Explanation of the regex used:
(1) .* = arbitrary number of characters at the start of string
(2) [^.\d] = one character which is not a dot and not a digit
(3) (...) grouping, allowing refer-back by $1
(4) \d*.?\d+ = optional digits + optional dot + digits
(5) $ = end of string
(6) $1 refer-back to group 1
Put into an xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<output>
<xsl:for-each select="root/*">
<num>
<xsl:value-of select="replace(., '.*[^.\d](\d*\.?\d+)$', '$1')"/>
</num>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
Applied to input
<?xml version="1.0" encoding="UTF-8"?>
<root>
<number>States45bx3.33</number>
<number>States45bx33</number>
<number>asdfs12310.13.0sads23.23</number>
</root>
This leads to
<?xml version="1.0" encoding="UTF-8"?>
<output>
<num>3.33</num>
<num>33</num>
<num>23.23</num>
</output>
You could put this into an xsl:function (XSLT 2.0 required for this also), so that you can simply retrieve the number at the end by calling the function, and have elements surrounding the result done outside the function:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:custom="http://customFunctions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<output>
<xsl:for-each select="root/*">
<num>
<xsl:value-of select="custom:getNumberAtEnd(.)"/>
</num>
</xsl:for-each>
</output>
</xsl:template>
<xsl:function name="custom:getNumberAtEnd">
<xsl:param name="input"/>
<xsl:value-of select="replace($input, '.*[^.\d](\d*.?\d+)$', '$1')"/>
</xsl:function>
</xsl:stylesheet>
Remark: when there is no number at the end of the string, the result of the function call or the earlier xsl:value-of statement will be the entire input string. When you don't want this, you will have to test the string first, or when you are certain that the end of the string will always be a well-formed number, then you can replace the regex used by the one below, which simply returns anything at the end after the final non-dot, non-digit, which may be an empty string.
<xsl:value-of select="replace($input, '.*[^.\d](.*)$', '$1')"/>

Excluding namespaces from XML

My xsl file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:go="http://www.google.com"
exclude-result-prefixes="go">
<xsl:include href="SomeLibrary.xsl"/>
<xsl:template match="/">
<xsl:call-template name="SomeTemplate">
<xsl:with-param name="Element" select="'randomParam'"/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
The SomeLibrary.xsl file:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.google.com">
<xsl:template name="SomeTemplate">
<xsl:param name="Element"/>
<Blabla>
<xsl:value-of select="$Element" />
</Blabla>
</xsl:template>
</xsl:stylesheet>
Input xml: just use an empty XML. The result is this:
<?xml version="1.0" encoding="UTF-8"?>
<Blabla xmlns="http://www.google.com">
randomParam
</Blabla>
What I want is to have the "Blabla" node without a namespace. How can I remove it or make sure it doesn't get there, without modifying my "SomeLibrary.xsl"?
In case you don't want to edit the imported stylesheet code, the way to remove the namespace is with a two-pass transformation (which in XSLT 1.0 requires using an xxx:node-set() extension function):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vrtfPass1">
<xsl:call-template name="SomeTemplate">
<xsl:with-param name="Element" select="'randomParam'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="vPass1" select="ext:node-set($vrtfPass1)"/>
<xsl:apply-templates select="$vrtfPass1/node()" mode="pass2"/>
</xsl:template>
<xsl:template match="*" mode="pass2">
<xsl:element name="{name()}">
<xsl:copy-of select="#*"/>
<xsl:apply-templates mode="pass2"/>
</xsl:element>
</xsl:template>
<xsl:template name="SomeTemplate" xmlns="http://www.google.com">
<xsl:param name="Element"/>
<Blabla>
<xsl:value-of select="$Element" />
</Blabla>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on any XML document (not used), the wanted result (the default namespace removed) is produced:
<Blabla>randomParam</Blabla>
Update:
The OP has indicated in a comment that he is using Xalan 2.07.
Below is almost the same solution, but with namespace and name for the xxx:node-set() function, as used in Xalan:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="xmlns:xalan="http://xml.apache.org/xalan" exclude-result-prefixes="x" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vrtfPass1">
<xsl:call-template name="SomeTemplate">
<xsl:with-param name="Element" select="'randomParam'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="vPass1" select="x:nodeset($vrtfPass1)"/>
<xsl:apply-templates select="$vrtfPass1/node()" mode="pass2"/>
</xsl:template>
<xsl:template match="*" mode="pass2">
<xsl:element name="{name()}">
<xsl:copy-of select="#*"/>
<xsl:apply-templates mode="pass2"/>
</xsl:element>
</xsl:template>
<xsl:template name="SomeTemplate" xmlns="http://www.google.com">
<xsl:param name="Element"/>
<Blabla>
<xsl:value-of select="$Element" />
</Blabla>
</xsl:template>
</xsl:stylesheet>
Explicitly create them with:
<xsl:element name="Blabla" namespace="">...</xsl:element>
but this means fiddling with the SomeLibrary.xsl file.
add exclude-result-prefixes="go" to the stylesheet element.
and, of course, update the namespace declaration from the default one to xmlns:go="http://www.google.com"

XSLT: How to reverse output without sorting by content

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?
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()], '
')"/>
<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="
"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the same XML document, the same correct result is produced.
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
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>
</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.
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="'
'"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
And the result:
z
c
x
a