February 24, 2002 - Adding Entity References on the Fly | WebReference

February 24, 2002 - Adding Entity References on the Fly

Yehuda Shiran February 24, 2002
Adding Entity References on the Fly
Tips: February 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

You can create entity references on the fly. Remember our date element from mydvd7.xml:

  <date>Jan 30, 2002</date>
Let's add an entity reference for the day of the week, before the date. We have already prepared the substitution in the DTD file:

  <!ENTITY day "Wednesday, ">
First, let's create the entity reference node:

  var newEntityReference = xmlDoc.createEntityReference("day");
Find the first and only date element:

  objNodeList = xmlDoc.getElementsByTagName("date");
Now, insert the new node as a child of the date element, before its first child, the text: Jan 30, 2002:

  objNodeList(0).insertBefore(newEntityReference, objNodeList(0).childNodes(0));
Let's verify now that the date element has two children: "Wednesday, " and "Jan 30, 2002". The following lines print these two nodes, as well as the child of the entity reference:

  alert(objNodeList(0).childNodes(0).text);
  alert(objNodeList(0).childNodes(1).text);
  alert(objNodeList(0).childNodes(0).childNodes(0).text);
Running this script will yield the two expected results. But notice that the entity reference has its own child which is also "Wednesday, ".