Using substring in xsl - xslt

Following on from an earlier question, and this is more about xsl syntax. I want to split part of a URL variable into a new variable in xsl.
This code works when the variable is sitting part way along a URL. EG:
http://www.mysite.com/test.aspx?aVar=something&bVar=somethingMore&cVar=yetMoreStill
<xsl:variable name="testVar" select="substring-after($url, 'bVar=')"/>
<xsl:value-of select="substring-before($testVar, '&')" />
The problem is the variable can sometime sit at the end of the URL (I have no control over this) EG:
http://www.mysite.com/test.aspx?aVar=something&bVar=somethingMore
So the above code fails. Is there away I can allow for both occurrences? The end game is I'm just trying to get the value of bVar no matter where it sits within the URL. Thanks.

How about the following workaround?
<xsl:variable name="testVar" select="substring-after($url, 'bVar=')"/>
<xsl:value-of select="substring-before(concat($testVar, '&'), '&')" />

Try this. This is XSLT 1.0:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/">
<xsl:call-template name="urlResolver">
<xsl:with-param name="input" select="'http://www.mysite.com/test.aspx?aVar=something&bVar=somethingMore'" />
</xsl:call-template>
<xsl:call-template name="urlResolver">
<xsl:with-param name="input" select="'http://www.mysite.com/test.aspx?aVar=something&bVar=somethingMore&cVar=yetMoreStill'" />
</xsl:call-template>
</xsl:template>
<xsl:template name="urlResolver">
<xsl:param name="input" />
<xsl:variable name="testVar" select="substring-after($input, 'bVar=')"/>
<xsl:choose>
<xsl:when test="contains($testVar, '&')"><xsl:value-of select="substring-before($testVar, '&')" /></xsl:when>
<xsl:otherwise><xsl:value-of select="$testVar" /></xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

Try to make use of tokenize (available in XSLT 2.0) like the following:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes" method="xml" version="1.0"/>
<xsl:template match="/">
<xsl:variable name="test"><![CDATA[http://www.mysite.com/test.aspx?aVar=something&bVar=somethingMore&cVar=yetMoreStill]]></xsl:variable>
<xsl:variable name="splitURL" select="tokenize($test,'&')"/>
<xsl:variable name="bvar" select="$splitURL[starts-with(.,'bVar')]"/>
<out><xsl:value-of select="substring-after($bvar, 'bVar=')"/></out>
</xsl:template>
</xsl:stylesheet>

The currently accepted answer is generally wrong.
Try it with this URL:
http://www.mysite.com/test.aspx?subVar=something&bVar=somethingMore
and you get the wrong result: something
This question was already answered... In case you read the answer you would just reuse it and get your QString from the produced result:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pUrl" select=
"'http://www.mysite.com/test.aspx?subVar=something&bVar=somethingMore'"/>
<xsl:template match="/">
<xsl:variable name="vrtfQStrings">
<xsl:call-template name="GetQueryStringParams"/>
</xsl:variable>
bVar = "<xsl:value-of select="ext:node-set($vrtfQStrings)/bVar"/>"
</xsl:template>
<xsl:template name="GetQueryStringParams">
<xsl:param name="pUrl" select="$pUrl"/>
<xsl:variable name="vQueryPart" select=
"substring-before(substring-after(concat($pUrl,'?'),
'?'),
'?')"/>
<xsl:variable name="vHeadVar" select=
"substring-before(concat($vQueryPart,'&'), '&')"/>
<xsl:element name="{substring-before($vHeadVar, '=')}">
<xsl:value-of select="substring-after($vHeadVar, '=')"/>
</xsl:element>
<xsl:variable name="vRest" select="substring-after($vQueryPart, '&')"/>
<xsl:if test="string-length($vRest) > 0">
<xsl:call-template name="GetQueryStringParams">
<xsl:with-param name="pUrl" select=
"concat('?', substring(substring-after($vQueryPart, $vHeadVar), 2))"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
When applied on any XML document (not used), this transformation produces the wanted, correct result:
bVar = "somethingMore"

