Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 29th, 2006, 01:27 PM
Registered User
 
Join Date: Sep 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default My first XSLT - 2 questions

Hi,
     I am developing my first XSLT scripts. I have 2 questions. Any help will be appreciated.
1. I have a phone number element in the input XML which must be reformatted. The phone # apperas multiple times(mobile , work, home). I have the following to transform the phone number which is working fine:
Code:
    <xsl:for-each select="n1:mobile">
                                        <xsl:variable name="curr_val" select="."/>
                                        <xsl:variable name="del_char" select="'()-'"/>
                                        <xsl:variable name="replace_char" select="''"/>
                                        <xsl:variable name="translate_result" select="translate($curr_val, $del_char, $replace_char)"/>
                                        <xsdLocal:MobilePhone>
                                            <xsl:value-of select="$translate_result"/>
                                        </xsdLocal:MobilePhone>
                                </xsl:for-each>
Now my question is, is there a way to reuse this transformation by having it saved in some common place accessible to multiple XSLT files? Do I have to rewrite this code for every phone element?

2.I have another field for system time. The current specifications don't say the format of the time. I have to convert that to GMT timezone. How can I do this.

I tried to find something on internet. So far didn't find anything.

Thnaks,

Reply With Quote
  #2 (permalink)  
Old September 29th, 2006, 02:19 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

On the first question, try to get into the habit of using xsl:apply-templates rather than xsl:for-each. Put the code for handling phone numbers in a separate template, and apply-templates whenever you want to invoke it. The details depend on whether the various phone numbers use the same element name or different element names, but the general approach works either way. If you want to invoke the template from more that one stylesheet, put it in a separate module which you can pull in using xsl:import or xsl:include.

On the second question, there's a lot of support for date and time manipulation in XSLT 2.0, but none in 1.0. If you're using a 1.0 processor you have to do it the hard way, or by calling out to another language.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old October 2nd, 2006, 07:22 AM
Registered User
 
Join Date: Sep 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks mhkay. I created the templates for formattign different phone numbers and date. I am using an import statement as I I needed formatting in multiple files.
I replaced the above piece of script with
Code:
<xsl:for-each select="n1:phone">
                                    <xsl:for-each select="n1:mobile">                                        
                                        <xsdLocal:MobilePhone>
                                                <xsl:call-template name="format_phone">
                                                    <xsl:with-param name="in_phone" select="."/>
                                                </xsl:call-template>
                                        </xsdLocal:MobilePhone>
                                    </xsl:for-each>
                                </xsl:for-each>
Regarding for-each, did you mean I should be able to completely get rid of them ?
Thanks,

Reply With Quote
  #4 (permalink)  
Old October 2nd, 2006, 08:54 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Yes, it's a good idea to try to write stylesheets that don't use xsl:for-each at all. WHen you've achieved that, you will understand xsl:apply-templates, and you will then be able to judge when it's appropriate to use apply-templates and when it's better to use for-each. The danger for beginners is using for-each because it feels more familiar.

I would write this code:

<xsl:for-each select="n1:phone">
                                    <xsl:for-each select="n1:mobile">
                                        <xsdLocal:MobilePhone>
                                                <xsl:call-template name="format_phone">
                                                    <xsl:with-param name="in_phone" select="."/>
                                                </xsl:call-template>
                                        </xsdLocal:MobilePhone>
                                    </xsl:for-each>
                                </xsl:for-each>

as

<xsl:apply-templates select="n1:phone"/>

<xsl:template match="n1:phone">
  <xsl:apply-templates select="n1:mobile"/>
</xsl:template>

<xsl:template match="n1:mobile">
  <xsdLocal:MobilePhone">
    ... code from your named template format-phone ...
  </xslLocal:MobilePhone>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
.NET Interview Questions, C# Interview Questions, dotnetuncle .NET Framework 2.0 4 June 22nd, 2019 07:03 AM
General XSLT Questions in the XSLT Forum jminatel BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 0 March 31st, 2008 07:50 PM
2 Questions on XSLT for RSS Feed Tachyon XSLT 4 June 14th, 2007 06:00 PM
Can XSLT read DTD/schema and Generate XSLT.. ROCXY XSLT 1 November 6th, 2006 09:39 AM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM



All times are GMT -4. The time now is 09:05 AM.


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