XML and the .NET Framework
Using XSLT Template similar to a Method
Question:
Hi all,
I have created the following template which re-formats a date...
<xsl:template name="FormatDate">Answer1:In general templates can have either @name attriubte or @match attribute (examples <xsl:template name="format-date" /> or <xsl:template match="node" />). If a template has @name attribute it's invoked with xsl:call-template instruction and it acts as a function. If a template has @match attribute it will be called automatically if the node being prcessed fullfills condition defined in @match attribute (there is more to this - priorities etc. but for simplicity this explanation should be fine). In both cases you can pass parameters with xsl:with-param instruction. The result of a template (regardless of the @name/@match attribute) is what the template writes to the output tree.
In Xslt 1.0 xsl:template with @name attribute is the way to achieve function like features. In Xslt 2.0 however there are new instructions (xsl:function) which allow to achieve the same in more convenient way. Unfortunately there is no Xslt 2.0 Processor from Microsoft for the time being.
Pawel
Answer2:
Pawel Kadluczka - MSFT wrote: |
|
In general templates can have either @name attriubte or @match attribute (examples <xsl:template name="format-date" /> or <xsl:template match="node" />). If a template has @name attribute it's invoked with xsl:call-template instruction and it acts as a function. If a template has @match attribute it will be called automatically if the node being prcessed fullfills condition defined in @match attribute (there is more to this - priorities etc. but for simplicity this explanation should be fine). In both cases you can pass parameters with xsl:with-param instruction. The result of a template (regardless of the @name/@match attribute) is what the template writes to the output tree.
In Xslt 1.0 xsl:template with @name attribute is the way to achieve function like features. In Xslt 2.0 however there are new instructions (xsl:function) which allow to achieve the same in more convenient way. Unfortunately there is no Xslt 2.0 Processor from Microsoft for the time being.
Pawel
| |
Nothing forbids a template to have *both* the "name" and "match" attributes specified. In fact this can be useful.
The FXSL library implements higher-order functions in XSLT 1.0 and XSLT/XPath 2.0.
More on implementing useful functions in XSLT 1.0 and XSLT 2.0, and in fact implementing higher-order functions, can be found here and here.
Thanks,
Dimitre Novatchev
Answer3:Thanks for teh VERY informative replies both 
I know VS2003 using XSLT 1.0, but does VS2005 also use XSLT 1.0 or is it 2.0?
Tryst
Answer4:The .NET framework 2.0, 3.0, and 3.5 provides System.Xml.Xsl.XslCompiledTransform which performs better than the System.Xml.Xsl.XslTransform in .NET 1.x but both do only implement XSLT and XPath 1.0 and not 2.0. If you want to use XSLT 2.0 with the .NET framework then you need to use third party solutions, there are two, the .NET version of Saxon from http://saxon.sourceforge.net/ and AltovaXML http://www.altova.com/altovaxml.html which is a COM solution but usable in .NET via COM interop.
MVP XML
Answer5:Thanks very much Martin.
Tryst