简体   繁体   中英

<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: help is appriciated.

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

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>
<xsl:sort select="." order="ascending"/>

so complete template is

<xsl:template match="ns0:MT_name">
  <xsl:for-each select="name">
    <xsl:sort select="." order="ascending"/>
    <xsl:copy-of select="."/>
  </xsl:for-each>
</xsl:template>

I also notice in your example you have a ' mark

Your template doesn't create any output since the body of xsl:for-each consists of xsl:sort only. In order to generate the desired output, the stylesheet could look like this one:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:ns0="http://xyz.com/sap/pi/TEST/xslt">

   <xsl:template match="ns0:MT_name">
      <xsl:copy>
         <xsl:for-each select="name">
            <xsl:sort select="." data-type="number"/>
            <xsl:copy-of select="."/>
         </xsl:for-each>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM