|
Subject:
|
apply-template from included xslt stylesheet
|
|
Posted By:
|
chobo
|
Post Date:
|
4/16/2008 12:32:21 AM
|
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:
// 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="/">
<apply-templates select="Title" />
</xsl:template>
</xsl:stylesheet>
|
|
Reply By:
|
samjudson
|
Reply Date:
|
4/16/2008 3:04:19 AM
|
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 -/
|
|
Reply By:
|
mhkay
|
Reply Date:
|
4/16/2008 3:19:07 AM
|
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
|
|
Reply By:
|
chobo
|
Reply Date:
|
4/16/2008 1:37:13 PM
|
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>
|
|
Reply By:
|
samjudson
|
Reply Date:
|
4/16/2008 2:47:02 PM
|
Please read the previous two helpful posts more carefully as they already contain the answer.
/- Sam Judson : Wrox Technical Editor -/
|
|
Reply By:
|
chobo
|
Reply Date:
|
4/16/2008 3:04:48 PM
|
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
|
|
Reply By:
|
samjudson
|
Reply Date:
|
4/16/2008 3:12:46 PM
|
I was referring to this:
quote: xsl:call-template using the 'name' attribute to call a template with the same 'name' attribute.
and this:
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 -/
|
|
Reply By:
|
chobo
|
Reply Date:
|
4/16/2008 4:09:33 PM
|
I did change the call-template to use "name" instead of "select" - see my second post.
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.
|
|
Reply By:
|
samjudson
|
Reply Date:
|
4/16/2008 4:13:48 PM
|
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 -/
|
|
Reply By:
|
chobo
|
Reply Date:
|
4/16/2008 5:25:40 PM
|
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
|