simpelXMLElement 属性命名空间

simpelXMLElement attribute namespaces

我已经研究这个问题几天了,需要一些帮助。 我想访问 'inhoud' 元素的命名空间属性。 在这种情况下,例如,我想要来自 contentType 属性的属性值。所以我想获取 'text/plain' 值。

<inhoud p10:contentType="text/plain" p6:bestandsnaam="hallo 2.txt" xmlns:p10="http://www.w3.org/2005/05/xmlmime">aGFsbG8gZGFhciB4DQoNCg0K</inhoud>

它以 p10 命名空间为前缀。

XML下方:

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <updateZaakdocument_Di02 xmlns="http://www.egem.nl/StUF/sector/zkn/0310">
            <edcLk02 p6:entiteittype="EDC" p6:functie="update" xmlns:p6="http://www.egem.nl/StUF/StUF0301">
                <parameters>
                    <p6:mutatiesoort>W</p6:mutatiesoort>
                </parameters>
                <object p6:entiteittype="EDC" p6:sleutelVerzendend="934087" p6:verwerkingssoort="W">
                    <inhoud p10:contentType="text/plain" p6:bestandsnaam="hallo 2.txt" xmlns:p10="http://www.w3.org/2005/05/xmlmime">aGFsbG8gZGFhciB4DQoNCg0K</inhoud>
                </object>
            </edcLk02>
        </updateZaakdocument_Di02>
    </s:Body>
</s:Envelope>

我试过这个:

<?php

    $sxe = new SimpleXMLElement($xml);

    $namespaces = $sxe->getNamespaces(true);

    $body = $sxe->xpath('//s:Body')[0];

    $inhoud = $body->updateZaakdocument_Di02->edcLk02->object->inhoud->children($namespaces["p10"]);
    print_r($inhoud);

结果是:

SimpleXMLElement Object ( [@attributes] => Array ( [contentType] => text/plain ) )

我从那里开始尝试:

echo (string) $inhoud >attributes($namespaces["p10"], true)->contentType;

但永远不要从中获得价值。 警告:节点不再存在于上面的行中

有人可以在这里指出正确的解决方案吗? 提前致谢 (-:

我认为当您获取 $inhoud 值时,您是在 p10 命名空间中获取子项。相反,您需要做的是获取 p10 命名空间中的属性...

$inhoud = $body->updateZaakdocument_Di02->edcLk02->object->inhoud;
print_r($inhoud);

echo "contentType=".(string)$inhoud->attributes($namespaces["p10"])->contentType;

这输出...

SimpleXMLElement Object
(
    [0] => aGFsbG8gZGFhciB4DQoNCg0K
)
contentType=text/plain

另外,在使用命名空间的各种方法时,第二个参数是标记您使用的前缀。当使用 $namespaces["p10"] 时,这是 URI,因此您应该省略第二个参数。