简体   繁体   中英

How to know if an attribute or node has a different namespace?

How do I know when I encounter a node with a namespace and when I do is it possible to extract that name and URL of that namespace?

XML:

<s:Image xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:test="library://ns.test.com/flex/"

         visible="false" 
         test:locked="true" />

XML parsing code:

public static function getAttributeNames(node:XML):Array {
    var result:Array = [];
    var attributeName:String;

    for each (var attribute:XML in node.attributes()) {
        attributeName = attribute.name().toString();

        result.push(attributeName);
    }
    return result;
}

trace (result);

[0] visible
[1] library://ns.test.com/flex/::locked

I could do a check for "::" in the string but that seems cumbersome. There must be a better way.

Using information from a different but related post I was able to check the attribute.namespace() object. Normally it's nothing! But when it has a namespace prefix it will return an object.

public static function getAttributeNames(node:XML):Array {
    var result:Array = [];
    var attributeName:String;

    for each (var attribute:XML in node.attributes()) {
        attributeName = attribute.name().toString();

        var attNamespace:Object = attribute.namespace();
        var a:Object = attribute.namespace().prefix     //returns prefix i.e. rdf
        var b:Object = attribute.namespace().uri        //returns uri of prefix i.e. http://www.w3.org/1999/02/22-rdf-syntax-ns#

        var c:Object = attribute.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(attribute.namespace().prefix, attribute.namespace().uri);

        var usageCount:XMLList = attribute..nsElement::*;
        result.push(attributeName);
    }

    return result;
}

Information garnered from here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM