使用 UBL/不同命名空间读取 PHP simplexml

标签 php xml namespaces simplexml

我有 this kind of xml文档。

我的问题是我无法通过 simplexml 读取标签,因为 namespace 不同。示例 BuyerCustomerParty->Party->Person->FamilyName标签。 BuyerCustomerParty , PartyPerson在下 cac -命名空间,但 FamilyName在下 cbc -命名空间。奇怪的是我可以写入标签并替换内容,但在此之前无法读取。

这里还有一些代码:

$sxe = simplexml_load_string($value);
$namespaces = $sxe->getDocNamespaces();
$sxe->registerXPathNamespace('cbc', $namespaces['cbc']);
$cbc = $sxe->children($namespaces['cbc']); 

//THIS PRINTS THE RIGHT VALUE            
$cbc->IssueDate;

$sxe->registerXPathNamespace('cac', $namespaces['cac']);
$cac = $sxe->children($namespaces['cac']); 

//BUT THIS PRINTS NOTHING
echo $fg = $cac->BuyerCustomerParty->Party->Person->FamilyName;

//BUT IF I CHANGE THE VALUE OF THE TAG... I CAN ACCESS THE TAG
$cac->BuyerCustomerParty->Party->Person->FamilyName = "value";

如何读取标签?

最佳答案

我的解决方案是将 cbc child 包括在内。

echo $fg = $cac->BuyerCustomerParty->Party->Person->children('cbc',TRUE)->FamilyName;

关于使用 UBL/不同命名空间读取 PHP simplexml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21765994/

相关文章:

php - Youtube Oauth 2.0 API视频上传失败

php - 无法使用PHPunit Selenium执行javascript

javascript - Laravel Gulp 未创建 js 文件夹

php - 这里需要一些 php/mysql 的掌握 : does wordpress post exist before import

android - TextView 在屏幕上显示很小

python - 雅虎体育 API XML 命名空间找不到元素

xml - 来自多个 XML 的 XSD 生成器

xml - 从 XSL 1.0 中的字母数字字符中删除前导零?

c++ - typedefed 类型对派生类总是可见的吗?

.net - 删除根节点下的所有命名空间