473,387 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to process text and numbers seperately in a given element?

Hi,
I want to process numbers in a different fashion:

My input is something like:
<A>Ankush 123 Singhal 23 man</A>

I want output as:
<Doc>Ankush <num>123</num> Singhal <num>23> man</Doc>

How can we achieve this functionality using XSLT. I am using xalan
2.4.1

Thanks in advance,
Ankush

Apr 12 '06 #1
4 1255
I want output as:
<Doc>Ankush <num>123</num>Singhal <num>23</num>man</Doc>

Apr 12 '06 #2


Ankush wrote:

I want to process numbers in a different fashion:

My input is something like:
<A>Ankush 123 Singhal 23 man</A>

I want output as:
<Doc>Ankush <num>123</num> Singhal <num>23> man</Doc>

How can we achieve this functionality using XSLT. I am using xalan
2.4.1


That is difficult to achieve with pure XSLT and XPath 1.0. You have to
write a named template with parameters passing in the complete string
and recursively call it splitting the string as necessary.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 12 '06 #3
As Martin said, you can use a stylesheet like the one below:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="A">
<Doc>
<xsl:call-template name="markNumbers">
<xsl:with-param name="value" select="normalize-space(.)"/>
</xsl:call-template>
</Doc>
</xsl:template>

<xsl:template name="markNumbers">
<xsl:param name="value" select="''"/>
<xsl:choose>
<xsl:when test="contains($value, ' ')">
<xsl:call-template name="markNumber">
<xsl:with-param name="value" select="substring-before($value,
' ')"/>
</xsl:call-template>
<xsl:text> </xsl:text>
<xsl:call-template name="markNumbers">
<xsl:with-param name="value" select="substring-after($value,
' ')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="markNumber">
<xsl:with-param name="value" select="$value"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="markNumber">
<xsl:param name="value" select="''"/>
<xsl:choose>
<xsl:when test="translate($value, '0123456789', '')=''">
<num><xsl:value-of select="$value"/></num>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$value"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

It will give the following output when applied on your sample input:

<?xml version="1.0" encoding="UTF-8"?><Doc>Ankush <num>123</num>
Singhal <num>23</num> man</Doc>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Apr 12 '06 #4
Thanks that helps a lot!!----In fact that was exactly what I was
looking for!!

Thanks
Ankush

George Bina wrote:
As Martin said, you can use a stylesheet like the one below:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="A">
<Doc>
<xsl:call-template name="markNumbers">
<xsl:with-param name="value" select="normalize-space(.)"/>
</xsl:call-template>
</Doc>
</xsl:template>

<xsl:template name="markNumbers">
<xsl:param name="value" select="''"/>
<xsl:choose>
<xsl:when test="contains($value, ' ')">
<xsl:call-template name="markNumber">
<xsl:with-param name="value" select="substring-before($value,
' ')"/>
</xsl:call-template>
<xsl:text> </xsl:text>
<xsl:call-template name="markNumbers">
<xsl:with-param name="value" select="substring-after($value,
' ')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="markNumber">
<xsl:with-param name="value" select="$value"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="markNumber">
<xsl:param name="value" select="''"/>
<xsl:choose>
<xsl:when test="translate($value, '0123456789', '')=''">
<num><xsl:value-of select="$value"/></num>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$value"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

It will give the following output when applied on your sample input:

<?xml version="1.0" encoding="UTF-8"?><Doc>Ankush <num>123</num>
Singhal <num>23</num> man</Doc>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com


Apr 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Emil Karlen | last post by:
I am modelling a software system in UML using Umbrello, which stores the UML-model in a XMI-file (which exact type I only guess). Now, my goal is to: have a XSL-transformation (which I call the...
7
by: MgGuigg | last post by:
Hello all, This is my first time posting a question to this forum, so here is hoping I am following protocol. I am scraping the rust off my old Basic programming skills, and have just recently...
4
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person...
335
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
3
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user...
9
by: Steven Bethard | last post by:
I have some text and a list of Element objects and their offsets, e.g.:: ... (etree.Element('a'), 0, 21), ... (etree.Element('b'), 11, 18), ... (etree.Element('c'), 18, 18), ... ] ...
0
by: JosAH | last post by:
Greetings, the last two article parts described the design and implementation of the text Processor which spoonfeeds paragraphs of text to the LibraryBuilder. The latter object organizes, cleans...
1
by: littlealex | last post by:
IE6 not displaying text correctly - IE 7 & Firefox 3 are fine! Need some help with this as fairly new to CSS! In IE6 the text for the following page doesn't display properly - rather than being...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.