E4X: capture nodes with namespaces?

I want to learn how to handle XML with namespaces in E4X, so basically this is what I want to learn, let's say I have this XML:

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/"> <rdf:item value="5"/> <item value="10"/> </rdf:RDF> 

How can I assign <rdf:item/> to the var variable rdfItems and <item/> to the var variable called regItems ?

Thanks!

+4
source share
3 answers

I'm not sure if this is the answer to the question correctly, but based on your scenario, the following code retrieves both values ​​(given the xml variable below is an XML object containing the piece of XML code that you specified)

 // Your "rdf" namespace namespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; use namespace rdf; // Your "reg" (ie, default) namespace namespace reg = "http://purl.org/rss/1.0/"; use namespace reg; private function getYourValues():void { var rdfitems:String = xml.rdf:: item.@value ; var regitems:String = xml.reg:: item.@value ; } 

A distinction must be made between the "rdf" and the "non-rdf" elements, since their element names are otherwise identical, so a second namespace is declared, allowing you to select each element independently. Hope this helps!

+4
source

If you have XML that contains multiple names, but you don't care about namespaces when retrieving values ​​from XML, you can do the following ....

XML example

 <ns1:Item> <ns1:ItemType>Printed Material</ns1:ItemType> <ns2:Book isbn="123456"> <ns2:Author> <ns2:FirstName>James</ns2:FirstName> <ns2:LastName>Smith</ns2:LastName> </ns2:Author> <ns2:Title>The Book Title</ns2:Title> </ns2:Book> <ns1:Item> 

You can get any element regardless of the namespace like this

 var itemType:String = xml.*::ItemType; var bookISBN:Number = xml.*:: Book.@isbn ; var bookTitle:String = xml.*::Book.Title; 
+7
source

If you don’t know the namespace you are dealing with, there are many methods you can use.

 node.namespace().prefix //returns prefix ie rdf node.namespace().uri //returns uri of prefix ie http://www.w3.org/1999/02/22-rdf-syntax-ns# node.inScopeNamespaces() //returns all inscope namespace as an associative array like above //returns all nodes in an xml doc that use the namespace var nsElement:Namespace = new Namespace(node.namespace().prefix, node.namespace().uri); var usageCount:XMLList = node..nsElement::*; 

your best bet is to just play with it. But I like that the previous boolean operator for xml filtering makes it easier to work with.

hope this gives you some ideas for dynamic namespace management

Yours faithfully,

John

+2
source

All Articles