 |
| 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
|
|
|
|

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

April 16th, 2008, 03:04 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
|

April 16th, 2008, 03:19 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

April 16th, 2008, 01:37 PM
|
|
Authorized User
|
|
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>
|
|

April 16th, 2008, 02:47 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Please read the previous two helpful posts more carefully as they already contain the answer.
/- Sam Judson : Wrox Technical Editor -/
|
|

April 16th, 2008, 03:04 PM
|
|
Authorized User
|
|
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 16th, 2008, 03:12 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
|

April 16th, 2008, 04:09 PM
|
|
Authorized User
|
|
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

April 16th, 2008, 04:13 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
|

April 16th, 2008, 05:25 PM
|
|
Authorized User
|
|
Join Date: Jan 2008
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |