如何使用 XSLT 和 Xpath 对 XML 进行排序

问题描述 投票:0回答:1
xml xpath xslt
1个回答
0
投票

正确的模板是

  <xsl:template match="employees">
    <xsl:copy>
      <xsl:apply-templates select="employee">
        <xsl:sort select="@lastname"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

然后您需要确保设置了身份转换,当前版本的 XSLT 3.0 是

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="employees">
    <xsl:copy>
      <xsl:apply-templates select="employee">
        <xsl:sort select="@lastname"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.