Маркирование и сортировка с XSLT 1.0

Пакет Python pydash может быть хорошим выбором.

from pydash.arrays import chunk
ids = ['22', '89', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '1']
chunk_ids = chunk(ids,5)
print(chunk_ids)
# output: [['22', '89', '2', '3', '4'], ['5', '6', '7', '8', '9'], ['10', '11', '1']]

для дополнительной проверки список чанка pydash

5
задан Paul Reiners 19 June 2009 в 17:33
поделиться

2 ответа

Вот неэффективное решение чистой версии 1:

<!-- Sort the tokens -->
<xsl:template name="sortTokens">
  <xsl:param name="tokens" select="''"/>      <!-- The list of tokens -->
  <xsl:param name="separator" select="' '"/>  <!-- What character separates the tokens? -->
  <xsl:param name="pivot" select="''"/>       <!-- A pivot word used to divide the list -->
  <xsl:param name="lessThan" select="''"/>    <!-- Accumulator for tokens less than the pivot (with leading separator) -->
  <xsl:param name="moreThan" select="''"/>    <!-- Accumulator for tokens more than the pivot (with leading separator) -->
  <xsl:param name="leadWith" select="''"/>    <!-- If set, output this before sorting -->
  <xsl:param name="trailWith" select="''"/>   <!-- If set, output this after sorting -->

  <!-- The first token -->
  <xsl:variable name="firstToken" select="substring-before(concat($tokens,$separator),$separator)"/>

  <!-- Is the first token more or less than the pivot? -->
  <xsl:variable name="pivotVsFirstToken">
    <xsl:call-template name="compareStrings">
      <xsl:with-param name="a" select="$pivot"/>
      <xsl:with-param name="b" select="$firstToken"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:choose>
    <!-- No input, no output -->
    <xsl:when test="$tokens = '' and $pivot = ''"></xsl:when>

    <!-- At the outset, the first token becomes the pivot -->
    <xsl:when test="$pivot = ''">
      <xsl:value-of select="$leadWith"/>
      <xsl:call-template name="sortTokens">
        <xsl:with-param name="separator" select="$separator"/>
        <xsl:with-param name="tokens" select="substring-after($tokens,$separator)"/>
        <xsl:with-param name="pivot" select="$firstToken"/>
      </xsl:call-template>
      <xsl:value-of select="$trailWith"/>
    </xsl:when>

    <!-- When all tokens are in a bucket, output the pivot between sorted buckets -->
    <xsl:when test="$tokens = ''">
      <xsl:call-template name="sortTokens">
        <xsl:with-param name="separator" select="$separator"/>
        <xsl:with-param name="tokens" select="substring-after($lessThan,$separator)"/>
        <xsl:with-param name="trailWith" select="$separator"/>
      </xsl:call-template>
      <xsl:value-of select="$pivot"/>
      <xsl:call-template name="sortTokens">
        <xsl:with-param name="separator" select="$separator"/>
        <xsl:with-param name="tokens" select="substring-after($moreThan,$separator)"/>
        <xsl:with-param name="leadWith" select="$separator"/>
      </xsl:call-template>
    </xsl:when>

    <!-- If the first token is less than the pivot, put it in the lessThan bucket -->
    <xsl:when test="number($pivotVsFirstToken) = 1">
      <xsl:call-template name="sortTokens">
        <xsl:with-param name="separator" select="$separator"/>
        <xsl:with-param name="tokens" select="substring-after($tokens,$separator)"/>
        <xsl:with-param name="pivot" select="$pivot"/>
        <xsl:with-param name="lessThan" select="concat($separator,$firstToken,$lessThan)"/>
        <xsl:with-param name="moreThan" select="$moreThan"/>
      </xsl:call-template>
    </xsl:when>

    <!-- If the first token is more than the pivot, put it in the moreThan bucket -->
    <xsl:otherwise>
      <xsl:call-template name="sortTokens">
        <xsl:with-param name="separator" select="$separator"/>
        <xsl:with-param name="tokens" select="substring-after($tokens,$separator)"/>
        <xsl:with-param name="pivot" select="$pivot"/>
        <xsl:with-param name="lessThan" select="$lessThan"/>
        <xsl:with-param name="moreThan" select="concat($separator,$firstToken,$moreThan)"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- Quote an apostrophe -->
<xsl:variable name="apos" select="&quot;'&quot;"/>

<!-- The comparison order of the characters -->
<xsl:variable name="characterOrder" select="concat(' !&quot;#$%&amp;',$apos,'()*+,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~')"/>

<!-- Return -1 if string a is less, 1 if string b is less, or 0 if they are equal -->
<xsl:template name="compareStrings">
  <xsl:param name="a" select="''"/>
  <xsl:param name="b" select="''"/>
  <xsl:choose>
    <xsl:when test="$a = '' and $b = ''">0</xsl:when>
    <xsl:when test="$a = ''">-1</xsl:when>
    <xsl:when test="$b = ''">1</xsl:when>
    <xsl:when test="substring($a,1,1) = substring($b,1,1)">
      <xsl:call-template name="compareStrings">
        <xsl:with-param name="a" select="substring($a,2)"/>
        <xsl:with-param name="b" select="substring($b,2)"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="contains(substring-after($characterOrder,substring($a,1,1)),substring($b,1,1))">-1</xsl:when>
    <xsl:otherwise>1</xsl:otherwise>
  </xsl:choose>
</xsl:template>
4
ответ дан 15 December 2019 в 01:09
поделиться

Если ваш процессор поддерживает EXSLT, вам лучше использовать str: tokenize

Для сортировки, почему бы не использовать xsl: sort?

<xsl:template match="/">
  <xsl:variable name="tokens">
    <xsl:call-template name="tokenize">
      <xsl:with-param name="string" select="'strawberry blueberry orange raspberry lime lemon'" />
    </xsl:call-template>
  </xsl:variable>

  <xsl:for-each select="$tokens">
    <xsl:sort select="text()" />
    <xsl:value-of select="." />
    <xsl:if test="not(last())">
      <xsl:text> </xsl:text>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

Обратите внимание, что вам может понадобиться exsl: набор узлов do для итерации.

0
ответ дан 15 December 2019 в 01:09
поделиться
Другие вопросы по тегам:

Похожие вопросы: