0

This is my XML:

<SECTION_CONTENT_LIST>
    <SECTION_CONTENT_LIST_ITEM>
        <NTC_LIGHTLISTPRODUCT>
            <REGION>10-Mediterraneo Occidentale - Francia</REGION>
            <ITA_LIGHT_NUMBER>0840</ITA_LIGHT_NUMBER>
        </NTC_LIGHTLISTPRODUCT>
    </SECTION_CONTENT_LIST_ITEM>
    <SECTION_CONTENT_LIST_ITEM>
        <NTC_LIGHTLISTPRODUCT>
            <REGION>10-Mediterraneo Occidentale - Francia</REGION>
            <ITA_LIGHT_NUMBER>0843</ITA_LIGHT_NUMBER>
        </NTC_LIGHTLISTPRODUCT>
    </SECTION_CONTENT_LIST_ITEM>        
    <SECTION_CONTENT_LIST_ITEM>
        <NTC_LIGHTLISTPRODUCT>
            <REGION>10-Mediterraneo Occidentale - Francia</REGION>
            <ITA_LIGHT_NUMBER>0850</ITA_LIGHT_NUMBER>
        </NTC_LIGHTLISTPRODUCT>
    </SECTION_CONTENT_LIST_ITEM>        
    <SECTION_CONTENT_LIST_ITEM>
        <NTC_LIGHTLISTPRODUCT>
            <REGION>16-Mar Tirreno - Francia (Corsica)</REGION>
            <ITA_LIGHT_NUMBER>0906</ITA_LIGHT_NUMBER>
        </NTC_LIGHTLISTPRODUCT>
    </SECTION_CONTENT_LIST_ITEM>
    <SECTION_CONTENT_LIST_ITEM>
        <NTC_LIGHTLISTPRODUCT>
            <REGION>16-Mar Tirreno - Francia (Corsica)</REGION>
            <ITA_LIGHT_NUMBER>0922.15</ITA_LIGHT_NUMBER>
        </NTC_LIGHTLISTPRODUCT>
    </SECTION_CONTENT_LIST_ITEM>
    <SECTION_CONTENT_LIST_ITEM>
        <NTC_LIGHTLISTPRODUCT>
            <REGION>25-Mare Adriatico - Slovenia</REGION>
            <ITA_LIGHT_NUMBER>3620</ITA_LIGHT_NUMBER>
        </NTC_LIGHTLISTPRODUCT>
    </SECTION_CONTENT_LIST_ITEM>
</SECTION_CONTENT_LIST>

This is my XSLT 1.0:

<table style='border-collapse:collapse;  width:100%; align:center' cellspacing="0" cellpadding="0">
    <xsl:for-each select="//ITA_LIGHT_NUMBER">
    <xsl:sort select="." order="ascending" data-type="text"/>                                           
        <tr style="line-height:0.78cm">
            <xsl:variable name="regione" select="preceding-sibling::REGION"/>
            <td style="height:0.68cm;border-bottom:dotted 1.0">
                <xsl:if test="following::REGION != $regione">
                    <xsl:value-of select="preceding-sibling::REGION"/>
                </xsl:if>
            </td>

            <td style="height:0.68cm;border-bottom:dotted 1.0">
                    <xsl:value-of select="."/>
            </td>
        </tr>   
    </xsl:for-each>
</table>

This create a table in two column. In the first column write Region and in the second column write ITA_LIGHT_NUMBER.

10-Mediterraneo Occidentale - Francia 0840
10-Mediterraneo Occidentale - Francia 0843
10-Mediterraneo Occidentale - Francia 0850
16-Mar Tirreno - Francia (Corsica) 0906
16-Mar Tirreno - Francia (Corsica) 0922.15
25-Mare Adriatico - Slovenia 3620

But I want this output:

10-Mediterraneo Occidentale - Francia 0840 - 0850
16-Mar Tirreno - Francia (Corsica) 0906 - 0922.15
25-Mare Adriatico - Slovenia 3620

Pratically: write the first region and the first ITA_LIGHT_NAME, if the following REGION is different from the current write the ITA_LIGHT_NUMBER.

3
  • 1
    This is a grouping question. To learn how to do this in XSLT 1.0, see: jenitennison.com/xslt/grouping/muenchian.html Then do a search for questions tagged XSLT 1.0 and grouping to find numerous examples. Commented Mar 18, 2015 at 16:00
  • Can you help me michael.hor257k. Thanks. Can I do this without use key? Commented Mar 18, 2015 at 16:06
  • 2
    It can be done without using a key but the key-based approaches are likely to be significantly more efficient to run. Why the restriction, out of interest? Keys are supported by every processor I've ever used. Commented Mar 19, 2015 at 10:56

1 Answer 1

1

That's much better if you use keys.

Define it for example at the beginning of your XSLT:

<xsl:key name="lightproducts" match="NTC_LIGHTLISTPRODUCT" use="REGION"/>

And you might modify your template as follows:

  <table style='border-collapse:collapse;  width:100%; align:center' cellspacing="0" cellpadding="0">
      <xsl:for-each select="//NTC_LIGHTLISTPRODUCT">
          <xsl:sort select="REGION" order="ascending" data-type="text"/>
          <xsl:variable name="regione" select="REGION" />

          <xsl:if test="not(preceding::NTC_LIGHTLISTPRODUCT[REGION/text() = $regione])">
              <tr style="line-height:0.78cm">
                  <td style="height:0.68cm;border-bottom:dotted 1.0">
                      <xsl:value-of select="$regione"/>
                  </td>
                  <td style="height:0.68cm;border-bottom:dotted 1.0">
                      <xsl:for-each select="key('lightproducts', $regione)">
                          <xsl:sort select="ITA_LIGHT_NUMBER"/>
                          <xsl:choose>
                              <xsl:when test="last() = 1">
                                  <xsl:value-of select="ITA_LIGHT_NUMBER"/>
                              </xsl:when>
                              <xsl:when test="position() = 1">
                                  <xsl:value-of select="ITA_LIGHT_NUMBER"/>
                              </xsl:when>
                              <xsl:when test="position() = last()">
                                  <xsl:text>-</xsl:text>
                                  <xsl:value-of select="ITA_LIGHT_NUMBER"/>
                              </xsl:when>
                          </xsl:choose>
                      </xsl:for-each>
                  </td>
              </tr>
          </xsl:if>
      </xsl:for-each>
  </table>

This is what I obtain with your input

<?xml version="1.0" encoding="utf-8"?>
<table style="border-collapse:collapse;  width:100%; align:center" cellspacing="0" cellpadding="0">
   <tr style="line-height:0.78cm">
      <td style="height:0.68cm;border-bottom:dotted 1.0">10-Mediterraneo Occidentale - Francia</td>
      <td style="height:0.68cm;border-bottom:dotted 1.0">0840-0850</td>
   </tr>
   <tr style="line-height:0.78cm">
      <td style="height:0.68cm;border-bottom:dotted 1.0">16-Mar Tirreno - Francia (Corsica)</td>
      <td style="height:0.68cm;border-bottom:dotted 1.0">0906-0922.15</td>
   </tr>
   <tr style="line-height:0.78cm">
      <td style="height:0.68cm;border-bottom:dotted 1.0">25-Mare Adriatico - Slovenia</td>
      <td style="height:0.68cm;border-bottom:dotted 1.0">3620</td>
   </tr>
</table>
Sign up to request clarification or add additional context in comments.

2 Comments

@Frik if this answer solves your problem, please accept it. Thnx!
Thanks potame's I try it tomorrow... but i think it work. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.