简体   繁体   中英

Split and count number of string in xslt 1.0

I have strings as below,

788565591,1,444,0,15956,555,126.99,15956,666,0,15956,777,101.66,15956
788565591,1,444,0,,555,126.99,,,0,15956,777

I am trying to write an xslt template to find out number of comma's in the string. Based on the count i need to add/delete few more to normalize length to 32.

Please advice/ Give me some idea in form of code.

Assuming the current context node is the one whose value you want to process, then

<xsl:variable name="numCommas"
              select="string-length() - string-length(translate(., ',', ''))" />

would give you the number of commas in the string (literally it's calculating the length of the string minus the length of the-string-with-all-commas-removed). If you then have a variable containing 32 commas (this could be a global variable defined outside the templates):

<xsl:variable name="thirtyTwoCommas"
              select="',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,'" />

then you can take the substring($thirtyTwoCommas, $numCommas + 1) to get a string containing the number of extra commas you would need to add to the original string to make it up to 32. If there's 32 or more commas in the original string then this would be empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM