You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

注册命名空间后无法通过XPath获取XML节点值的求助

命名空间注册后PHP XPath查询XML节点无结果问题

尝试使用XPath查询获取XML节点值,已为XML注册所需命名空间,但无法得到结果;使用在线XPath工具执行相同查询可获取正确值,移除命名空间注册代码则会触发未定义命名空间错误。

PHP代码

$digestValueXPath = "//Invoice/ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent/sig:UBLDocumentSignatures/sac:SignatureInformation/ds:Signature/ds:Object/xades:QualifyingProperties/xades:SignedProperties/xades:SignedSignatureProperties/xades:SigningCertificate/xades:Cert/xades:CertDigest/ds:DigestValue";

$xml = new DOMDocument(encoding: 'utf-8');
$xml->loadXML(str_replace("\r", "", $invoiceXML));
$xpath = new DOMXPath($xml);

$xpath->registerNamespace('sig', "urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2");
$xpath->registerNamespace('sac', "urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2");
$xpath->registerNamespace('sbc', "urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2");
$xpath->registerNamespace('ds', "http://www.w3.org/2000/09/xmldsig#");
$xpath->registerNamespace('xades', "http://uri.etsi.org/01903/v1.3.2#");

$digestValue = $xpath->query($digestValueXPath);
dd($digestValue);
$digestValue->item(0)->nodeValue = $encodedHashedCert;

对应XML文件

<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
    <ext:UBLExtensions>
        <ext:UBLExtension>
            <ext:ExtensionURI>urn:oasis:names:specification:ubl:dsig:enveloped:xades</ext:ExtensionURI>
            <ext:ExtensionContent>
                <!-- 注:以下签名值仅为示例 -->
                <sig:UBLDocumentSignatures xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2" xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2">
                    <sac:SignatureInformation>
                        <cbc:ID>urn:oasis:names:specification:ubl:signature:1</cbc:ID>
                        <sbc:ReferencedSignatureID>urn:oasis:names:specification:ubl:signature:Invoice</sbc:ReferencedSignatureID>
                        <ds:Signature Id="signature" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
                            <ds:SignedInfo>
                                <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2006/12/xml-c14n11" />
                                <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256" />
                                <ds:Reference Id="invoiceSignedData" URI="">
                                    <ds:Transforms>
                                        <ds:Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116">
                                            <ds:XPath>not(//ancestor-or-self::ext:UBLExtensions)</ds:XPath>
                                        </ds:Transform>
                                        <ds:Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116">
                                            <ds:XPath>not(//ancestor-or-self::cac:Signature)</ds:XPath>
                                        </ds:Transform>
                                        <ds:Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116">
                                            <ds:XPath>not(//ancestor-or-self::cac:AdditionalDocumentReference[cbc:ID='QR'])</ds:XPath>
                                        </ds:Transform>
                                        <ds:Transform Algorithm="http://www.w3.org/2006/12/xml-c14n11" />
                                    </ds:Transforms>
                                    <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
                                    <ds:DigestValue>p4VtKWX9+VGndJMdv/7WNu1V4=</ds:DigestValue>
                                </ds:Reference>
                                <ds:Reference Type="http://www.w3.org/2000/09/xmldsig#SignatureProperties" URI="#xadesSignedProperties">
                                    <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
                                    <ds:DigestValue>NzY2MGJkMjc0YTU0OTgzZjM2MzNmIzZjNhNWRiZjU4MTc1OTYyN2IxYg==</ds:DigestValue>
                                </ds:Reference>
                            </ds:SignedInfo>
                            <ds:SignatureValue>MEQCIHBXIaofiuU/2KZxLldH6oETcvT4IhyBJszCxTeaNcAAAiBpRELXsTiiBqFxq/dXfa6hq+7RXC2Le9sK87S8EhiwKw==</ds:SignatureValue>
                            <ds:KeyInfo>
                                <ds:X509Data>
                                    <ds:X509Certificate>MIIBsTCCAVYCFCfs84hVGkzmV5naL3A/HWfk3CZ6MAoGCCqGSM49BAMCMFwxDzANBgNVBAMMBkFMQU1BTDELMAkGA1UEBhMCUVQQO9N6MPbMdBcRECB5VctM9uQs6/AiEAqzL4INGoyjdcAg5gNhHnSRyLbnocwUpAx69obSopmuE=</ds:X509Certificate>
                                </ds:X509Data>
                            </ds:KeyInfo>
                            <ds:Object>
                                <xades:QualifyingProperties Target="signature" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#">
                                    <xades:SignedProperties Id="xadesSignedProperties">
                                        <xades:SignedSignatureProperties>
                                            <xades:SigningTime>2023-07-10T10:19:34Z</xades:SigningTime>
                                            <xades:SigningCertificate>
                                                <xades:Cert>
                                                    <xades:CertDigest>
                                                        <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
                                                        <ds:DigestValue>NDg2OTk1NjVlOWNlMmQwMTQ4YmNhYTcMDBmNWQ1NTNiMTg3YThjMDg1N2ZiODYyZQ==</ds:DigestValue>
                                                    </xades:CertDigest>
                                                    <xades:IssuerSerial>
                                                        <ds:X509IssuerName>L</ds:X509IssuerName>
                                                        <ds:X509SerialNumber>22793452360720868986</ds:X509SerialNumber>
                                                    </xades:IssuerSerial>
                                                </xades:Cert>
                                            </xades:SigningCertificate>
                                        </xades:SignedSignatureProperties>
                                    </xades:SignedProperties>
                                </xades:QualifyingProperties>
                            </ds:Object>
                        </ds:Signature>
                    </sac:SignatureInformation>
                </sig:UBLDocumentSignatures>
            </ext:ExtensionContent>
        </ext:UBLExtension>
    </ext:UBLExtensions>
</Invoice>

更新说明

改用以下XPath表达式可正常工作:

private $digestValueXPath = "/*/*[name()='ext:UBLExtensions']/*[name()='ext:UBLExtension']/*[name()='ext:ExtensionContent']/*[name()='sig:UBLDocumentSignatures']/*[name()='sac:SignatureInformation']/*[name()='ds:Signature']/*[name()='ds:Object']/*[name()='xades:QualifyingProperties']/*[name()='xades:SignedProperties']/*[name()='xades:SignedSignatureProperties']/*[name()='xades:SigningCertificate']/*[name()='xades:Cert']/*[name()='xades:CertDigest']/*[name()='ds:DigestValue']";

内容的提问来源于stack exchange,提问作者mohammed awni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 05:45:53