moock.org is supported in part by


July 02, 2008

convert xml tags to lowercase

Here's a handy little function for converting all tag names and attribute names in an XML object to lower case. Useful for doing case-insensitive matching on tag names and attribute names.

public function xmlTagsToLowerCase (xml:XML):XML {
  // Convert the root tag to lowercase
  xml.setName(xml.name().toString().toLowerCase());
  for each (var attribute:XML in xml.@*) {
    attribute.setName(attribute.name().toString().toLowerCase());
  }
  // Convert all descendant tags to lowercase
  for each (var child:XML in xml..*) {
    // If the node is an element...
    if (child.nodeKind() == "element") {
      // ...change its name to uppercase.
      child.setName(child.name().toString().toLowerCase());
      // If the node has any attributes, change their names to uppercase.
      for each (attribute in child.@*) {
        attribute.setName(attribute.name().toString().toLowerCase());
      }
    }
  }
  return xml;
}
// Usage:
var x:XML = xmlTagsToLowerCase(new XML ("<A C='D'><B>test</B></A>"));
trace(x); // Output: <a c="D"><b>test</b></a>
Posted by moock at July 2, 2008 09:49 PM