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 April 16th, 2008, 12:32 AM
Authorized User
 
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default apply-template from included xslt stylesheet

Hi, I am trying to apply some templates from an included style sheet, but it says named template does not exist. I've tried using:

<apply-templates select="Title" />
<call-template select="Title" />

none of them work.

The overall layout of the style sheet is this:

Code:
// common.xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" />

  <xsl:template match="Title">    
      <b>Title</b>
   </xsl:template>

</xsl:stylesheet>

Code:
// title.xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:include href="common.xslt" />
  <xsl:output method="html" indent="yes" />

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

</xsl:stylesheet>
 
Old April 16th, 2008, 03:04 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

xsl:call-template using the 'name' attribute to call a template with the same 'name' attribute.

http://www.w3schools.com/xsl/el_call-template.asp

xsl:apply-template should work in this instance, except you seem to have forgotten to include the "xslt:" before "apply-template". Also, this wont output anything if the root element doesn't have a child element called 'Title'.

/- Sam Judson : Wrox Technical Editor -/
 
Old April 16th, 2008, 03:19 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you show us one thing that didn't work then we can tell you why it didn't work. If you say you tried lots of things then it's more difficult.

When you use xsl:call-template, it must take the form <xsl:call-template name="Title"/> (note "name", not "select") and there must be a named template somewhere <xsl:template name="Title"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 16th, 2008, 01:37 PM
Authorized User
 
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm trying to use either apply-templates, or call-template in the title.xslt file, but none of them work.

When I use apply-templates in title.xslt nothing shows up,it's as if nothing is being applied.

When I try call-template using the name of the template, it comes up with "The named template title does not exist".


// common.xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" />

  <xsl:template match="Title">
      <b>Title</b>
   </xsl:template>

</xsl:stylesheet>



// title.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:include href="common.xslt" />
  <xsl:output method="html" indent="yes" />

  <xsl:template match="/">
     <div>
      1. <xsl:apply-templates select="Title" />
      2. <xsl:call-template name="Title" />
     </div>
   </xsl:template>

</xsl:stylesheet>


 
Old April 16th, 2008, 02:47 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Please read the previous two helpful posts more carefully as they already contain the answer.

/- Sam Judson : Wrox Technical Editor -/
 
Old April 16th, 2008, 03:04 PM
Authorized User
 
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry for being a bit slow here, but I don't get it... I just want to use an included template in another xslt file, what am I doing wrong?

I went back and I noticed this statement
"Also, this wont output anything if the root element doesn't have a child element called 'Title'."

If that is the case then how can I use included templates in the context of my example? Thanks
 
Old April 16th, 2008, 03:12 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I was referring to this:

Quote:
quote: xsl:call-template using the 'name' attribute to call a template with the same 'name' attribute.
and this:

Quote:
quote:When you use xsl:call-template, it must take the form <xsl:call-template name="Title"/> (note "name", not "select") and there must be a named template somewhere <xsl:template name="Title"/>

/- Sam Judson : Wrox Technical Editor -/
 
Old April 16th, 2008, 04:09 PM
Authorized User
 
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I did change the call-template to use "name" instead of "select" - see my second post.

Quote:
quote:there must be a named template somewhere <xsl:template name="Title"/>
There is, but it is in the style sheet I am trying to include. When I try to reference that template it doesn't work; to be more specific when I reference it with call-template it tells me the name does not exist, when I reference it with apply-template nothing shows up.

My complete example is in my second post, thanks.
 
Old April 16th, 2008, 04:13 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Your example post has template with match="Title". it should be name="Title".

Match is used when you are trying to automatically match against an XML element from your input XML document (likewise with apply-template, which will search for the best matching template for the XML elements you specify in the select attribute).

For calling a named template, both call-template and the template must have the same name attribute.

/- Sam Judson : Wrox Technical Editor -/
 
Old April 16th, 2008, 05:25 PM
Authorized User
 
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That did the trick, thanks! My head's still spinning trying to wrap my brain around this. Just to reiterate, since the template I made is my own named template and it doesn't match anything in an input xml document, I have to use call-template; apply-templates is used when the template matches some element in an input xml document using a select expression? Thanks for your patience






Similar Threads
Thread Thread Starter Forum Replies Last Post
Question on Apply Template vvenk XSLT 7 July 10th, 2008 12:21 PM
Difference between call-template ,apply-templates vikkiefd XSLT 4 March 12th, 2008 05:09 AM
Included XSLT and use as object YoungLuke C# 2 October 31st, 2007 04:40 AM
Looking for developers for a XSLT stylesheet david_n XSLT 0 February 27th, 2007 10:03 AM
Apply Custom template & import data from text file shampabera Excel VBA 2 August 8th, 2005 07:41 AM





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