Friday, December 19, 2008

XSLT note 1 - built-in template

A simple xslt is a empty xslt.

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> <xsl:output method="xml" indent="yes"/> </xsl:stylesheet>

But it does something, it actually is equivalent to the following style sheet. Those template are built-in and can not be removed.

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> <xsl:output method="xml" indent="yes"/> <xsl:template match="* | /"> <xsl:apply-templates /> </xsl:template> <xsl:template match="text() | @*"> <xsl:value-of select="." /> </xsl:template> <xsl:template match="processing-instruction() | comment()" /> </xsl:stylesheet>

Let's take a look what is the function of the template. The first template <xsl:template match="* | /"> apply all the nodes match "* | /" with this template. * is any node, "/" is root node. The <xsl:apply-templates> element first selects a set of nodes using the expression specified in the select attribute. If this attribute is left unspecified, which here is this case, all children of the current node are selected. For each of the selected nodes, <xsl:apply-templates> directs the XSLT processor to find an appropriate <xsl:template> to apply. Templates are tested for applicability by comparing the node to the XPath expression specified in the template's match attribute. If more than one template satisfies the match pattern, the one appearing with the highest priority is chosen. If several templates have the same priority, the last in the style sheet is chosen.

It seems that <xsl:apply-templates /> is same as <xsl:apply-templates select="*" />, but it is not. It is same as <xsl:apply-templates select="* | text() | processing-instruction() | comment() "/>. If we want attribute is included we should use <xsl:apply-templates select="* | @* | text() | processing-instruction() | comment() "/>. This template basically output all the value of element nodes.

No comments:

Post a Comment