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



我有这种xml文档。

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

这里还有一些代码:

$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儿童包括在内。

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

最新更新