Solved

XSLT

Posted on 2008-10-23
8
611 Views
Last Modified: 2013-11-18
I need XML and XSLT FILES  for formatting a simple US phone number like 517-280-9768 using Regular expressions.

Is there any way to do???Can any one of you send me a sample with any phone number using regular expressions.
I am sure XSLT2.0 supports regular expressions but not sure where to put this line of code and make the example working...
<xsl:analyze:string  select="." regex="([0-9]{{3}})-([0-9]{{4}})">
<xsl:value-of select="regex-group(1)"/>

I would appreicate your help

0
Comment
Question by:onebite2
  • 4
  • 2
  • 2
8 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
If this is your XML

<?xml version="1.0" encoding="UTF-8"?>
<phone>517-280-9768 </phone>

You can use the XSLT attached
<?xml version="1.0" encoding="UTF-8"?>

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

<xsl:template match="phone">

    <xsl:analyze-string  select="." regex="([0-9]{{3}})-([0-9]{{4}})">

        <xsl:matching-substring>

            <xsl:value-of select="regex-group(1)"/>

        </xsl:matching-substring>

        </xsl:analyze-string>

 </xsl:template>

</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:onebite2
Comment Utility
I am getting following error:
<xsl:analyze string> is howing a red line with error..

XslTransformException
---------------------
'regex' is not a recognized extension element. An error occurred at C:\Documents and Settings\c-onebite2\My Documents\Visual Studio 2008\Projects\XML File\phone.xslt(8,3).

Can u tell me what should I do....


0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
That is because you are using an XSLT1 processor
If you use XSLT2, you have to make sure that you have an XSLT2 processor
I strongly recommend Saxon9B
www.saxonica.com
0
 

Expert Comment

by:ramuexpert
Comment Utility
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
Comment Utility
@onebite2,

If you know that the phone number is a correct phone number,
and all you need is the second bit ('280')
This would be an XSLT1 solution for the same problem
(using substring processing since regexes are not available in XSLT1)

@ramuexpert,

I completely miss the point of your post, other then providing a general XSLT1 tutorial (that isn't even very good).
Can you explain how this helps solving this question, before I start reading pages and pages without a clear result.
Please be careful just dumping URLs in responses, you make people loose a lot of time... often for nothing
Thanks

Geert
<?xml version="1.0" encoding="UTF-8"?>

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

    <xsl:template match="phone">

        <xsl:value-of select="substring-before(substring-after(., '-'), '-')"/>

    </xsl:template>

</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:onebite2
Comment Utility
@Gertone

Thanks a lot !!Your answer showed me a way.....but I was wondering if I could show the complete phone number instead of just the middle number like 280......
can you tell me what line of code should be added to get the complete number??

If you are going to write xsl:matching-substring implementations using regex to format the string  how about writing a common xsl document which will have all these functions for phone,zip code, date, time, etc. and then link that common file to all our xsl templates.How can we do this???

Thanks a lot !!!I appreciate if you can help me getting a work around for this..
0
 

Expert Comment

by:ramuexpert
Comment Utility
@Gertone
I am sorry and will take care next time.
My idea is simple if i cannot direct solution , then show some place which might help.
@onebite2:
Were you able to find a fix? Thanks for response.
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
showing the entire phone number is just the <xsl:value-of of the entire element.
   <xsl:template match="phone">
        <xsl:value-of select="."/>
    </xsl:template>

What do you want to do?
It seems that you want to validate wheither a certain field contains a valid phone-number

You can have a general xsl file that contains a number of functions (templates)
and you include that in your XSLT
<xsl:include> is what you need then
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now