Solved

XSLT

Posted on 2008-10-23
8
616 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
ID: 22790255
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
ID: 22790433
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
ID: 22790497
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Expert Comment

by:ramuexpert
ID: 22791271
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 22794017
@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
ID: 22796530
@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
ID: 22834886
@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
ID: 22845415
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I starting with php 12 133
Data at the root level is invalid. Line 1, position 1 8 150
Form submit issue 11 85
MS SQL Database Generating XML using query 21 44
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

832 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