3

我是 xslt 的新手,我有以下代码,它不适用于简单的排序:帮助已获得帮助。

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

输入是:

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

预期输出:

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

3 回答 3

2

更改<xsl:sort select="name"/><xsl:sort select="."/>。当前上下文已经是name.


试试这个 XSLT 1.0 样式表:

<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>
于 2012-05-11T14:45:12.380 回答
0
<xsl:sort select="." order="ascending"/>

所以完整的模板是

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

我还注意到在你的例子中你有一个 ' 标记

于 2012-05-11T14:44:45.743 回答
0

您的模板不会创建任何输出,因为 的正文仅xsl:for-each包含xsl:sort。为了生成所需的输出,样式表可能如下所示:

<?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>
于 2012-05-11T14:52:28.963 回答