[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Newbie issue with apply-templates and output
Hi,
At 11:21 AM 3/11/2005, you wrote: I'm quite new to all this XSL stuff and have now got very confused with what's happening with the output using my XSL - it's dumping out the text from nodes even tough I've not specified in the XSL to do this. That's because there are built-in templates that provide for this automatically. As another post suggested. However, it went on to suggest you suppress the built-in template for text nodes by saying <xsl:template match="text()"/> which I think is almost certainly a bad idea, even though it works on your small example. If you adopt this approach to coding your XSLT, you'll find it doesn't scale gracefully, and that you have to write lots of other code to get it to work ... when things could be *much* simpler if you simply used that built-in default template, and instead changed your template matching "element" from (I've commented it up) <xsl:template match="element"> <temp><xsl:value-of select="."/></temp> <!-- creates a 'temp' element containing the current node's value --> <xsl:apply-templates/> <!-- traverses to the child nodes and processes them using best matching templates (including built-in templates if necessary) --> </xsl:template> to the simpler: <xsl:template match="element"> <temp> <xsl:apply-templates/> </temp> </xsl:template> This will have the same effect, and won't have the unfortunate consequence of forcing you to write "value-of" statements everywhere else in your stylesheet as it gets more complex. To suppress that extra text, you want a template that will suppress it without altering the processing of all the text nodes you do want. So: <xsl:template match="body/text()"/> You may still be confused as to why this works and why it's such a good idea. Research this question by studying the XSLT processing model and what <xsl:apply-templates/> actually does. You'll be glad you did: understanding this is essential to solving almost any non-trivial problem in XSLT. Or to put it a little differently: those built-in default templates are there for a reason, and as an XSLT author you want to know that reason and how to take advantage of it. What you don't want to do is find a way to "work around" a central feature of the language. Cheers, Wendell The code (xml & xsl) is given below with the output - and I'm confused why it's output 'heading 1' etc twice, and why 'I'm here' is being output at all. How can I change my xsl so it only outputs the headings once, and the extra text isn't displayed at all? ====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|