我试图使用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相关问答推荐

PHP cUrl扩展与v8.2.12更新损坏

在ShipStation for WooCommerce中使用自定义订单项目元数据

在WooCommerce中禁用特定日期之前的免费送货

WooCommerce常规价格增加额外的附加平价

目标类[Set_Locale]不存在.拉威尔

如何从该字符串中删除这些图标转换的ASCII问号字符?

使用ESI的Symfony Sulu清漆

创建一个Woocommerce我的帐户订单页面,仅显示已完成的订单

将WooCommerce WC_Order对象与wp_Schedule_Event一起使用

防止同时从Laravel中的用户帐户中提取

以编程方式同步 WPML 翻译更改 Woocommerce 产品销售价格

在 PHP 中将字符串分解为数组(字符串类似于 WP 短代码)

使用帖子 ID 更新 wp 帖子

AWS S3:当对象名称包含 % 时复制对象失败并出现错误 - 无效的复制源编码

PHP:数组的等边

Composer自动加载器重复了子类的类文件路径

PHP Symfony 在测试.env文件中将接口自动装配作为构造函数参数,但使用接口的特定类

我需要一个正则表达式模式来获取在不包含特定字符串后面的模式中的文本

Laravel auth()->id() 在生产服务器中不工作

如何从此字符串创建 Carbon 对象:2022-06-21T05:52:46.648533678Z?