Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 10th, 2006, 07:30 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
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
 
Old February 10th, 2006, 07:54 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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





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





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