开发者

CDATA not rendering tags

开发者 https://www.devze.com 2023-04-05 05:18 出处:网络
I\'m trying to display XML tags mixed in with plain text on a web page.I do this from a python script that obtains it\'s data from a database.I\'ve simplified my problem to the program below.

I'm trying to display XML tags mixed in with plain text on a web page. I do this from a python script that obtains it's data from a database. I've simplified my problem to the program below.

#!/usr/local/bin/python
print """Content-type: text/html;charset=utf-8\n\n"""
print """<html><body>
start:<![CDATA[This is the <xml> tag </xml>.]]>:end
</body></html>"""

I'm expecting it to display the following:

start:This is the <xml> tag </xml>.:end

In both IE8 and Chrome15 it however displays the following:

start: tag .]]>:end

When I look at the HTML source of the page in IE, I can see the following:

<html><body>
start:<![CDATA[This is the <xml> tagxml.]]>:end
</body></html>

In Chrome I see the the same when looking at the source, but it seems that the <![CDATA[This is the <xml> part is 开发者_运维知识库in green because it is considered a comment.

I particularly want to keep the text (instead of converting the < to &lt;) because via javascript I access the elements, allowing people to edit them in a separate textarea. Converting them would then save them converted, resulting in problems further down in processing. I could convert them back before saving, but this seems like the wrong approach.

Any idea what I'm doing wrong?

Thanks in advance, Grant


CDATA is part of XML, not HTML, so the browser ignores it, and then treats any tags in it as it would any other tags - ignoring ones it doesn't recognise, and paying attention to those it does.

I think there's no alternative but to use &lt; etc and convert to tags when editing and convert back when saving.


<!DOCTYPE html>
<html>
    <body>
        <div id="div1">&lt;b&gt;hi&lt;/b&gt;</div>
        <textarea id="area"></textarea>
        <script type="text/javascript">
            var div1 = document.getElementById('div1')
            var area = document.getElementById('area')
            var text = div1.firstChild.nodeValue
            area.value = text
        </script>
    </body>
</html>

Where the problem is?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号