curiousDev
curiousDev

Reputation: 437

Splitting function in xslt 1.0 not working

I have the following xml

<?xml version="1.0" encoding="utf-8"?>
<Root>
                     <Number>2</Number>
                     <Vehicle>
                        <V4Code>PP03340105</V4Code>
                        <SourceCode>PP03340105</SourceCode>
                        <DRCCode>PP03340105</DRCCode>
                        <OptionalCode1>ANDREA WILLIAM|DWIGHT ROBINSON|04</OptionalCode1>

                     </Vehicle>
                  </Root>

Trying to split with the method given below

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="text()" name="split">
  <xsl:param name="pText" select="Root/OptionalCode1"/>
  <xsl:if test="string-length($pText)">
   <xsl:if test="not($pText=.)">
    <br />
   </xsl:if>
    <xsl:element name ="Name1">
   <xsl:value-of select=
    "substring-before(concat($pText,'|'),'|')"/>
     </xsl:element>
   <xsl:call-template name="split">
    <xsl:with-param name="pText" select=
     "substring-after($pText, '|')"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

output is just this :

<?xml version="1.0" encoding="UTF-8"?>

Anyhelp is much appreciated please.I am not sure in what line i am making mistake. Since i am new to this xslt, not sure how to find the line in which i am making mistake too.

Output should be

<Root>
<List1>Andrea William</List1>
<List2>Dwight Robinson</List2>
</Root>

Upvotes: 0

Views: 81

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66781

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="Root">
        <xsl:copy>
            <xsl:call-template name="split">
                <xsl:with-param name="pText" select="Vehicle/OptionalCode1"/>
            </xsl:call-template>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="text()" name="split">
        <xsl:param name="pText" />
        <xsl:param name="level" select="1"/>
        <xsl:if test="string-length($pText)">
            <!--We don't want numeric-only values in our list -->
            <xsl:if test="translate($pText, '0123456789', '')">
                <!--use the level to construct an incrementing List element name -->
                <xsl:element name ="List{$level}">
                    <xsl:value-of select=
                        "substring-before(concat($pText,'|'),'|')"/>
                </xsl:element>
            </xsl:if>
            <xsl:call-template name="split">
                <xsl:with-param name="pText" select=
                    "substring-after($pText, '|')"/>
                <!--increment the level, used to construct List element names -->
                <xsl:with-param name="level" select="$level + 1"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Related Questions