Does XSLT have a Split () function?

I have a string in node and I would like to split the string into '?' and returns the last element in the array.

For example, in the block below:

<a> <xsl:attribute name="href"> /newpage.aspx?<xsl:value-of select="someNode"/> </xsl:attribute> Link text </a> 

I would like to split the value of someNode .

Edit: Here is the VB.Net that I use to download Xsl for my Asp.Net page:

 Dim xslDocPath As String = HttpContext.Current.Server.MapPath("~/App_Data/someXslt.xsl") Dim myXsltSettings As New XsltSettings() Dim myXMLResolver As New XmlUrlResolver() myXsltSettings.EnableScript = True myXsltSettings.EnableDocumentFunction = True myXslDoc = New XslCompiledTransform(False) myXslDoc.Load(xslDocPath, myXsltSettings, myXMLResolver) Dim myStringBuilder As New StringBuilder() Dim myXmlWriter As XmlWriter = Nothing Dim myXmlWriterSettings As New XmlWriterSettings() myXmlWriterSettings.ConformanceLevel = ConformanceLevel.Auto myXmlWriterSettings.Indent = True myXmlWriterSettings.OmitXmlDeclaration = True myXmlWriter = XmlWriter.Create(myStringBuilder, myXmlWriterSettings) myXslDoc.Transform(xmlDoc, argumentList, myXmlWriter) Return myStringBuilder.ToString() 

Refresh here an example of splitting XML into a specific node

+42
split xml xslt
Sep 25 '08 at 22:07
source share
8 answers

Use the recursive method:

 <xsl:template name="output-tokens"> <xsl:param name="list" /> <xsl:variable name="newlist" select="concat(normalize-space($list), ' ')" /> <xsl:variable name="first" select="substring-before($newlist, ' ')" /> <xsl:variable name="remaining" select="substring-after($newlist, ' ')" /> <id> <xsl:value-of select="$first" /> </id> <xsl:if test="$remaining"> <xsl:call-template name="output-tokens"> <xsl:with-param name="list" select="$remaining" /> </xsl:call-template> </xsl:if> </xsl:template> 
+51
Sep 26 '08 at 18:04
source share

If you can use XSLT 2.0 or higher, you can use tokenize(string, separator) :

 tokenize("XPath is fun", "\s+") Result: ("XPath", "is", "fun") 

See the w3schools XPath link.

By default, .NET does not support XSLT 2.0, not to mention XSLT 3.0. The only known 2.0+ processors for .NET are Saxon for.NET with IKVM , Exselt , the .NET XSLT 3.0 processor currently in beta, and the XMLPrime XSLT 2.0 processor.

+11
Sep 25 '08 at 22:14
source share

I ended up using the substring-after() function. Here is what worked for me:

 <a> <xsl:attribute name="href"> /newpage.aspx?<xsl:value-of select="substring-after(someNode, '?')"/> </xsl:attribute> Link text </a> 

Even after installing my XSLT version on 2.0, I still get the error “ 'tokenize()' is an unknown XSLT function. ” When I try to use tokenize() .

+8
Sep 25 '08 at 23:24
source share

Adding another feature, if your template engine supports EXSLT , you can use tokenize () from this.

For example:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings" extension-element-prefixes="str"> ... <a> <xsl:attribute name="href"> <xsl:text>/newpage.aspx?</xsl:text> <xsl:value-of select="str:tokenize(someNode)[2]"/> </xsl:attribute> </a> ... </xsl:stylesheet> 
+6
May 19 '10 at 16:06
source share

.NET does not support XSLT 2.0, unfortunately. I am sure that it supports EXSLT, which has a split () function. Microsoft has an older page on its implementation of EXSLT.

+3
Sep 26 '08 at 18:44
source share

You can write a template using the string-before and string-after functions and use it. I wrote a blog about this.

Finally, an xslt template appeared that would split the line with the delimiters into substrings. I do not pretend to be his smartest script, but it definitely solves my problem.

Styles:

 <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:for-each select="Paths/Item"> <xsl:call-template name="SplitText"> <xsl:with-param name="inputString" select="Path"/> <xsl:with-param name="delimiter" select="Delimiter"/> </xsl:call-template> <br/> </xsl:for-each> </xsl:template> <xsl:template name="SplitText"> <xsl:param name="inputString"/> <xsl:param name="delimiter"/> <xsl:choose> <xsl:when test="contains($inputString, $delimiter)"> <xsl:value-of select="substring-before($inputString,$delimiter)"/> <xsl:text disable-output-escaping = "no"> </xsl:text> <xsl:call-template name="SplitText"> <xsl:with-param name="inputString" select="substring-after($inputString,$delimiter)"/> <xsl:with-param name="delimiter" select="$delimiter"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:choose> <xsl:when test="$inputString = ''"> <xsl:text></xsl:text> </xsl:when> <xsl:otherwise> <xsl:value-of select="$inputString"/> <xsl:text> </xsl:text> </xsl:otherwise> </xsl:choose> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> 

XML file (for conversion):

 <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="textSpliter.xslt"?> <Paths> <Item> <Path>C:\ProgramFiles\SomeWierdSoftware</Path> <Delimiter>\</Delimiter> </Item> </Paths> 
+2
May 28 '11 at 8:18
source share

XSLT 1.0 does not have a separation function as such, but you can potentially achieve what you are trying to do with the substring functions in front of and substring.

Alternatively, if you use the Microsoft XSLT engine, you can use the built-in C #.

+1
Sep 25 '08 at 22:18
source share

For the record only, if you do this with 1.0, and you really need split / tokenise, you need xslt extensions .

+1
Sep 26 '08 at 8:04
source share



All Articles