View Full Version : document.createTextNode with entities

05-01-2003, 01:00 AM
I don't know if this is specific to working in the XML DOM, or generally with this method, but when I do this


and append it to a node, it outputs the text "»" rather than the "" symbol. I've also tried » and » but they all just output the literal text


05-01-2003, 01:27 AM
Nope, it's not specific to the XML (Core) DOM - a TextNode can only contain pure text. If you want to create an entity, you have to use Document.createEntityReference which to my knowledge isn't implemented anywhere (or at least wasn't working even in moz last time I tried to use it).

It takes a DOMString with UTF-16 characters, so I guess you can use a JavaScript string with U+00bb to get your result. ("\u00bb")

05-01-2003, 01:33 AM
No it didn't work in moz - it returned a component failure.

But your other solution worked; nice idea :thumbsup:

05-01-2003, 01:58 AM
Only knew about that due to the fact I've tried to do what you wanted to do before... I doubt I'd found it from reading the specs.

05-02-2003, 07:58 AM
Entity references are necessary in the HTML or XML markup. For example, &lt; in a text file tells a parser that the '<' character is part of the data, not the start of a new tag.

But once a document is parsed into a DOM representation, you no longer need to distiguish between the markup and the data. Programming object represent nodes, rather than syntactic strings of text. The DOM doesn't need entity references because there's no mistaking between what's structure and what's data. That's why it gives you the literal text.

How you define characters within the DOM depends on the particular DOM API. liorean's suggestion works in JavaScript because that language uses the \u escape sequence in string literals to define arbitrary Unicode characters. I think C and Java use that too, but other languages might use other methods. It's all dependent on the language of the DOM implementation, not on the DOM or XML standards.

I hadn't heard of document.createEntityReference before but I suspect it's a convenience function that will maps an entity reference to a native character code for you. That would be a nice alternative to looking up the codes.

05-02-2003, 12:35 PM
Entities are a bit more than that. An entity in HTML is usually a single character - it needn't be. And entity can be a strings of longer content, composed of other entities and/or characters. They are defined by the DTD, though, and that's where the problem lies: our browsers are not validating XML parsers, even if expat and msxml have that ability - they don't parse the DTD, they merely checks if one they recognise exists, and use that one.
In other words, they don't "get" entities. They resolve the standard html set of them, but they don't let the user define new ones.

An entity reference is what it sounds like, a reference to an entity - the entity itself is what tells us what content it's to be replaced with. (Look on entities as the declaration of a variable - and entity references as the usage of that variable. It's the closest analogue that I can think of.)

05-02-2003, 12:48 PM
Originally posted by liorean
In other words, they don't "get" entities. They resolve the standard html set of them, but they don't let the user define new ones.

You almost speak truth. Mozilla is not a validating XML parser, very true. However, you can define entities to use within a DTD. It also understands MathML entities given the correct doctype.

Lame example I did for an entirely other reason (but is chockful of my &mathml; entity for shortcut namespacing, and of course the MathML entities):

05-02-2003, 01:12 PM
Hmm, I doubt you are right about that - in the same way moz has a knowledge of the XHTML namespace, it has a knowledge of the MathML namespace. The knowledge of the namespaces means they have either the rules of that DTD hardcoded, or they has a copy of it that they parse when encountering that DTD declaration. This means they also have a knowledge of the entities used in that namespace.

Try doing the same, but use a custom namespace that moz doesn't have any knowledge of - you'd find that moz doesn't handle them. (moz might not parse custom DTDs, but it does parse the <!ENTITY> entries in the DTD declaration)

(I've not done any testing on this - so you may be right...)

05-04-2003, 07:43 PM
I never said Mozilla understood DTD. I said given the correct DTD, it knows MathML entities. You are correct, it is hardcoded, but you were debating something I had never said. :)

I was just saying that it knew about defined entities inside an inline doctype basically.

05-04-2003, 08:26 PM
Oh, I just misinterpreted this, then:
Originally posted by jkd
However, you can define entities to use within a DTD. You meant Document Type Declaration (the SGML construct that references the definition, whether embedded or included), not as I thought Document Type Definiton (the actual definition, most often included through a .dtd file).

W3C warns for the problems with the abbreviation DTD and states that you should use the wording "DTD declaration" or "doctype declaration" if you speak of the SGML construct and not the actual definition.

02-25-2006, 08:24 PM
I know this is nearly 3 years late, but you helped me figure out this solution: http://www.codingforums.com/showthread.php?t=80593