1

Let's say the I have the String

[Apple,Pie,Pizza]

How could I turn that into

<Root>
    <Apple/>
    <Pie/>
    <Pizza/>
</Root>

I can get rid of the brackets easily and tokenize the string, but it get an array back that I don't know how to transform into a list of elements.

Thanks

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
dmendezg
  • 1,888
  • 1
  • 15
  • 23

2 Answers2

4

An regular input to XSLT must always be XML, so the stylesheet below assumes as input:

<?xml version="1.0" encoding="UTF-8"?>
<string>[Apple,Pie,Pizza]</string>

Stylesheet (XSLT 2.0)

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="string">
        <root>
            <xsl:for-each select="tokenize(substring(.,2, string-length()-2),',')">
                <xsl:element name="{.}"/>
            </xsl:for-each>
        </root>
    </xsl:template>

</xsl:stylesheet>

Output

<root>
   <Apple/>
   <Pie/>
   <Pizza/>
</root>

In case you can only use XSLT 1.0, a recursive template to achieve the same in XSLT 1.0.

Stylesheet (XSLT 1.0)

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="string">
        <root>
            <xsl:call-template name="elementifier">
                <xsl:with-param name="str" select="substring(.,2, string-length()-2)"/>
            </xsl:call-template>
        </root>
    </xsl:template>

    <xsl:template name="elementifier">
        <xsl:param name="str"/>

        <xsl:choose>
            <xsl:when test="$str">
                <xsl:variable name="element-name">
                    <xsl:choose>
                        <xsl:when test="contains($str,',')">
                            <xsl:value-of select="substring-before($str,',')"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$str"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>

                <xsl:element name="{$element-name}"/>
                <xsl:call-template name="elementifier">
                    <xsl:with-param name="str" select="substring-after($str,',')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise/>
        </xsl:choose>

    </xsl:template>

</xsl:stylesheet>
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
1

With XSLT 2.0 you have the string operations and then you have a sequence of strings you can process e.g.

<xsl:param name="str1" select="'[Apple,Pie,Pizza]'"/>

<xsl:for-each select="tokenize(replace($str1, '^\[|\]$', ''), ',')">
  <xsl:element name="{.}"/>
</xsl:for-each>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110