简体   繁体   中英

PHP: DOMDocument - attributes with a colon in it?

I'm using DOMDocument to parse an XML (SVG).

Some nodes have attributes with a colon in it, like:

<svg 
   id="svg2"
   width="1000"
   height="1000"
   sodipodi:version="0.32"
   inkscape:version="0.48.1 "
   ...
>

But when I do:

$node= DOMDocument->documentElement;
foreach($node->childNodes as $key=>$childnode) {
  foreach($childnode->attributes as $attribute) {
    echo $attribute->name."\n";
  }
}

the attributes with a: are printed without the first part (namespace)

How do I get the namespace for that attribute when iterating through the attributes like this?

You will need to work with namespaces (which is what the colon indicates) explicitly when you use DOMDocument .

Take a look at this method: http://www.php.net/manual/en/domelement.getattributenodens.php

Answer from OP's comment, nodeName from DOMNode .

$node= DOMDocument->documentElement;
foreach($node->childNodes as $key=>$childnode) {
  foreach($childnode->attributes as $attribute) {
    echo $attribute->nodeName."\n";
  }
}

Original Answer:

How about prefix from DOMNode ?

$node= DOMDocument->documentElement;
foreach($node->childNodes as $key=>$childnode) {
  foreach($childnode->attributes as $attribute) {
    echo $attribute->prefix.":".$attribute->name."\n";
  }
}

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