Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can an xslt template carry both name and match attributes?

Tags:

xslt

Is the following (a) allowed (b) useful

<xsl:template match="foo" name="bar"> 
</xsl:template>

(it means that the template could be triggered either from recursive template processing or directly from <xsl:call-template name="bar"/>

like image 710
peter.murray.rust Avatar asked Jun 25 '11 13:06

peter.murray.rust


People also ask

How does xsl template match work?

The <xsl:template> element is used to build templates. The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).

What is the use of template in XSLT?

XSLT Template defines a way to set rules to generate the desired Output in a proper structure for a particular context and to build a template. The template tells the XSLT processor how to format a particular output from the XML source document.

Which XSLT element is used to call a named template?

The <xsl:call-template> element calls a named template.

What is the difference between call template and apply template in XSLT?

With <xsl:apply-templates> the current node moves on with every iteration, whereas <xsl:call-template> does not change the current node. I.e. the . within a called template refers to the same node as the . in the calling template.


1 Answers

Simply put, yes. I quite often name the identity template and invoke it directly using a <xsl:call-template name="identity" />.

It's a useful tool for a form of inheritance; you can define a template to match one node, and another that handles a derivative of that node that does the specifics, then calls the more general template.

For example:

<xsl:template match="animal" name="animal">
  <!-- handle any animal related stuff here -->
</xsl:template>

<xsl:template match="dog">
  <xsl:call-template name="animal" />
  <!-- handle any dog specific stuff here -->
</xsl:template>
like image 81
Flynn1179 Avatar answered Oct 05 '22 05:10

Flynn1179