简体   繁体   English

PHP:DOMDocument - 带有冒号的属性?

[英]PHP: DOMDocument - attributes with a colon in it?

I'm using DOMDocument to parse an XML (SVG).我正在使用 DOMDocument 解析 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)带有 a: 的属性在没有第一部分(命名空间)的情况下打印

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 .当您使用DOMDocument时,您将需要明确使用名称空间(冒号表示的内容)。

Take a look at this method: http://www.php.net/manual/en/domelement.getattributenodens.php看看这个方法: http://www.php.net/manual/en/domelement.getattributenodens.php

Answer from OP's comment, nodeName from DOMNode .来自 OP 的评论的回答,来自DOMNodenodeName

$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 ?来自DOMNodeprefix怎么样?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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