XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (685 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
12.46Mb size Format: txt, pdf, ePub

If the output is not XML or HTML at all, then rather than using
disable-output-escaping
, it is better to set
method=“text”
on the

element. In this case, special characters will never be escaped (which also means that disabling output escaping has no effect).

Why disable-output-escaping Is Deprecated

The use of
disable-output-escaping
is often discouraged; indeed it is officially deprecated in XSLT 2.0.

The first reason for this is that it works only if the result tree is being serialized. If the result tree is fed directly into another application, then
disable-output-escaping
has no effect. This happens, for example, in the Firefox browser, where the HTML-structured result tree is used directly by the rendering engine, without first serializing it as text and then re-parsing it. So a stylesheet that depends on
disable-output-escaping
won't always work.

The second reason that the facility is discouraged is that it's often a symptom of careless programming: Its use reveals that the stylesheet author is thinking too much in terms of creating tags in a serialized file, not in terms of creating nodes in a result tree.

Here's an example of a misuse of disabling output escaping that you will often encounter (only in other people's stylesheets, of course). The author wanted to get markup tags into the output document, and they couldn't see how to achieve this with the regular facilities of

or literal result elements. For example, the author might have been thinking along these lines:



   

      


       

       


  •    

          


   



The intended effect here is to output a


    tag if the preceding element is not a bullet element, and to output a

tag when the following element is not a bullet element. Of course, it doesn't work, because the

    and

tags are not properly nested; this template will be thrown out by the XML parser before the XSLT processor even gets to look at it.

So their next thought might be to write the tags as text, as follows:


   

      <ul>

   

   


  •    

          /<ul>

       


    You now have something that is legal XML and indeed legal XSLT, but it's not guaranteed to work under all circumstances. And even if it does work, it's badly written code, because it's cutting against the grain of the language.

    With a bit of thought you can usually find a way to achieve the output you want without resorting to such devices.

    The first thing is to think in terms of outputting a result tree containing nodes, not a text file containing tags. Don't try to generate the


      start tag and the

    end tag as two separate actions; try to generate a

      element node as a single action, and then generate its children.

      In fact, when you see this kind of logic, you can be pretty sure that the problem being tackled is a grouping problem. The solution to a grouping problem always involves two nested loops: In this case, an outer loop to generate the


        element and an inner loop to generate the

      • elements. The solution to this particular grouping problem is shown as the first example of how to use the
        group-adjacent
        attribute of

        , on page 335 in Chapter 6.

        Using disable-output-escaping to Wrap HTML in CDATA

        One technique used quite often is to wrap HTML inside an XML document, for example:


          


            Dept 178

            App 263

          


          

            

               

                  An HTML page with unmatched tags

                  HTML authors are often lazy!


               

            ]]>

          


        One way you can include the HTML within the XML message is to put it through a program such as Dave Raggett's
        html tidy
        utility (available from
        http://www.w3.org/
        ), which converts it to well-formed XHTML. But you may not want to risk changing it, so using a
        CDATA
        section as shown here is the only alternative. When you do this, however, the
        <
        and
        >
        characters are no longer treated as markup characters; they are now ordinary text. If you try to run an XSLT transformation that outputs the HTML enclosed in this message, these characters will therefore be escaped, typically by writing them as
        <
        and
        >
        . This isn't what you want; so you can solve the problem by writing:

    Other books

    Betting on You by Sydney Landon
    Inventing Ireland by Declan Kiberd
    Myrmidon by David Wellington
    Anatomy of a Misfit by Andrea Portes
    Death in a White Tie by Ngaio Marsh
    Fearless Magic by Rachel Higginson