XML code in cocoon colors
Here is as an attached file (in hope that will work)
Fastest way to test it, open it in an XSL compliant browser (IE 5.5 under
windows, others ?)
Remove stylesheet processing instruction to compare

About webapp hierarchy
For our apps, we usually share resources (like this one, or tuned error
messages and other xsl, js, css)
They are in a WEB-INF/res directory as if it was updatable (like a jar)
Some xsl seems to be duplicated in cocoon webapp, can't cocoon show a good
practice on this question ?

----- Original Message -----
From: "Joerg Heinicke" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, October 03, 2002 1:41 AM
Subject: Re: generic resources


> Hello Frédéric,
>
> can you provide the code, either (if it's not too much) on the list
> (zipped) or as patch in bugzilla? I'm really interested in some code
> better than simple-xml2html.xsl.
>
> Regards,
>
> Joerg
>
> Frédéric Glorieux wrote:
> >     Hello developpers
> >
> > I'm essentially a cocoon user (degree: coding pipes like serializers and
so
> > on) but I got some tools in my box wich can take a place in webapp
distrib,
> > especially :
> >
> > an xsl stylesheet to show xml source in html, well commented for
adaptation
> > to replace simple-xml2html.xsl
> >     - interesting features
> >             forget MS.IE styles and give your colors to XML
> >             all xmlns:*="uri" attributes
> >             no more "+" to clean-up after copy/paste (but still toggles
on
> > tags, with DOM  conformant JS)
> >             text with <br/> - &amp; - non breakable space
> >     - usage
> >             global xsl on input
> >             as an import on xml blocks (for documentation)
> >
> > Don't you think that cocoon needs is own style to show xml ? Source code
> > given on request, other features possible (if you have ideas).
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, email: [EMAIL PROTECTED]
>
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="xml.xsl"?>
<!--
 |
 | displaying xml source in html
 | [EMAIL PROTECTED]
 |
 | fast test, apply this to itself
 |
 | usage :
 |   as a root xsl matching all elements
 |   as an import xsl to format some xml
 |     with <xsl:apply-templates select="node()" mode="src"/>
 |     in this case you need to copy css and js somewhere to link with
 |
 | features :
 |   DOM compatible toggle
 |   no extra characters to easy copy/paste code
 |   html formatting oriented for logical css
 |   commented to easier adaptation
 |   all xmlns:*="uri" attributes in root node
 |   text reformating ( xml entities )
 |
 | problems :
 |   <![CDATA[ node ]]> can't be shown (processed by xml parser before xsl transformation)
 |   NS6, border don't want to appears
 |
 +-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:saxon="http://saxon.sf.net/";>
    <xsl:output indent="yes" method="html"/>
    <!-- low priority on root node in case of include -->
    <xsl:template match="/" priority="-2">
        <html>
            <head>
                <style xml:lang="en" test="&quot;&amp;&gt;&lt;">

            /* 
            body css properties are applied to operators
            nodes have a class :
                processing-instruction, comment, 
                text, attribute value
                tag name, attribute name
            tag+atts are included in el class
            global formatting classes neeeded for nesting           
             */   

            body 
                {font-family: Verdana, sans-serif; font-size: 90%; 
                color:#FFCC00;}
            .pi 
                {color:#0086b2; font-weight: bold;}
            .comment 
                { margin:0; padding:0.5ex;
                 font-size:100%; line-height:100%;
                font-family: Verdana, sans-serif;
                color:White; background:#959595; font-weight: 100; }
            .text 
                { font-size:100%; color:black; margin:0; margin-left:4ex; padding:0; 
                font-weight:bold; font-family:Verdana, sans-serif;
                }
            .val  
                {color:black; font-family:Verdana, sans-serif; font-weight:bold;}
            .tag  
                {color:#0086b2; font-weight: bold;}
            .att  
                {color:#039acc; }

            .el, .el-toggle
                {background:WhiteSmoke; white-space:nowrap; margin:0}

            /* global formatting */

            .toggle, .el-toggle 
                {cursor: pointer}
            .hide, .hide *
                {text-decoration: underline; }
            .block  
                {margin-left:2ex; padding-left:2ex; border-left: 1 dotted }

            /* ideas 
                    (fixed-width font could be used for showing code) 
                    font-family: monospace, sans-serif; 
            */

                </style>
                <script type="text/javascript">
            // toggle next node, add class "hide" to the toggler
           function toggle(o)
           {
                if (!o.innerHTML || !o.className || !o.nextSibling) return;
                oo=o.nextSibling;
                if (!oo || !oo.style) return;
                oo.style.display=(oo.style.display=='none')?'':'none';
                // change class of the toggler
                o.className=
                    (o.className.search('hide') != -1)?
                     o.className.replace(/ ?hide ?/gi, ''):
                     "hide "+o.className;
                 // close element properly
                o.innerHTML=
                    (o.innerHTML.search('/&amp;gt;') != -1)?
                     o.innerHTML.replace(/\/&amp;gt;/, '&amp;gt;'):
                     o.innerHTML.replace(/&amp;gt;/, '/&amp;gt;');
           }

           </script>
            </head>
            <body>
                <xsl:apply-templates mode="src"/>
            </body>
        </html>
    </xsl:template>
    <!-- PI -->
    <xsl:template match="processing-instruction()" mode="src">
        <div class="pi">
            <xsl:text>&lt;?</xsl:text>
            <xsl:value-of select="name(.)"/>
            <xsl:text>&#32;</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>?></xsl:text>
        </div>
    </xsl:template>
    <!-- xmlns attributes -->
    <xsl:template name="xmlns">
        <xsl:if test=".=/*">
            <xsl:choose>
                <xsl:when xmlns:xalan="http://xml.apache.org/xalan"; test="function-available('xalan:distinct')">
                    <xsl:for-each select="xalan:distinct((//*|//@*)/namespace::*[name()!='xml'])">
                        <xsl:call-template name="att">
                            <xsl:with-param name="name" select="concat('xmlns:', name())"/>
                            <xsl:with-param name="value" select="."/>
                        </xsl:call-template>
                    </xsl:for-each>
                </xsl:when>
                <xsl:when xmlns:saxon="http://saxon.sf.net/"; test="function-available('saxon:distinct')">
                    <xsl:for-each select="saxon:distinct((//*|//@*)/namespace::*[name()!='xml'])">
                        <xsl:call-template name="att">
                            <xsl:with-param name="name" select="concat('xmlns:', name())"/>
                            <xsl:with-param name="value" select="."/>
                        </xsl:call-template>
                    </xsl:for-each>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:call-template name="distinct">
                        <xsl:with-param name="crowd" select="(//*|//@*)/namespace::*[name()!='xml']"/>
                    </xsl:call-template>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:template>
    <!-- attribute -->
    <xsl:variable name="search-atts" select="document('')/*/xsl:template[@name='search-atts']/search"/>
    <xsl:template name="att" match="@*" mode="src">
        <xsl:param name="name" select="name()"/>
        <xsl:param name="value">
            <xsl:value-of select="."/>
        </xsl:param>
        <xsl:text>&#32;</xsl:text>
        <span class="att">
            <xsl:value-of select="$name"/>
        </span>
        <xsl:text>="</xsl:text>
        <span class="val">
            <!-- correct entities -->
            <xsl:call-template name="replaces">
                <xsl:with-param name="string" select="$value"/>
                <xsl:with-param name="searches" select="$search-atts"/>
            </xsl:call-template>
        </span>
        <xsl:text>"</xsl:text>
    </xsl:template>
    <!-- find/replace to search in attributes value -->
    <xsl:template name="search-atts">
        <!-- entities -->
        <search>
            <find>&amp;</find>
            <replace>&amp;amp;</replace>
        </search>
        <search>
            <find>&quot;</find>
            <replace>&amp;quot;</replace>
        </search>
        <search>
            <find>&gt;</find>
            <replace>&amp;gt;</replace>
        </search>
        <search>
            <find>&lt;</find>
            <replace>&amp;lt;</replace>
        </search>
    </xsl:template>
    <!--  text -->
    <xsl:template match="text()" mode="src"><!-- only non empty text nodes are matched --></xsl:template>
    <xsl:variable name="search-text" select="document('')/*/xsl:template[@name='search-text']/search"/>
    <xsl:template match="*/text()[normalize-space(.)!='']" mode="src" priority="1">
        <pre class="text">
            <xsl:call-template name="replaces">
                <xsl:with-param name="string" select="."/>
                <xsl:with-param name="searches" select="$search-text"/>
            </xsl:call-template>
        </pre>
    </xsl:template>
    <!-- find/replace to search -->
    <xsl:template name="search-text">
        <!-- could be used to preserve text formatting if is not in a <pre/> tag
        <search>
            <find>&#32;</find>
            <replace>&#160;</replace>
        </search>
        <search>
            <find>&#xA;</find>
            <replace>
                <br/>
            </replace>
        </search>
        -->
        <!-- entities -->
        <search>
            <find>&amp;</find>
            <replace>&amp;amp;</replace>
        </search>
        <search>
            <find>&gt;</find>
            <replace>&amp;gt;</replace>
        </search>
        <search>
            <find>&lt;</find>
            <replace>&amp;lt;</replace>
        </search>
    </xsl:template>
    <!-- comment -->
    <xsl:template match="comment()" mode="src">
        <div>
            <span class="toggle" onclick="if(window.toggle) toggle(this); return false;">
                <xsl:text>&lt;!--</xsl:text>
            </span>
            <pre class="comment">
                <xsl:value-of select="."/>
            </pre>
            <xsl:text>--></xsl:text>
        </div>
    </xsl:template>
    <!-- element empty -->
    <xsl:template match="*" mode="src">
        <div>
            <span class="el">
                <xsl:text>&lt;</xsl:text>
                <span class="tag">
                    <xsl:value-of select="name(.)"/>
                </span>
                <xsl:call-template name="xmlns"/>
                <xsl:apply-templates select="@*" mode="src"/>
                <xsl:text>/></xsl:text>
            </span>
        </div>
    </xsl:template>
    <!-- element with text -->
    <xsl:template match="*[text()]" mode="src">
        <div>
            <span class="el-toggle" onclick="if(window.toggle) toggle(this);">
                <xsl:text>&lt;</xsl:text>
                <span class="tag">
                    <xsl:value-of select="name(.)"/>
                </span>
                <xsl:call-template name="xmlns"/>
                <xsl:apply-templates select="@*" mode="src"/>
                <xsl:text>></xsl:text>
            </span>
            <span>
                <xsl:apply-templates mode="src"/>
                <span class="el">
                    <xsl:text>&lt;/</xsl:text>
                    <span class="tag">
                        <xsl:value-of select="name(.)"/>
                    </span>
                    <xsl:text>></xsl:text>
                </span>
            </span>
        </div>
    </xsl:template>
    <!-- element with children -->
    <xsl:template match="*[*]" mode="src" priority="2">
        <div>
            <span class="el-toggle" onclick="if(window.toggle) toggle(this);">
                <xsl:text>&lt;</xsl:text>
                <span class="tag">
                    <xsl:value-of select="name(.)"/>
                </span>
                <xsl:call-template name="xmlns"/>
                <xsl:apply-templates select="@*" mode="src"/>
                <xsl:text>></xsl:text>
            </span>
            <div>
                <div class="block">
                    <xsl:apply-templates mode="src"/>
                </div>
                <span class="el">
                    <xsl:text>&lt;/</xsl:text>
                    <span class="tag">
                        <xsl:value-of select="name(.)"/>
                    </span>
                    <xsl:text>></xsl:text>
                </span>
            </div>
        </div>
    </xsl:template>
    <!--
     find/replace on a set of nodes
     thanks to [EMAIL PROTECTED]
     http://www.biglist.com/lists/xsl-list/archives/200110/msg01229.html 
     fixed and adapted by [EMAIL PROTECTED] -->
    <xsl:template name="replaces">
        <xsl:param name="string"/>
        <xsl:param name="searches" select="no-node"/>
        <xsl:variable name="first" select="$searches[1]"/>
        <xsl:variable name="rest" select="$searches[position() > 1]"/>
        <xsl:choose>
            <xsl:when test="$first and contains($string, $first/find)">
                <!-- replace with rest in before -->
                <xsl:call-template name="replaces">
                    <xsl:with-param name="string" select="substring-before($string, $first/find)"/>
                    <xsl:with-param name="searches" select="$rest"/>
                </xsl:call-template>
                <!-- copy-of current replace -->
                <xsl:copy-of select="$first/replace/node()"/>
                <!-- replace with all in after -->
                <xsl:call-template name="replaces">
                    <xsl:with-param name="string" select="substring-after($string, $first/find)"/>
                    <xsl:with-param name="searches" select="$searches"/>
                </xsl:call-template>
            </xsl:when>
            <!-- empty the searches -->
            <xsl:when test="$rest">
                <xsl:call-template name="replaces">
                    <xsl:with-param name="string" select="$string"/>
                    <xsl:with-param name="searches" select="$rest"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$string"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!--  thanks to [EMAIL PROTECTED] -->
    <xsl:template name="distinct">
        <xsl:param name="crowd"/>
        <xsl:param name="ones" select="crowd[1]"/>
        <xsl:choose>
            <xsl:when test="$crowd">
                <xsl:call-template name="distinct">
                    <xsl:with-param name="ones" select="$ones | $crowd[1][not(. = $ones )]"/>
                    <xsl:with-param name="crowd" select="$crowd[position() &gt; 1]"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:for-each select="$ones">
                    <xsl:call-template name="att">
                        <xsl:with-param name="name" select="concat('xmlns:', name())"/>
                        <xsl:with-param name="value" select="."/>
                    </xsl:call-template>
                </xsl:for-each>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]

Reply via email to