Re: [xsl] replacing < > in template

Subject: Re: [xsl] replacing < > in template
From: Mike Brown <mike@xxxxxxxx>
Date: Sat, 18 May 2002 13:59:55 -0600 (MDT)
Mattias Konradsson wrote:
>    <example>
>        <menu id="t1" pageid="menu1" >
>             <submenu id="t3" label="File">
>                  <menuitem id="t4"  label="Quit" doFunction="window.close()"
> />
>             </submenu>
>       </menu>
>    </example>
> 
> What I'd want is that all the code withing example should be shown, so the
> tags needs to be replaced with
>  entities, my question is if you in xslt can do a template for example so
> that all tags within it regardless of whether they have templates elsewhere
> get's their "<" ,">", "&"  etc replaced by html entities? Possible?

In XSLT you only have access to the XPath/XSLT node tree obtained after
parsing the XML. There are some pieces of information that are in the
original, serialized document that you won't see in the node tree. This is
because it is the parser's job to only report the logical parts of the
document, the things intended for the application -- like elements,
attributes, Unicode character data -- not the stuff that was intended only for
the parser -- like encoding, the DTD, and entities.

Among the things you will not be able to preserve:
  - order of attributes
  - type of quotes used around attribute values (' or ")
  - extraneous whitespace in attribute values
  - entity references ("&amp;", "&foo;")
  - character references ("&#xA;", "&#10;")
  - XML declaration and info about the original encoding
  - document type declaration and the DTD
  - comments (the parser may or may not report them)
  - extraneous whitespace in element start/end tags
  - whether empty elements use 1 tag ("<foo/>") or 2 ("<foo></foo>")

If you can live without those things, then you can look at your node tree and 
construct a simulation of how it would look when serialized in XML syntax:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:preserve-space elements="*"/>
  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:apply-templates mode="serialize"/>
  </xsl:template>

  <xsl:template match="*" mode="serialize">
    <xsl:value-of select="concat('&lt;',name())"/>
    <xsl:variable name="q">"</xsl:variable>
    <xsl:for-each select="@*">
      <xsl:value-of select="concat(' ',name(),'=',$q,.,$q)"/>
    </xsl:for-each>
    <xsl:choose>
      <xsl:when test="node()">
        <xsl:text>&gt;</xsl:text>
        <xsl:apply-templates mode="serialize"/>
        <xsl:value-of select="concat('&lt;/',name(),'&gt;')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>/&gt;</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="comment()" mode="serialize">
    <xsl:value-of select="concat('&lt;--',.,'--&gt;')"/>
  </xsl:template>

  <xsl:template match="processing-instruction()" mode="serialize">
    <xsl:value-of select="concat('&lt;?',.,'?&gt;')"/>
  </xsl:template>

  <!-- built-in templates for root and text nodes,
       with mode="serialize", will suffice -->

</xsl:stylesheet>

   - Mike
____________________________________________________________________________
  mike j. brown                   |  xml/xslt: http://skew.org/xml/
  denver/boulder, colorado, usa   |  resume: http://skew.org/~mike/resume/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread