Solved

XSLT

Posted on 2008-10-23
8
617 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

828 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