Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 Display Modes
  #1 (permalink)  
Old February 10th, 2006, 06:30 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: Ennis, Clare, Ireland.
Posts: 127
Thanks: 8
Thanked 0 Times in 0 Posts
Default Templates

Hi,

I have inherited the following code contained within a XSL stylesheet:

********

 <xsl:element name="ShippingInstructions">
        <xsl:attribute name="ShippingType">Delivery</xsl:attribute>
    <xsl:copy-of select="./*"></xsl:copy-of>
      </xsl:element>
    </xsl:template>



  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>


  <xsl:template match="*">
    <xsl:choose>
      <xsl:when test="@ShippingType='Collection'">
        <xsl:call-template name="ShippingType"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="copy"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <xsl:template name="copy">
    <xsl:copy>
      <xsl:for-each select="@*">
        <xsl:copy/>
      </xsl:for-each>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

********

What I want to do is check through the document as above but execute another template outside of the '<xsl:template match="*">' statement if the XML document contains a date or time element (i.e. DateAdd Template (See below)).

<xsl:template match ="DateAdd">

   <xsl:call-template name="DateAdd">
      <xsl:with-param name="Date">20060214 07:00</xsl:with-param>
  </xsl:call-template>

Can you help me code a statement to check each template or XML element whereby:
1. Template is equal to 'DateAdd'
2. Template is not equal to 'DateAdd'

AND would this mean replacing the '<xsl:template match="*">' statement?

Can someone please help with an example couple of lines. I'm still quite new to XML/XSL.

Thanks,



Neal

A Northern Soul
__________________
Neal

A Northern Soul
Reply With Quote
  #2 (permalink)  
Old February 10th, 2006, 06:54 AM
mhkay's Avatar
Wrox Author
Points: 17,770, Level: 58
Points: 17,770, Level: 58 Points: 17,770, Level: 58 Points: 17,770, Level: 58
Activity: 62%
Activity: 62% Activity: 62% Activity: 62%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,838
Thanks: 0
Thanked 267 Times in 262 Posts
Default

Firstly, I hate to see a template rule whose whole body is an xsl:choose. Split

<xsl:template match="*">
    <xsl:choose>
      <xsl:when test="@ShippingType='Collection'">
        <xsl:call-template name="ShippingType"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="copy"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

into:

<xsl:template match="*">
    <xsl:call-template name="copy"/>
</xsl:template>

and

<xsl:template match="*[@ShippingType='Collection']">
   <xsl:call-template name="ShippingType"/>
</xsl:template>

and then simplify further, because a template can have both a name and a match pattern so you can do:

<xsl:template name="ShippingType" match="*[@ShippingType='Collection']">
....
</xsl:template>

Then you can just add your match="DateAdd" template as another rule. There's a built-in set of priority rules if the same element matches multiple rules, for example match="XYZ[A='2']" takes precedence over match="XYZ" which takes precedence over match="*". But if things get more complicated (or if you want to be explicit) you can attach explicit priorities with the "priority" attribute.






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


Thread Tools
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
"templates" Somesh ASP.NET 2.0 Professional 8 March 27th, 2007 08:53 AM
Templates pilcher85 Dreamweaver (all versions) 1 March 21st, 2007 05:51 PM
Templates Won't Apply neilac333 XSLT 5 October 26th, 2006 01:39 PM
Templates ervinel Classic ASP Basics 0 July 13th, 2006 02:47 PM
Class templates shenku ASP.NET 1.0 and 1.1 Professional 1 May 10th, 2005 08:32 AM



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


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