3 Replies Latest reply on Dec 4, 2008 11:14 AM by aharv1

    Selecting XML Children with Namespaces

      I am trying to select child nodes of an XML element that have a different namespace from the parent, but xml.child(name) returns an empty list.

      In the attached code, londonList contains a single XML element and parisList is empty. I can't figure out how to find the paris child, and have tried adding the prefix to findParis (so it is "ff:b") as well as turning findParis into an XML object with the name "b" and adding the ff namespace to it, then passing that to child. Neither of those have worked.

      Any help is appreciated.
        • 1. Re: Selecting XML Children with Namespaces
          -Hob Level 1
          You need to use a namespace object and an e4x expression. Your code will look something like this:

          var ns:Namespace = new Namespace(" http://fakenamespace");
          var parisList:XMLList = testXML.ns::b;

          note the :: between the ns & the b. ns b is the value that you're trying to match and putting the :: before it allows you to specify its namespace.
          • 2. Re: Selecting XML Children with Namespaces
            aharv1 Level 1
            Unfortunately in my real scenario I don't know what the element will be called. It could be in <foo> or <bar> etc, so I need to be able to use a variable (in which the element name is set beforehand). With "::" I can't use a variable, so textXML.ns::findParis looks for a "findParis" element and not "b".

            That's why I'm trying to use testXML.child(findParis) because I can use a variable. I just can't figure out how to make it incorporate the namespace as well.
            • 3. Re: Selecting XML Children with Namespaces
              aharv1 Level 1
              I figured out the solution. the child() method uses the default namespace to search for the element name. Since I couldn't figure out how to specify a different namespace in the method call, just set the default namespace before you call child to the namespace of the element.