用于在标签名称中使用冒号解析 XML 的 PHP 库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1575788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:08:28  来源:igfitidea点击:

PHP library for parsing XML with a colons in tag names?

phpxmlnamespacessimplexml

提问by mpen

I've been trying to use SimpleXML, but it doesn't seem to like XML that looks like this:

我一直在尝试使用SimpleXML,但它似乎不喜欢看起来像这样的 XML:

<xhtml:div>sample <xhtml:em>italic</xhtml:em> text</xhtml:div>

So what library will handle tags that look like that (have a colon in them)?

那么哪个库会处理看起来像这样的标签(其中有一个冒号)?

回答by Nathan

Say you have some xml like this.

假设您有一些这样的 xml。

<xhtml:div>
  <xhtml:em>italic</xhtml:em>
  <date>2010-02-01 06:00</date>
</xhtml:div>

You can access 'em' like this: $xml->children('xhtml', true)->div->em;

您可以像这样访问“他们”: $xml->children('xhtml', true)->div->em;

however, if you want the date field, this: $xml->children('xhtml', true)->div->date;wont work, because you are stuck in the xhtml namespace.

但是,如果你想要日期字段,这个:$xml->children('xhtml', true)->div->date;不会工作,因为你被困在 xhtml 命名空间中。

you must execute 'children' again to get back to the default namespace:

您必须再次执行 'children' 以返回默认命名空间:

$xml->children('xhtml', true)->div->children()->date;

回答by EarnestoDev

If you want to fix it quickly do this (I do when I feel lazy):

如果您想快速修复它,请执行以下操作(当我感到懒惰时我会这样做)

// Will replace : in tags and attributes names with _ allowing easy access
$xml = preg_replace('~(</?|\s)([a-z0-9_]+):~is', '_', $xml);

This will convert <xhtml:to <xhtml_and </xhtml:to </xhtml_. Kind of hacky and can fail if CDATA NameSpaced XML container blocks are involved or UNICODE tag names but I'd say you are usually safe using it (hasn't failed me yet).

这将转换<xhtml:<xhtml_</xhtml:</xhtml_。如果涉及 CDATA NameSpaced XML 容器块或 UNICODE 标记名称,则可能会失败,但我会说您通常可以安全地使用它(还没有让我失望)

回答by Ollie Saunders

Colon denotes an XML namespace. The DOMhas good support for namespaces.

冒号表示 XML 命名空间。该DOM有命名空间很好的支持。

回答by Patryk K

I don't think it's a good idea to get rid of the colon or to replace it with something else as some people suggested. You can easily access elements that have a namespace prefix. You can either pass the URL that identifies the namespace as an argument to the children() method or pass the namespace prefix and "true" to the children() method. The second approach requires PHP 5.2 and up.

我认为摆脱冒号或像某些人建议的那样用其他东西代替它不是一个好主意。您可以轻松访问具有命名空间前缀的元素。您可以将标识命名空间的 URL 作为参数传递给 children() 方法,也可以将命名空间前缀和“true”传递给 children() 方法。第二种方法需要 PHP 5.2 及更高版本。

SimpleXMLElement::children

SimpleXMLElement::children