我试图使用XPath查询获取XML node 值,但没有得到任何结果.我为该XML注册了名称空间,但是没有办法获得结果,尽管当我在相同的查询中使用http://xpather.com/时,我得到了正确的值. 注意:当我删除注册名称空间行时,我收到未定义的名称空间错误.

$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#");
    // dd($digestValueXPath);
    // $canonicalizationInvoiceXML = $xml->C14N();
    // $digestValue = $xml->getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "ds:DigestValue");
    $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>
            <!-- Please note that the signature values are sample values only -->
            <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 算法rithm="http://www.w3.org/2006/12/xml-c14n11" />
                            <ds:SignatureMethod 算法rithm="http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256" />
                            <ds:Reference Id="invoiceSignedData" URI="">
                                <ds:Transforms>
                                    <ds:Transform 算法rithm="http://www.w3.org/TR/1999/REC-xpath-19991116">
                                        <ds:XPath>not(//ancestor-or-self::ext:UBLExtensions)</ds:XPath>
                                    </ds:Transform>
                                    <ds:Transform 算法rithm="http://www.w3.org/TR/1999/REC-xpath-19991116">
                                        <ds:XPath>not(//ancestor-or-self::cac:Signature)</ds:XPath>
                                    </ds:Transform>
                                    <ds:Transform 算法rithm="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 算法rithm="http://www.w3.org/2006/12/xml-c14n11" />
                                </ds:Transforms>
                                <ds:DigestMethod 算法rithm="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 算法rithm="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 算法rithm="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>

最新情况: 当使用此表达式时,它是有效的

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']";

推荐答案

您的发票也在命名空间中:xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"

只需添加一些前缀,如:

$xpath->registerNamespace('default-ns', "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2");

然后将您的XPath更改为:

$digestValueXPath = "//default-ns: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";

Php相关问答推荐

未找到laravel cascadeOnEdit

无法添加ProxyClass__setInitialized()上的忽略

PHP邮件表单无法识别重音/特殊字符

从WC会话变量更改WooCommerce checkout 托运方法成本

在Laravel Eloquent中实现一对多关系

PHP MySQL求和子树并与父树累加

从字符串转换日期和/或时间时,Laravel转换失败

根据WooCommerce中的自定义用户元数据值更改用户角色

从WooCommerce管理订单汇总中隐藏原始小计行

使用 PHP,我可以将 foreach 语句放入瞬态中吗?

有没有办法像引用枚举一样引用注册表中的对象?

如何在 Laravel 中app文件夹之外的文件夹内创建文件路径?

使用 foreach php 将记录正确分配给正确的父级

使用用户元数据填写 woocommerce checkout 字段

由于 PHP 版本不受支持,如何终止 PHP 脚本?

如何设置Mailersend PHP API 模板变量?

正则表达式请帮我从内容中找到这个词

PHP删除具有相同键的项目,除了具有最低值的项目

htaccess 无限循环问题

PHP路由不重定向