HTML tag handling in XSL

Before I explain the problem I am facing, I probably need to explain the problem that I wanted to solve first. :)

I have XML to be formatted using XSL. This XML contains HTML tags in CDATA, for example: -

<doc>
    <![CDATA[
        <b>Hello!</b>
    ]]>
</doc>

When XSL performs the conversion, the browser displays <b>Hello!</b>, rather than highlights, the Hello! . I checked the source code and it looks like this: -

<doc>
    &lt;b&gt;Hello!&lt;/b&gt;
</doc>

To solve this problem, I am thinking of using jQuery to render text as HTML, for example:

$(document).ready(function(){
    var obj = $(".content");
    alert("text: " + obj.text()); // to test if JQuery works
    obj.html(obj.text());
});

So, I tried adding the JQuery library to XSL, and after several attempts and reading about the problem with the tag, <script>it seems that I need to add a fiction between the open and close tags <script>, for example this: -

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js">
    <xsl:comment/>
</script>

2 Firebug: -

b.style is undefined
(function(a,b){function ci(a){return d...a:a+"px")}}),a.jQuery=a.$=d})(window);

$ is not defined

... javascript , .

JQuery 1.5.2 1.3.2, , - . Firebug: -

K.style is undefined
(function(){var R=/((?:\((?:\([^()]+\)...,typeof K==="string"?K:K+"px")}})})();

JQuery 1.3.2. : obj.html(obj.text()); .

obj.html(obj.text()); obj.text("Just Testing: " + obj.text());, , ... , Just Testing: .

obj.html(obj.text()); ? : HTML- CData HTML, ?

.

UPDATE

... obj.html("aaa"); . , - XSL, JQuery . 1.3.2.

+3
3

futzing , , , , . JQuery, Firebug .

getElementsByClassName() getElementByClass GetElementById Javascript? . JS "xml.js": -

xml.js

function getElementsByClassName(node,classname) {
    if (node.getElementsByClassName) { // use native implementation if available
        return node.getElementsByClassName(classname);
    } 
    else {
        return (function getElementsByClass(searchClass,node) {
            if (node == null) {
                node = document;
            }
            var classElements = [],
            els = node.getElementsByTagName("*"),
            elsLen = els.length,
            pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;

            for (i = 0, j = 0; i < elsLen; i++) {
                if ( pattern.test(els[i].className) ) {
                    classElements[j] = els[i];
                    j++;
                }
            }
            return classElements;
        })(classname, node);
    }
}

window.onload=function() {
    var elements = getElementsByClassName(document, "html");

    for (var i = 0; i < elements.length; i++) {
         var e = elements[i];
         e.innerHTML = e.innerHTML.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
    }
};

XSL

JS XSL: -

<script type="text/javascript" src="xml.js"><xsl:comment/></script>

class="html" , HTML, : -

<p class="html">
    <xsl:call-template name="getDoc" />
</p>

XML

XML : -

<doc>     
  <![CDATA[         
     <b>Hello!</b> this is a <i>test</i>. 
      Email me at <a href="mailto:cow@dung.com">Moo</a>.
    ]]> 
</doc> 

HTML CDATA, , .

UPDATE

, CDATA < > : -

<doc>     
     &lt;b&gt;Hello!&lt;/b&gt; this is a &lt;i&gt;test&lt;/i&gt;. 
      Email me at &lt;a href="mailto:cow@dung.com"&gt;Moo&lt;/a&gt;.
</doc> 

: -

<xsl:value-of select="..." disable-output-escaping="yes" />

... : -

  • IE 8: , .
  • Chrome 9: ,
  • Safari 5: ,
  • Firefox 4: - .

, -... FF4... bah.

, javascript, .

+1

:

<doc>     
  <![CDATA[         
     <b>Hello!</b>     
    ]]> 
</doc> 

<doc>     
  <b>Hello!</b>     
 </doc> 

CDATA - 2 1.

, !

+3

Did you try to get text from obj first into a variable and then using a variable?

var objText = obj.text();
obj.html(objText);

Just a quick suggestion, just thought I'd ask.

0
source

All Articles