Related

Select a parallel node in xslt

Consider following xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<base>
<a>
<b>
<c>Text 1</c>
</b>
</a>
</base>
<base>
<a>
<b>
<c>Text 2</c>
</b>
</a>
</base>
</root>
and this xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="root/base[1]" />
</xsl:template>
<xsl:template match="base//*">
<xsl:if test="text()">
<xsl:value-of select="text()" />
<!-- i need Text 2 here -->
</xsl:if>
<xsl:apply-templates />
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
The original xml is much more nested an i don't know the exact structure. But there is a parallel node with the same structure. If my template is at //root/base[1]/a/b/c I want to reference //root/base[2]/a/b/c
However I only know that I am in some node below //root/base[1] and that there is the same node in //root/base[2].
Is there a possibility to accomplish this goal?
This stylesheet requires no extensions, and does not make any assumption on unique names; the boring part is to pass the correct "twin node" every time you call xsl:apply-templates:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="root/base[1]">
<xsl:with-param name="twin" select="root/base[2]"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="*">
<xsl:param name="twin"/>
<xsl:if test="text()">
<xsl:value-of select="text()" />
<xsl:value-of select="$twin/text()"/>
</xsl:if>
<xsl:for-each select="*">
<xsl:variable name="pos" select="position()"/>
<xsl:apply-templates select=".">
<xsl:with-param name="twin" select="$twin/*[$pos]"/>
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
Solution with using saxon:evaluate extension (http://saxon.sourceforge.net/saxon7.9/extensions.html#evaluate)
You can also use dyn:evaluate if you are using xslt 1.0 processors (http://exslt.org/dyn/index.html):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
xmlns:saxon="http://saxon.sf.net/"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="root/base[1]" />
</xsl:template>
<xsl:template match="base//*">
<xsl:if test="text()">
<xsl:variable name="path">
<xsl:call-template name="constructPath">
<xsl:with-param name="node" select="."/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="rewritedPath" select="concat('/root/base[2]', substring($path, 11))"/>
<xsl:value-of select="text()" />
<xsl:value-of select="saxon:evaluate($rewritedPath)" />
</xsl:if>
<xsl:apply-templates />
</xsl:template>
<xsl:template name="constructPath">
<xsl:param name="node"/>
<xsl:choose>
<xsl:when test="$node/parent::node()/name()">
<xsl:call-template name="constructPath">
<xsl:with-param name="node" select="$node/parent::node()"/>
</xsl:call-template>
<xsl:value-of select="concat('/', $node/name())"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('/', $node/name())"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
Here we use recursive template "constructPath" to create string and process it in evaluate() function. This will select the exact node as in the parallel branch. Hope this will help.
Try the code below. This code will work only if each base element has children with unique element name.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="root/base[1]" />
</xsl:template>
<xsl:template match="base//*">
<xsl:if test="text()">
<xsl:variable name="name" select="name()"/>
<xsl:value-of select="text()" />
<xsl:value-of select="ancestor::base/following-sibling::base[1]//*[name() = $name]" />
</xsl:if>
<xsl:apply-templates />
</xsl:template>
<xsl:template match="text()" />
However you can use dyn:evaluate from exslt extension or saxon:eval

Excluding namespaces from XML

My xsl file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:go="http://www.google.com"
exclude-result-prefixes="go">
<xsl:include href="SomeLibrary.xsl"/>
<xsl:template match="/">
<xsl:call-template name="SomeTemplate">
<xsl:with-param name="Element" select="'randomParam'"/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
The SomeLibrary.xsl file:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.google.com">
<xsl:template name="SomeTemplate">
<xsl:param name="Element"/>
<Blabla>
<xsl:value-of select="$Element" />
</Blabla>
</xsl:template>
</xsl:stylesheet>
Input xml: just use an empty XML. The result is this:
<?xml version="1.0" encoding="UTF-8"?>
<Blabla xmlns="http://www.google.com">
randomParam
</Blabla>
What I want is to have the "Blabla" node without a namespace. How can I remove it or make sure it doesn't get there, without modifying my "SomeLibrary.xsl"?
In case you don't want to edit the imported stylesheet code, the way to remove the namespace is with a two-pass transformation (which in XSLT 1.0 requires using an xxx:node-set() extension function):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vrtfPass1">
<xsl:call-template name="SomeTemplate">
<xsl:with-param name="Element" select="'randomParam'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="vPass1" select="ext:node-set($vrtfPass1)"/>
<xsl:apply-templates select="$vrtfPass1/node()" mode="pass2"/>
</xsl:template>
<xsl:template match="*" mode="pass2">
<xsl:element name="{name()}">
<xsl:copy-of select="#*"/>
<xsl:apply-templates mode="pass2"/>
</xsl:element>
</xsl:template>
<xsl:template name="SomeTemplate" xmlns="http://www.google.com">
<xsl:param name="Element"/>
<Blabla>
<xsl:value-of select="$Element" />
</Blabla>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on any XML document (not used), the wanted result (the default namespace removed) is produced:
<Blabla>randomParam</Blabla>
Update:
The OP has indicated in a comment that he is using Xalan 2.07.
Below is almost the same solution, but with namespace and name for the xxx:node-set() function, as used in Xalan:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="xmlns:xalan="http://xml.apache.org/xalan" exclude-result-prefixes="x" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vrtfPass1">
<xsl:call-template name="SomeTemplate">
<xsl:with-param name="Element" select="'randomParam'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="vPass1" select="x:nodeset($vrtfPass1)"/>
<xsl:apply-templates select="$vrtfPass1/node()" mode="pass2"/>
</xsl:template>
<xsl:template match="*" mode="pass2">
<xsl:element name="{name()}">
<xsl:copy-of select="#*"/>
<xsl:apply-templates mode="pass2"/>
</xsl:element>
</xsl:template>
<xsl:template name="SomeTemplate" xmlns="http://www.google.com">
<xsl:param name="Element"/>
<Blabla>
<xsl:value-of select="$Element" />
</Blabla>
</xsl:template>
</xsl:stylesheet>
Explicitly create them with:
<xsl:element name="Blabla" namespace="">...</xsl:element>
but this means fiddling with the SomeLibrary.xsl file.
add exclude-result-prefixes="go" to the stylesheet element.
and, of course, update the namespace declaration from the default one to xmlns:go="http://www.google.com"

How to assign formated value to other variable in xslt

<xsl:value-of select="substring-before($temp1,';')" disable-output-escaping="yes"/>
where temp1="fassdf sdf; asdf &dfsdfsdf;fsdfsf;"
The above code I am using to split value using ";". The problem is temp1 having &, so it splits this value by the escaped sequence character ;. So i am getting wrong output. But if I use the disable-output-escaping="yes" then the "&" is converted to &.
How to get the formatted value from the string? So if i split the string i will not get any issue. Because I will get string with & instead of &
Lets assume a sample XML for your/our convenience ..
<?xml version="1.0" encoding="utf-8"?>
<root>
<child>sharepoint; R&D;Department;</child>
</root>
XSLT code to output the desired one:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="text" indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="node()"/>
</xsl:template>
<xsl:template match="child">
<xsl:call-template name="SplitString">
<xsl:with-param name="StringVal" select="concat(.,';')"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="SplitString">
<xsl:param name="StringVal"/>
<xsl:variable name="first" select="substring-before($StringVal, ';')" />
<xsl:variable name="remaining" select="substring-after($StringVal, ';')" />
<xsl:value-of select="normalize-space($first)" disable-output-escaping="yes" />
<xsl:if test="$remaining">
<xsl:value-of select="'
'"/>
<xsl:call-template name="SplitString">
<xsl:with-param name="StringVal" select="$remaining" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
This is the Output you get:
sharepoint
R&D
Department

xsl:call-template does not work in XSL

I am trying to call the below template from my code . But I keep getting javax.xml.transform.TransformerException: ElemTemplateElement error: incrementValue.For a different template I still get javax.xml.transform.TransformerException: ElemTemplateElement error: templateName.Since the stylesheet is too long I am pasting the relevant code of the stylesheet. Can someone let me know what I am doing wrong ??
<xsl:stylesheet version = '2.0'
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes"
xmlns:mngi="www.medianewsgroup.com"
exclude-result-prefixes="xs xdt mngi dirReader"
xmlns:date="http://exslt.org/dates-and-times"
xmlns:utildate="xalan://java.util.Date"
xmlns:dirReader="xalan://com.mngi.eidos.util.DirectoryReader"
extension-element-prefixes="date utildate dirReader">
<xsl:strip-space elements="*"/>
<xsl:output method="xml"
indent="yes"
encoding="utf-8"
doctype-system="/SysConfig/Classify/Dtd/MNG/classify-story.dtd"/>
<xsl:template match="/">
<xsl:processing-instruction name="EM-dtdExt"
>/SysConfig/Rules/MNG/MNG.dtx</xsl:processing-instruction>
<xsl:processing-instruction name="EM-templateName"
>/SysConfig/BaseConfiguration/MNG/Templates/MNG_story.xml</xsl:processing-instruction>
<xsl:processing-instruction name="xml-stylesheet"
>type="text/css" href="/SysConfig/BaseConfiguration/MNG/Css/MNG-story-nonechannel.css"</xsl:processing-instruction>
<!-- Added By Sachin -->
<xsl:processing-instruction name="EM-dtdExt"
>/SysConfig/Rules/MNG/MNG.dtx</xsl:processing-instruction>
<xsl:processing-instruction name="EM-templateName"
>/SysConfig/BaseConfiguration/MNG/Templates/MNG_story.xml</xsl:processing-instruction>
<xsl:processing-instruction name="xml-stylesheet"
>type="text/css" href="/SysConfig/BaseConfiguration/MNG/Css/MNG-story-nonechannel.css"</xsl:processing-instruction>
<xsl:variable name="UPPERCASE" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ '" />
<xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="HubName" select="translate(/Article/Hub/HubName, ' ', '')" />
<xsl:variable name="lowerhubname" select="translate($HubName, $UPPERCASE, $lowercase)" />
<xsl:variable name="SiteRoot" select="'C:/TwinCitiesArticles'" />
<xsl:variable name="DatePath" select="translate(substring-before(/Article/PublishingDates/WebPublish_DTTM, 'T'), '-', '/')"/>
<xsl:variable name="PhotoDir" select="'photos/'" />
<xsl:variable name="PhotoPath" select="concat($SiteRoot, $DatePath, '/', $lowerhubname, $PhotoDir)" />
<TodaysDate>
<xsl:value-of select="utildate:new()"/>
</TodaysDate>
<imageDir>
<xsl:value-of select="$PhotoPath"/>
</imageDir>
<xsl:variable name="totalPhotos" select="dirReader:totalPhotos($PhotoPath)"/>
<xsl:variable name="photoList" select="dirReader:readDirectory($PhotoPath)"/>
<xsl:variable name="pName" select="dirReader:photoName($totalPhotos,$PhotoPath)"/>
<xsl:variable name="firstPhotoName" select="dirReader:firstPhoto($totalPhotos,$PhotoPath)"/>
<xsl:variable name="currentIdx" select="dirReader:currentIndex($firstPhotoName,$PhotoPath)"/>
<totalPhotos>
<xsl:value-of select="$totalPhotos" />
</totalPhotos>
<xsl:template name="incrementValue">
<xsl:param name="currentIdx"/>
<xsl:if test="$currentIdx < $totalPhotos">
<xsl:value-of select="$currentIdx"/>
<photoName>
<xsl:variable name="photoFromIndex"
select="dirReader:photoNameWithIndex($currentIdx,$PhotoPath)"/>
<xsl:value-of select="concat($PhotoPath,'',$photoFromIndex)"/>
</photoName>
<xsl:call-template name="incrementValue">
<xsl:with-param name="currentIdx" select="$currentIdx + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:if test="$totalPhotos > 0">
<photoName>
<!--xsl:value-of select="$currentIdx"/-->
<xsl:variable name="photoFromIndex" select="dirReader:photoNameWithIndex($currentIdx,$PhotoPath)"/>
<xsl:value-of select="concat($PhotoPath,'',$photoFromIndex)"/>
</photoName>
<xsl:call-template name="incrementValue">
<xsl:with-param name="currentIdx" select="$currentIdx"/>
</xsl:call-template>
</xsl:if>
Your xsl:if, xsl:value-of and xsl:variable all need to exist inside an xsl:template, xsl:variable or xsl:param, I am not sure whether they are not.
An xsl:template must be a child of xsl:stylesheet only.
You need to remove the template definitions from inside the first <xsl:template match="/">
Define the incrementValue template seperate and put the content of the other template inside the main <xsl:template match="/">
so you have something like this:
<xsl:stylesheet version = '2.0'
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes"
xmlns:mngi="www.medianewsgroup.com"
exclude-result-prefixes="xs xdt mngi dirReader"
xmlns:date="http://exslt.org/dates-and-times"
xmlns:utildate="xalan://java.util.Date"
xmlns:dirReader="xalan://com.mngi.eidos.util.DirectoryReader"
extension-element-prefixes="date utildate dirReader">
<xsl:strip-space elements="*"/>
<xsl:output method="xml"
indent="yes"
encoding="utf-8"
doctype-system="/SysConfig/Classify/Dtd/MNG/classify-story.dtd"/>
...
<xsl:variable name="totalPhotos" select="dirReader:totalPhotos($PhotoPath)"/>
...
<xsl:template match="/">
...
<xsl:if test="$totalPhotos > 0">
<photoName>
<!--xsl:value-of select="$currentIdx"/-->
<xsl:variable name="photoFromIndex" select="dirReader:photoNameWithIndex($currentIdx,$PhotoPath)"/>
<xsl:value-of select="concat($PhotoPath,'',$photoFromIndex)"/>
</photoName>
<xsl:call-template name="incrementValue">
<xsl:with-param name="currentIdx" select="$currentIdx"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="incrementValue">
<xsl:param name="currentIdx"/>
<xsl:if test="$currentIdx < $totalPhotos">
<xsl:value-of select="$currentIdx"/>
<photoName>
<xsl:variable name="photoFromIndex" select="dirReader:photoNameWithIndex($currentIdx,$PhotoPath)"/>
<xsl:value-of select="concat($PhotoPath,'',$photoFromIndex)"/>
</photoName>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
EDIT: Variables used in both templates will have to be declared globally as I have done with <xsl:variable name="totalPhotos" select="dirReader:totalPhotos($PhotoPath)"/> above so they are available to both templates because at the minute they are only scoped to the template they are in. or you can pass them as parameters as is done with <xsl:with-param name="currentIdx" select="$currentIdx"/>. If there are variables that only exist in the incrementValue template move out of the main template into that one.
WARNING: This is untested as I don't fully understand the problem due to lack of input so I am only sorting out the syntax.

How can I achieve SPLIT function in XSLT 1.0

I have a following scenario. I have a TEAM-MEMBERS node which has name in the format last name, first name
<TEAM-MEMBER><LONG-NAME>Last Name, First Name</LONG-NAME></TEAM-MEMBER>
I want to transform this to
<CONTACT><FIRSTNAME>First Name</FIRSTNAME><LASTNAME>Last Name</LASTNAME></CONTACT>
Basically i want to split the <LONG-NAME> node's value by ,
How can I achieve this using XSLT 1.0
This XSLT will be consumed by BizTalk Server hence i am looking for some XSLT 1.0 solutions only
Thanks
Karthik
Here is a complete XSLT 1.0 solution that uses a general "split" template that splits a string into multiple substrings, provided a delimiter to designate the boundary between substrings:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="TEAM-MEMBER">
<xsl:variable name="vrtfSplitWords">
<xsl:call-template name="split">
<xsl:with-param name="pText" select="."/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="vSplitWords"
select="ext:node-set($vrtfSplitWords)/*"/>
<CONTACT>
<FIRSTNAME><xsl:value-of select="$vSplitWords[2]"/></FIRSTNAME>
<LASTNAME><xsl:value-of select="$vSplitWords[1]"/></LASTNAME>
</CONTACT>
</xsl:template>
<xsl:template name="split">
<xsl:param name="pText" select="."/>
<xsl:param name="pDelim" select="', '"/>
<xsl:param name="pElemName" select="'word'"/>
<xsl:if test="string-length($pText)">
<xsl:element name="{$pElemName}">
<xsl:value-of select=
"substring-before(concat($pText,$pDelim),
$pDelim
)
"/>
</xsl:element>
<xsl:call-template name="split">
<xsl:with-param name="pText" select=
"substring-after($pText,$pDelim)"/>
<xsl:with-param name="pDelim" select="$pDelim"/>
<xsl:with-param name="pElemName" select="$pElemName"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<TEAM-MEMBER><LONG-NAME>Last Name, First Name</LONG-NAME></TEAM-MEMBER>
the wanted, correct result is produced:
<CONTACT>
<FIRSTNAME>First Name</FIRSTNAME>
<LASTNAME>Last Name</LASTNAME>
</CONTACT>
II. Solution using FXSL
This transformation uses the str-split-to-words template from FXSL:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext"
>
<xsl:import href="strSplit-to-Words.xsl"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:param name="pmaxLines" select="10"/>
<xsl:template match="/">
<xsl:variable name="vwordNodes">
<xsl:call-template name="str-split-to-words">
<xsl:with-param name="pStr" select="/"/>
<xsl:with-param name="pDelimiters"
select="',()'"/>
</xsl:call-template>
</xsl:variable>
<xsl:apply-templates select=
"ext:node-set($vwordNodes)/*[normalize-space()]"/>
</xsl:template>
<xsl:template match="word[normalize-space()][1]">
<FIRSTNAME>
<xsl:value-of select="normalize-space()"/>
</FIRSTNAME>
</xsl:template>
<xsl:template match="word[normalize-space()][2]">
<MIDNAME>
<xsl:value-of select="normalize-space()"/>
</MIDNAME>
</xsl:template>
<xsl:template match="word[normalize-space()][last()]">
<LASTNAME>
<xsl:value-of select="normalize-space(.)"/>
</LASTNAME>
</xsl:template>
</xsl:stylesheet>
when applied to this XML document (made quite more complex):
<TEAM-MEMBER><LONG-NAME>First Name, (Jr.), Last Name</LONG-NAME></TEAM-MEMBER>
the wanted, correct result is produced:
<FIRSTNAME>First Name</FIRSTNAME>
<MIDNAME>Jr.</MIDNAME>
<LASTNAME>Last Name</LASTNAME>
Do Note:
The str-split-to-words template accepts multiple delimiters. Thus in this transformation the delimiters used are: ',', '(' and ')'
You need a recursive named template. Fortunately it's already been written: look for str:tokenize at http://www.exslt.org.
used substring-after() and substring-before() to get around the split