Re: [xsl] macthed element lost structures?

Subject: Re: [xsl] macthed element lost structures?
From: "J.Pietschmann" <j3322ptm@xxxxxxxx>
Date: Sat, 27 Mar 2004 23:28:16 +0100
lech wrote:
> my xsl lost all marks of matched elements
> I want the output to be    <FirstStream><name>     TheFirstName    </name>
> <data>  TheFirstData   </data> /FirstStream>....
> 
> But the output I got is:    <FirstStream>    TheFirstName    TheFirstData
> </FirstStream>....
> 
> the" <name>" and "<data>"is lost,  does anybody have some idea why?

You probaly run into default templates. The problem is here:

> <xsl:template match="overall">
> <FirstStream><xsl:apply-templates select="stream1"/>
               ^^^^^^^^^^^^^^^^^^^^^^^^^
You tell the processor to recursively apply templates.
The processor looks for matching templates, and if there
aren't any, it falls back to the default template, which
simply applies templates to child nodes. It ultimately
copies text nodes, but wont copy elements or attributes.

If you want to have a literal copy of the subtree, use
xsl:copy-of instead of xsl:apply-templates:

 <xsl:template match="overall">
   <FirstStream>
     <xsl:copy-of select="stream1"/>
   </FirstStream>
   <SecondStream>
     <xsl:copy-of select="stream2|stream3"/>
   <SecondStream>
 </xsl:template>
This is also very fast.

If you have some other templates you want kick in on elements
deeper down the tree, add a generic copy-through template to
your style sheet:
 <xsl:template match="node()|@*">
    <xsl:copy>
       <xsl:apply-template select="node()|@*"/>
    </xsl:copy>
 </xsl:template>
This template matches everything and will therefore be used
rather than falling back to the default templates.

See
 http://www.w3.org/TR/xslt#built-in-rule
and
 http://www.w3.org/TR/xslt#copying
for more details.

J.Pietschmann

Current Thread