Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Display Modes
  #1 (permalink)  
Old October 11th, 2017, 12:13 PM
Authorized User
Points: 115, Level: 2
Points: 115, Level: 2 Points: 115, Level: 2 Points: 115, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Nov 2016
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Split values and insert some character


Can you please anyone help me.

My input:
<Para0 id="335-100" refid="[335-100]">
<con>Corrections Act 1986 </con>
<sec>Pt 9E, 30G, 30H, 30I, ss 47M, 79H, 104ZC, 104ZD</sec>
Expected output

<Para0 id="335-100" refid="[335-100]">
<sec>Pt 9E, Pt 30G, Pt 30H, Pt 30I, ss 47M, ss 79H, ss 104ZC, 104ZD</sec>

My code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

<xsl:template match="node()|@*">
<xsl:apply-templates select="node()|@*"/>

<xsl:template match="Para0">
<xsl:for-each select="//sec">
<xsl:for-each select="tokenize(.,',')">

<xsl:variable name="ddd">
<xsl:when test="matches(.,'\s+[A-z]+\s+')">
<xsl:message select="."></xsl:message>
<xsl:analyze-string select="current()" regex="\s+([A-z]+)\s+">

<xsl:value-of select="regex-group(1)"/>
<xsl:value-of select="substring-before(.,' ')"/>
<xsl:message select="$ddd"></xsl:message>
<xsl:value-of select="concat($ddd, .)"/><xsl:text>, </xsl:text>


my output:
<sec>PtPt 9E, 30G, 30H, 30I, ss ss 47M, 79H, 104ZC, 104ZD, </sec>

Reply With Quote
  #2 (permalink)  
Old December 15th, 2017, 02:07 PM
mhkay's Avatar
Wrox Author
Points: 18,410, Level: 59
Points: 18,410, Level: 59 Points: 18,410, Level: 59 Points: 18,410, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,949
Thanks: 0
Thanked 289 Times in 284 Posts

If you're able to use XSLT 3.0 I would do:

<xsl:for-each-group select="tokenize(., '\s*,\s*')"
    group-starting-with=".[contains(., ' ')]">
   <xsl:variable name="prefix" select="substring-before(., ' ')"/>
   <xsl:value-of select="head(current-group()), 
                                   tail(current-group())!($prefix||' '||.)"
In XSLT 2.0 you can't do group-starting-with on a sequence of atomic values. You could achieve the same effect by first turning the sequence of atomic values into a sequence of text nodes, and then grouping the text nodes.
Michael Kay
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Split value and insert some character value in particular node values bharathidasang XSLT 4 October 13th, 2017 02:05 AM
Converting normal text to UTF values using "character map" ROCXY XSLT 1 June 8th, 2011 02:43 PM
split string based on Character count ptn77 XSLT 14 September 1st, 2010 01:03 PM
Split field at designated character but at " " neo_jakey Classic ASP Professional 0 March 14th, 2005 11:31 AM
To split values and store in the database lily611 SQL Server 2000 3 July 15th, 2004 10:01 AM

All times are GMT -4. The time now is 05:15 PM.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.