Francesco Irrera
Francesco Irrera

Reputation: 473

split letter from number in a sorted list xslt 1.0

This is XML code you can see this items group sorted by letter and number:

<LIGHT_RANGE_LIST>
  <RANGE>W19</RANGE>
  <RANGE>W17</RANGE>
  <RANGE>R15</RANGE>
  <RANGE>R13</RANGE>
  <RANGE>R11</RANGE>
</LIGHT_RANGE_LIST>

This is my XSLT code:

<xsl:for-each select="RANGE">
    <span style="text-align:center;font-family:Univers Condensed; font-size:9pt; ">
        <xsl:apply-templates/>
    </span>
</xsl:for-each>

I have provisionally applied this template, obtaining this result:

W19
W17
R15
R13
R11

My ultimate goal is to have the following output:

W19
R15

For each letter, take the letter with the largest numeric value - given that it is possible to have a single letter (not two)

Upvotes: 1

Views: 281

Answers (2)

Francesco Irrera
Francesco Irrera

Reputation: 473

At the end I study your version and I apply this, that is similar but created by me. Thank you for your cooperation.

Variable name:
Lcorrente->current letter
LSuccessiva->next letter
Ncorrente->current number
Nsuccessiva->Next number

<xsl:for-each select="RANGE">

<!-- Prendo le lettere-->
<xsl:variable name="LCorrente" select="substring(.,1,1)"/>                                                                                                                          
<xsl:variable name="LSuccessiva" select="substring(preceding-sibling::RANGE[1]  [preceding::IMMUTABLE_ID=$EF],1,1)"/>

<!-- Prendo i numeri-->
<xsl:variable name="NCorrente" select="number(substring(.,2,string-length(.)-1))"/>
<xsl:variable name="NSuccessiva" select="number(substring(following-sibling::RANGE[1][preceding::IMMUTABLE_ID=$EF],2,string-length(following-sibling::RANGE[1][preceding::IMMUTABLE_ID=$EF])-1))"/>

<xsl:when test="$LCorrente!=$LSuccessiva">
    <span style="font-family:Univers Condensed; font-size:9pt; color:orange; text-align:center;">
        <xsl:value-of select="$LCorrente"/>                                                                         
    </span>     

    <span style="font-family:Univers Condensed; font-size:9pt; color:red; text-align:center; ">
        <xsl:value-of select="$NCorrente"/>
    </span>
</xsl:when>
</xsl:for-each>

Upvotes: 0

michael.hor257k
michael.hor257k

Reputation: 117102

Try something like:

<?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" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:key name="by_letter" match="RANGE" use="substring(., 1, 1)" />

<xsl:template match="/">
<div>
    <!-- for each distinct letter -->
    <xsl:for-each select="LIGHT_RANGE_LIST/RANGE[generate-id() = generate-id(key('by_letter', substring(., 1, 1))[1])]">
        <!-- process the subgroup -->
        <xsl:for-each select="key('by_letter', substring(., 1, 1))">
            <xsl:sort select="substring(., 2)" data-type="number" order="descending"/>
            <xsl:if test="position()=1">
                <span><xsl:value-of select="."/></span>
            </xsl:if>
        </xsl:for-each>
    </xsl:for-each>
</div>
</xsl:template>
</xsl:stylesheet>

See an explanation here.

Upvotes: 1

Related Questions