Raffaello
Raffaello

Reputation: 1706

Get prefix of XML namespace from URI

How can I get the prefix of a xml namespace from URI?

example:

<Name xmlns:tiger="http://www.census.gov">
</Name>

I've got the

"http://www.census.gov"

I want to get the prefix

tiger

How can I do this in Actionscript / Flex?

thx

EDIT

The answer doesn't work with this complex example:

<Name xmlns:tiger="http://www.census.gov" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wfs" xmlns:wfs="http://www.opengis.net/wfs" xmlns:ows="http://www.opengis.net/ows" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:it.geosolutions="http://www.geo-solutions.it" xmlns:cite="http://www.opengeospatial.net/cite" xmlns:worldWS="worldWS" xmlns:sde="http://geoserver.sf.net" xmlns:topp="http://www.openplans.org/topp" xmlns:sf="http://www.openplans.org/spearfish" xmlns:nurc="http://www.nurc.nato.int" xmlns:solWS="solWS">
    tiger:poly_landmarks
</Name>

I've got empty Array.

ANSWER MY OWN Q

for EDIT example

var xml:XML = <Name xmlns:tiger="http://www.census.gov" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wfs" xmlns:wfs="http://www.opengis.net/wfs" xmlns:ows="http://www.opengis.net/ows" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:it.geosolutions="http://www.geo-solutions.it" xmlns:cite="http://www.opengeospatial.net/cite" xmlns:worldWS="worldWS" xmlns:sde="http://geoserver.sf.net" xmlns:topp="http://www.openplans.org/topp" xmlns:sf="http://www.openplans.org/spearfish" xmlns:nurc="http://www.nurc.nato.int" xmlns:solWS="solWS">
    tiger:poly_landmarks
</Name>

var ns:Namespace = xml.namespace("http://www.census.gov");
if(ns.uri == "http://www.census.gov")
....

Upvotes: 0

Views: 280

Answers (1)

fsbmain
fsbmain

Reputation: 5267

    var xml:XML = <Name xmlns:tiger="http://www.census.gov"></Name>;
    var ns:Array = xml.namespaceDeclarations();
    trace(ns[0].prefix); //output: tiger

UPD for complex xml (output tiger as well):

    var xml:XML = <Name xmlns:tiger="http://www.census.gov" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wfs" xmlns:wfs="http://www.opengis.net/wfs" xmlns:ows="http://www.opengis.net/ows" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:it.geosolutions="http://www.geo-solutions.it" xmlns:cite="http://www.opengeospatial.net/cite" xmlns:worldWS="worldWS" xmlns:sde="http://geoserver.sf.net" xmlns:topp="http://www.openplans.org/topp" xmlns:sf="http://www.openplans.org/spearfish" xmlns:nurc="http://www.nurc.nato.int" xmlns:solWS="solWS">
        tiger:poly_landmarks
    </Name>

    var nss:Array = xml.namespaceDeclarations();
    for each(var ns:Namespace in nss)
    {
        if(ns.uri == "http://www.census.gov")
        {
            trace(ns.prefix);
            break;
        }
    }

Upvotes: 2

Related Questions