Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 29th, 2006, 12:16 AM
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to force XSLT to output a close tag in XML?

I'm using XSLT to generate XHTML-compliant code, and I'm trying to figure out a way to force it to include the close tag on an empty element.

The element in question is <textarea>. I've tried a few solutions, and am using some on other elements, but they dont work with textarea. To be clear,
  • output: html won't work, because it needs to be valid XML
  • Inserting dummy characters like '' inside won't work, because everything inside a <textarea> is treated like CDATA and is included in the field as editable text. Using a space or a return isn't a good solution either because then the form will submit it as part of the data (unless I hack every page on the server to remove it, which I'd rather not.)
I do have a solution, but it's extremely ugly, and won't work if I ever pass the XSLT processing onto the browser (since Firefox won't let you insert elements this way). But it works for now:

    <xsl:text disable-output-escaping="yes"><![CDATA[<textarea id="input_description" name="description" cols="45" rows="5">]]></xsl:text>
        <xsl:if test="description"><xsl:value-of select="description" disable-output-escaping="yes" /></xsl:if>
    <xsl:text disable-output-escaping="yes"><![CDATA[</textarea>]]></xsl:text>

If I want to include generated content inside the <textarea> element, this gets very ugly very fast.

Anyone else have a solution?

Thanks!
Reply With Quote
  #2 (permalink)  
Old June 29th, 2006, 02:37 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

XSLT outputs a result tree, it doesn't output tags (unless you use the disable-output-escaping hack). It's the serializer that takes the result tree and produces appropriate tags.

If you're generating XML, use <xsl:output method="xml"/>. The serializer will decide whether to use <textarea></textarea> or <textarea/>. In XML, both forms are completely equivalent, so any consumer of the XML will accept either.

If you're generating HTML, similarly, use <xsl:output method="html"/>, and the serializer will decide which form of tagging is appropriate.

If you're generating output to be read by something other than an XML parser or an HTML browser, then you may have a problem. The official answer in such cases is to write your own serializer.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old June 29th, 2006, 02:39 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Oh, I should have said that XSLT 2.0 has <xsl:output method="xhtml"/>, which will try to produce output that is valid XML but respects the conventions for making the XML acceptable to legacy browsers. This option isn't available in standard XSLT 1.0 though it's offered by some XSLT 1.0 processors. With 1.0, the best answer is to generate two outputs: HTML for the browser, and XML for everyone else.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
copying a tag to output based on a query anboss XSLT 6 July 17th, 2008 11:10 AM
Extra xmlns="" tag in output Mango_Lier XSLT 4 October 16th, 2007 11:35 AM
How do I transform XSLT without the <?xml?> tag? nadavvin XSLT 4 June 10th, 2007 09:25 AM
how to get xml tag names in xslt mdawoodk XSLT 8 March 14th, 2005 05:55 AM
Trying to output a tag within a tag jaucourt XSLT 3 January 12th, 2005 11:57 AM



All times are GMT -4. The time now is 04:07 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.