Re: [xsl] problem adding a new element to an XML document using xsl:element...

Subject: Re: [xsl] problem adding a new element to an XML document using xsl:element...
From: Garry Cronin <garry.cronin@xxxxxxxxxx>
Date: Thu, 23 Sep 2004 15:52:55 +0100
Thanks David. I used your suggestion i.e.

<xsl:template match="ROWSET">
  <ROWSET>
  <TABLE>mytable</TABLE>
   <xsl:apply-templates/>
  </ROWSET>
 </xsl:template>

and it works a treat.

I notice I still have to include the <ROWSET> and </ROWSET> literals in the stylesheet. Can't I simply instruct the stylesheet to output the existing ROWSET elements unchanged, without having to repeat them in the stylesheet. The current solution implies that I would have to manually recontruct any attributes the ROWSET element might have in the input XML?

- Garry

David Carlisle wrote:

Any ideas what I'm doing wrong?


two things


a) you want to put your table element before the result of applying
templates to the input, but you have

 <xsl:template match="ROWSET">
  <xsl:element name="TABLE">
     <xsl:text>mytable</xsl:text>
     <xsl:apply-templates/>
   </xsl:element>
 </xsl:template>

which applies templates )inside_ the table element so it puts everything
inside the table so you want

  <xsl:template match="ROWSET">
  <ROWSET>
  <xsl:element name="TABLE">
     <xsl:text>mytable</xsl:text>
   </xsl:element>
   <xsl:apply-templates/>
  </ROWSET>
 </xsl:template>

or equivalently

  <xsl:template match="ROWSET">
  <ROWSET>
  <TABLE>mytable</TABLE>
   <xsl:apply-templates/>
  </ROWSET>
 </xsl:template>

then you are applying templates to the children of ROWSET but you have
no templates matching those elements so you get the result of the
default template, which just results in the character data and no
element markup (compare with the result of a stylesheet that just has an
empty xsl:stylesheet element and no template children).

You could add a template for * that does the identity transform (copied
from the XSLT spec or the archives of this list or the faq) but if you
want the whole tree copied there is no need to use apply templates at
all you can just copy the tree:


<xsl:template match="ROWSET"> <ROWSET> <TABLE>mytable</TABLE> <xsl:copy-of select="*"/> </ROWSET> </xsl:template>

David

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Current Thread