[Home] [By Thread] [By Date] [Recent Entries]

  • From: "Martin v. Loewis" <martin@l...>
  • To: gtn@e...
  • Date: Thu, 29 Mar 2001 10:21:57 +0200

> Given that the DOM allows adjacent Text nodes unless
> normalise() is called, and CDATASection nodes extend 
> the interfaces of Text, how would your processing
> change if you just dealt with Text alone?

After I get the tree, I call normalize() on it. Suppose my processing
is to count people whose first name is "Karl", in a document like

<people><person><first>Karl</first><last>Käfer</last></person></people>

Then I'd do

  karls = 0
  root.normalize()
  for person in root.getElementsByTagName("person"):
      first = person.getElementsByTagName("first")[0]
      for n in first.childNodes:
          if n.nodeType == Node.TEXT_NODE and n.data == "Karl":
              break
      else:
          continue
      karl += 1

Now, if there are CDATA sections in there, this algorithm will break:
the string Karl could still be split across several nodes.

Regards,
Martin

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member