Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 11th, 2017, 11:13 AM
Authorized User
 
Join Date: Nov 2016
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Split values and insert some character

Hi,

Can you please anyone help me.

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

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

My code:

<?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="2.0">

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

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

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


<xsl:matching-substring>
<xsl:value-of select="regex-group(1)"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before(.,' ')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:message select="$ddd"></xsl:message>
<xsl:value-of select="concat($ddd, .)"/><xsl:text>, </xsl:text>

</xsl:for-each>
</sec>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

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

Thanks,
Bharathi
 
Old December 15th, 2017, 02:07 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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

Code:
<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||' '||.)"
      separator=","/>
</xsl:for-each-group>
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
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





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 01:05 AM
Converting normal text to UTF values using "character map" ROCXY XSLT 1 June 8th, 2011 01:43 PM
split string based on Character count ptn77 XSLT 14 September 1st, 2010 12: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 09:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.