xmlxsltsap-pisap-xi

<xsl:sort> not working in XSLT PI mapping


I'm newbie to XSLT and I have below code which is not working for simple sort. Any help is appreciated.

<xsl:template match="ns0:MT_name">
<xsl:for-each select="name">
<xsl:sort select="name"/>
</xsl:for-each>
</xsl:template>

input is:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_name xmlns:ns0="http://example.com/sap/pi/TEST/xslt">
   <name>11</name>
   <name>88</name>
   <name>55</name>
</ns0:MT_name>

output expected:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_name xmlns:ns0="http://example.com/sap/pi/TEST/xslt">
   <name>11</name>
   <name>55</name>
   <name>88</name>
</ns0:MT_name>

Solution

  • Change <xsl:sort select="name"/> to <xsl:sort select="."/>. The current context is already name.


    Try this XSLT 1.0 stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:ns0="http://xyz.com/sap/pi/TEST/xslt">
      <xsl:output indent="yes"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="ns0:MT_name">
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates select="name">
            <xsl:sort select="." order="ascending"/>
          </xsl:apply-templates>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>