tags:

views:

101

answers:

2

hi! where should I use <copy-of> and <apply-templates> ? I mean where they play unique role .. Most of the time replacing <apply-templates> with <copy-of> gives out weird output .. why is that?

thanx in advance ..

+4  A: 
copy-of

will simply return you a dump of the XML in the supplied node-set

apply-templates

on the other hand will apply any templates applicable to the node-set passed it.

Tim
+5  A: 

copy-of is an exact copy of the matched input xml element. No xslt processing takes place and the output from that element will be exactly the same as the input.

apply-templates tells the xslt engine to process templates that match the selected elements. Apply-Templates is what gives xslt its overriding capability, since the templates you create with match on elements can have different priorities, and the template with the highest priority will be executed.

Input:

<a>
   <b>asdf</b>
   <b title="asdf">asdf</b>
</a>

Xslt 1:

<xsl:stylesheet ... >
   <xsl:template match="a">
        <xsl:copy-of select="b" />
   </xsl:template>
</xsl:stylesheet>

Xml output 1:

<b>asdf</b>
<b title="asdf">asdf</b>

Xslt 2:

<xsl:stylesheet ... >
   <xsl:template match="a">
        <xsl:apply-templates select="b" />
   </xsl:template>

   <xsl:template match="b" priority="0">
        <b><xsl:value-of select="." /></b>
        <c><xsL:value-of select="." /></c>
   </xsl:template>

   <xsl:template match="b[@title='asdf']" priority="1">
       <b title="{@title}"><xsl:value-of select="@title" /></b>
   </xsl:template>
</xsl:stylesheet>

Xml output 2:

<b>asdf</b>
<c>asdf</c>
<b title="asdf">asdf</b>
LorenVS
ohk .. fine thanx .. sorry my reputation is not 15 yet so couldn't vote you up .. :)
infant programmer