XSLT Custom Sort

Is it possible that XSLT will be sorted alphabetically, with 5 elements being “preferred”.

i.e. given

<teams>
<team id="142" name="Scotland" />
<team id="110" name="Liverpool" />
<team id="13" name="Manchester United" />
<team id="123" name="England" />
<team id="84" name="Chelsea" />
<team id="295" name="Wales" />
<team id="49" name="Arsenal" />
<team id="126" name="Northern Ireland" />
<team id="121" name="Republic of Ireland" />
<team id="42" name="Manchester City" />
<team id="298" name="Tottenham Hotspur" />
<team id="299" name="Bolton" />
</teams>

I need the national teams sorted first in a specific order, and the rest in alphabetical order:

<teams>
<team id="123" name="England" />
<team id="126" name="Northern Ireland" />
<team id="121" name="Republic of Ireland" />
<team id="142" name="Scotland" />
<team id="295" name="Wales" />
<team id="49" name="Arsenal" />
<team id="299" name="Bolton" />
<team id="84" name="Chelsea" />
<team id="110" name="Liverpool" />
<team id="42" name="Manchester City" />
<team id="13" name="Manchester United" />
<team id="298" name="Tottenham Hotspur" />
</teams>

I tried, but failed.

Is there a neat way to do this, or do you need to sort national teams individually and then sort, which excludes all national teams?

+3
source share
4 answers

You can do it:

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:my="http://tempuri.org"
  exclude-result-prefixes="my"
>

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

  <my:data>
    <my:nationalteams>
      <my:team id="121" /><!-- Republic of Ireland -->
      <my:team id="123" /><!-- England -->
      <my:team id="126" /><!-- Northern Ireland -->
      <my:team id="142" /><!-- Scotland -->
      <my:team id="295" /><!-- Wales -->
    </my:nationalteams>
  </my:data>

  <xsl:template match="teams">
    <xsl:copy>
      <xsl:variable name="national" select="
        document('')/*/my:data/my:nationalteams/my:team
      " />
      <!-- national teams preferred -->
      <xsl:apply-templates select="team[@id = $national/@id]">
        <xsl:sort select="@name" />
      </xsl:apply-templates>
      <!-- other teams after them -->
      <xsl:apply-templates select="team[not(@id = $national/@id)]">
        <xsl:sort select="@name" />
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="team">
    <xsl:copy-of select="." />
  </xsl:template>

</xsl:stylesheet>

The whole <my:data>can be moved to a secondary XML / config file, where you can also leave "my" namespace.

After this, one line will require a small change:

<xsl:variable name="national" select="
  document('config.xml')/data/nationalteams/team
" />

: -)

<teams>
  <team id="123" name="England" />
  <team id="126" name="Northern Ireland" />
  <team id="121" name="Republic of Ireland" />
  <team id="142" name="Scotland" />
  <team id="295" name="Wales" />
  <team id="49" name="Arsenal" />
  <team id="299" name="Bolton" />
  <team id="84" name="Chelsea" />
  <team id="110" name="Liverpool" />
  <team id="42" name="Manchester City" />
  <team id="13" name="Manchester United" />
  <team id="298" name="Tottenham Hotspur" />
</teams>
+5

, , , . xsl:sort: / , :

<?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" indent="yes"/>

  <xsl:template match="/teams">
    <teams>
      <xsl:for-each select="team">
        <xsl:sort select="not(@name = 'England' or @name='Northern Ireland'
                           or @name='Republic of Ireland' 
                           or @name='Scotland' or @name='Wales')" 
                  data-type="number"/>
        <xsl:sort select="@name"/>
        <team>
          <xsl:value-of select="@name"/>
        </team>
      </xsl:for-each>
    </teams>
  </xsl:template>
</xsl:stylesheet>

, not(). , (0 - false, 1 - ), , "false", .

+11

xsl: sort, , xsl: apply-templates xsl: sort

, , , , . , , , .

<xsl:sort select="not(CountryValue = 'Scotland')"/>
<xsl:sort select="not(CountryValue = 'India')"/>
<xsl:sort select="CountryValue"/>

:

Scotland   
Scotland 
Scotland 
India 
Afghanistan 
Afghanistan 
Afghanistan 
Afghanistan 
Afghanistan 
Afghanistan 
Albania 
Albania 
Albania 
Algeria
+1

XSLT ?

, , node, node. node, "sortname":

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="urn:schemas-microsoft-com:xslt">
    <xsl:output method="xml" />
    <xsl:template match="/teams">
        <xsl:variable name="extendedteams">
        <xsl:for-each select="team">
            <xsl:copy>
                <xsl:copy-of select="@*" />
                <xsl:attribute name="sortname">
                    <xsl:choose>
                        <xsl:when test="@name='England' or @name='Northern Ireland' or @name='Republic of Ireland' or @name='Scotland' or @name='Wales'">1</xsl:when>
                        <xsl:otherwise>2</xsl:otherwise>
                    </xsl:choose>
                    <xsl:value-of select="@name" />
                </xsl:attribute>
            </xsl:copy>
        </xsl:for-each>
        </xsl:variable>
        <xsl:copy>
        <xsl:for-each select="exsl:node-set($extendedteams)/team">
            <xsl:sort select="@sortname" />

            <xsl:copy>
                <xsl:copy-of select="@*[name() != 'sortname']" />
            </xsl:copy>
        </xsl:for-each>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

"1" "2" , .

See information on Node Installs to see which XSLT handlers support these extensions.

+1
source

Source: https://habr.com/ru/post/1715323/


All Articles