3

I remember that in Visual Basic a function Split() exists that divides a string by a particular character.

Now I am programming in XSLT and I would like to use a similar function, but I don't know XSLT very well.

In attachment I put xml code:

<SECTION_CONTENT_LIST_ITEM>
    <NTC_LIGHTLISTPRODUCT>
        <VICINITY>TARANTO</VICINITY>
        <ITA_LIGHT_NAME>Porto industriale esterno;Meda elastica</ITA_LIGHT_NAME>
    </NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>
    <NTC_LIGHTLISTPRODUCT>
        <VICINITY>TARANTO</VICINITY>
        <ITA_LIGHT_NAME>Porto industriale esterno;Meda elastica</ITA_LIGHT_NAME>
    </NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>
    <NTC_LIGHTLISTPRODUCT>
        <VICINITY>TARANTO</VICINITY>
        <ITA_LIGHT_NAME>Porto industriale esterno;Meda elastica</ITA_LIGHT_NAME>
    </NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>
    <NTC_LIGHTLISTPRODUCT>
        <VICINITY>TARANTO</VICINITY>
        <ITA_LIGHT_NAME>MAR GRANDE;Porto industriale interno;Accesso al IV sporgente;All 341;Radice IV sporgente (Ant)</ITA_LIGHT_NAME>
    </NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>
    <NTC_LIGHTLISTPRODUCT>
        <VICINITY>TARANTO</VICINITY>
        <ITA_LIGHT_NAME>MAR GRANDE;Porto industriale interno;Accesso al IV sporgente;All 341;Circa 700 m a NNW dell'Ant(Post)</ITA_LIGHT_NAME>
    </NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>

i would like to see:

TARANTO
<br />
Porto industriale esterno
-Meda elastica
-Meda elastica
-Meda elastica
<br />
Mar Grande
-Porto industriale interno
--Accesso al IV sporgente
---All 341
----Radice IV sporgente (Ant)
----Circa 700 m a NNW dell'Ant(Post)
<br />

as a tree from <ITA_LIGHT_NAME> using a function similar to Split() but in XSLT. Thanks.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • - for each child is necessary use - –  Nov 14 '13 at 16:36
  • With your input XML this is quite difficult, especially if you don't know XSLT very well. (There is no `Split()` function in XSLT.) **1)** Can you do anything about the XML? **2)** Are other languages than XSLT an option? **3)** If you must use XSLT, what XSLT engine (name and version) are you using? – Tomalak Nov 14 '13 at 16:50
  • only xslt version 1.0 and i dont know different languige at moment. –  Nov 14 '13 at 16:54
  • I mean the *XSLT engine*, not the XSLT language version. And I asked more than one question. – Tomalak Nov 14 '13 at 16:55
  • 5
    You need to use a recursive template like in the following duplicate question http://stackoverflow.com/questions/4845660/xsl-how-to-split-strings – Matthew Green Nov 14 '13 at 17:10

1 Answers1

1

Looking at the desired output, it is not just a matter of split...

Still, writing a template as suggested Matthew Green would works. However, if you have access to Java, calling an external Java function would make it much easier.

public String splitString(String myStringToSplit, String delimiter)
{
    String[] mySplittedString = myStringToSplit.split(delimiter);
    String returnString = "";

    for(int i = 0; i < mySplittedString.length; i++)
    {
        returnString += mySplittedString[i] + "<br/>";
    }

    return returnString;
}

And in XSLT, after declaring your java namespace :

<xsl:value-of select="namespace:splitString('your;String;To;Split', ';')"/>

This is fully compatible with XSLT 1.0.

Now to get your desired output, once again as this is too complex algorithm for XSLT, I would parse the entire XML and concatenate each node delimited by a specific char for each element into one String than I would send it to a Java function which would return the tree for me. This way, it'll be easier to check for already existing VICINITY, etc. and to make sure you don't have duplicated node.

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76