Page 1 of 1

Split String To Array Using XPATH

Posted: Fri Jul 22, 2016 12:22 am
by Alex
I'm trying to break a string into an array based on the length. for Ex: If I have a string of 30 characters, I want to convert into an array of 5 characters each using an xpath function.

Is there any way to do this? Also, I noticed in this thread - Creating an incremental count variable in XSLT / XPath when using Xpath for..in..return?

that we can use "For" as "For $i in 1 to $length return $i". When I'm trying to use "1 to 5" i.e. constants, xpath is accepting, but when I'm trying to pass a variable " 1 to $length", xpath is not accepting.

Please suggest.

Alex

Re: Split String To Array Using XPATH

Posted: Sat Jul 30, 2016 5:58 pm
by Jamil

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<root>
<string>123456789012345678901234567890</string>
</root>

Code: Select all


<?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:output method="text" encoding="UTF-8"/>

<xsl:template match="/root/string">
<xsl:variable name="var" select="substring(.,1,5),substring(.,6,5),substring(.,11,5),substring(.,16,5),substring(.,21,5),substring(.,26,5)"/>
<xsl:text>&#xa;</xsl:text>
<xsl:value-of select="$var[1]"/><xsl:text>&#xa;</xsl:text>
<xsl:value-of select="$var[2]"/><xsl:text>&#xa;</xsl:text>
<xsl:value-of select="$var[3]"/><xsl:text>&#xa;</xsl:text>
<xsl:value-of select="$var[4]"/><xsl:text>&#xa;</xsl:text>
<xsl:value-of select="$var[5]"/><xsl:text>&#xa;</xsl:text>
<xsl:value-of select="$var[6]"/><xsl:text>&#xa;</xsl:text>
</xsl:template>

</xsl:stylesheet>

Code: Select all


12345
67890
12345
67890
12345
67890