I'm trying to sort alphabetically fields (in this case VICINITY), but if not exist (VICINITY), I need to consider another field (ITA_LIGHT_NAME), thereby creating two nested sort.
I create this xslt:
<xsl:text>List A<xsl:text>
<xsl:for-each select="//VICINITY[not(. = preceding::VICINITY)]">
<xsl:sort select="." data-type="text" order="ascending"/>
<xsl:sort select="preceding-sibling::ITA_LIGHT_NUMBER" data-type="text" order="ascending"/>
<xsl:variable name ="localita" select="."/>
<xsl:value-of select="."/>
<br/>
</xsl:for-each>
<br/>
<xsl:text>List B<xsl:text>
<xsl:for-each select="//ITA_LIGHT_NAME[not(. = preceding::ITA_LIGHT_NAME)]">
<xsl:if test="not(preceding-sibling::VICINITY)">
<xsl:value-of select="."/><br/>
</xsl:if>
</xsl:for-each>
This give two result (List A and List B divided in two different list):
List A
ANCONA (Only one time)
GENOVA
MESSINA
VENEZIA
List B
Capo Peloro
Capo Rizzuto (Only one time)
But, I really would like as final output a single list(sorted alphabetically):
ANCONA (Only one time)
Capo Peloro
Capo Rizzuto (Only one time)
GENOVA
MESSINA
VENEZIA
Practically, should work in the following way: Order alphabetically by VICINITY(without repetition) if exist, if not exist, order by ITA_LIGHT_NAME(without repetition).
Thi is my XML, can you Help to simulate this output:
<SECTION_CONTENT_LIST>
<SECTION_CONTENT_LIST_ITEM>
<NTC_LIGHTLISTPRODUCT>
<ITA_LIGHT_NUMBER>3921.9</ITA_LIGHT_NUMBER>
<VICINITY>ANCONA</VICINITY>
<ITA_LIGHT_NAME>Installazioni 1</ITA_LIGHT_NAME>
</NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>
<NTC_LIGHTLISTPRODUCT>
<ITA_LIGHT_NUMBER>3924</ITA_LIGHT_NUMBER>
<VICINITY>ANCONA</VICINITY>
<ITA_LIGHT_NAME>Installazioni 2</ITA_LIGHT_NAME>
</NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>
<NTC_LIGHTLISTPRODUCT>
<ITA_LIGHT_NUMBER>1577</ITA_LIGHT_NUMBER>
<VICINITY>GENOVA</VICINITY>
<ITA_LIGHT_NAME>Granarolo</ITA_LIGHT_NAME>
</NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>
<NTC_LIGHTLISTPRODUCT>
<ITA_LIGHT_NUMBER>2746</ITA_LIGHT_NUMBER>
<VICINITY>MESSINA</VICINITY>
<ITA_LIGHT_NAME>Meda elastica</ITA_LIGHT_NAME>
</NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>
<NTC_LIGHTLISTPRODUCT>
<ITA_LIGHT_NUMBER>4231.4</ITA_LIGHT_NUMBER>
<VICINITY>VENEZIA</VICINITY>
<ITA_LIGHT_NAME>Segnale da nebbia</ITA_LIGHT_NAME>
</NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>
<NTC_LIGHTLISTPRODUCT>
<ITA_LIGHT_NUMBER>2736</ITA_LIGHT_NUMBER>
<ITA_LIGHT_NAME>Capo Peloro</ITA_LIGHT_NAME>
</NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>
<NTC_LIGHTLISTPRODUCT>
<ITA_LIGHT_NUMBER>3396</ITA_LIGHT_NUMBER>
<ITA_LIGHT_NAME>Capo Rizzuto</ITA_LIGHT_NAME>
</NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>
<NTC_LIGHTLISTPRODUCT>
<ITA_LIGHT_NUMBER>3399</ITA_LIGHT_NUMBER>
<ITA_LIGHT_NAME>Capo Rizzuto</ITA_LIGHT_NAME>
</NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
</SECTION_CONTENT_LIST>
No, it cannot work that way. What you need to do first is get only distinct records (considering VICINITY if it exists, ITA_LIGHT_NAME otherwise), then sort the results by the same.
The first step is done through a method known as Muenchian grouping - read about it here: http://www.jenitennison.com/xslt/grouping/muenchian.html
Note also that your input is missing a root element. Once you add it, you can try:
XSLT 1.0
to obtain: