xsl-list
[Top] [All Lists]

RE: Add an element to the result xml document

2002-10-08 01:58:27
I have sucessfully copied all the elements in source xml to 
result xml but I also want duplicates of some elements.  In 
the partial xml doc below I want the result to contain two 
copies of the <MessageID> and </MessageID> elements.

I am using this XSLT:

<!-- copy all the elements to the result document --> 
<xsl:template match="/">
      <xsl:copy-of select="."/>
</xsl:template>

Anything else in your stylesheet is irrelevant. You come in at the root
node, you copy the entire source tree to the output, and you exit. The
simplest transformation there is. Not hard to be successful at that one!

If you want to do anything other that a straight copy, the first thing
to do is to get rid of the <xsl:copy-of> and write
<xsl:apply-templates>. Then you can start writing template rules that
describe how you want each node to be processed. By default you probably
want nodes unchanged, so write

<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

and then add template rules for any nodes which you don't want to copy
exactly.

For example (I can't see why you would want to do this):

<xsl:template match="MessageID">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
<!-- and again... -->
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

Michael Kay
Software AG
home: Michael(_dot_)H(_dot_)Kay(_at_)ntlworld(_dot_)com
work: Michael(_dot_)Kay(_at_)softwareag(_dot_)com 


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>