![]() |
![]() |
||||||
|
|||||||
| Tags: |
![]() |
|
|||
|
I am trying to select child nodes of an XML element that have a different
namespace from the parent, but xml.child(name) always returns null. 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. var testXML:XML = <root xmlns:ff="http://fakenamespace"><a>London</a><ff:b >Paris</ff:b></root>; var findLondon:String = "a"; var findParis:String ="b"; var londonList:XMLList = testXML.child(findLondon); var parisList:XMLList = testXML.child(findParis); |
| Sponsored Links |
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
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. var testXML:XML = <gg:root xmlns:gg="http://ggnamespace" xmlns:ff="http://fakenamespace"> <gg:a>London</gg:a> <ff:b>Paris</ff:b> </gg:root>; var findLondon:String = "a" var findParis:String ="b"; var ff:Namespace = new Namespace("http://fakenamespace"); var gg:Namespace = new Namespace("http://ggnamespace"); default xml namespace = gg; var londonList:XMLList = testXML.child(findLondon); default xml namespace = ff; var parisList:XMLList = testXML.child(findParis); |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
- Contact Us
-|-
Adobe Dreamweaver Forums -|-
Archive -|-
Top -|-Rules/Disclaimer-|-Help/Support-|-Advertise