如何在注册命名空间后获取XML节点的值?

huangapple go评论69阅读模式
英文:

How to get xml node value after registering namespace?

问题

I am trying to get xml node value with xpath query but I am getting no results. I registered namespaces for the xml but there is no way to get the result although when I use http://xpather.com/ with the same query I get the right value.
Note when I remove registering namespaces lines I get undefined namespace error.

$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;

and this is the xml file:

<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 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>NzY2MGJkMjc0YTU0OTgzZjM2MzNmIzZjNhNWRiZjU4MTc1OTUyN2IxYg==</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:

<details>
<summary>英文:</summary>

I am trying to get xml node value with xpath query but I am getting no results. I registered namespaces for the xml but there is no way to get the result although when I use http://xpather.com/ with the same query I get the right value.
Note when I remove registering namespaces lines I get undefined namespace error.

    $digestValueXPath = &quot;//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&quot;;
    
    $xml = new DOMDocument(encoding: &#39;utf-8&#39;);
        $xml-&gt;loadXML(str_replace(&quot;\r&quot;, &quot;&quot;, $invoiceXML));
        $xpath = new DOMXPath($xml);

        $xpath-&gt;registerNamespace(&#39;sig&#39;, &quot;urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2&quot;);
        $xpath-&gt;registerNamespace(&#39;sac&#39;, &quot;urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2&quot;);
        $xpath-&gt;registerNamespace(&#39;sbc&#39;, &quot;urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2&quot;);
        $xpath-&gt;registerNamespace(&#39;ds&#39;, &quot;http://www.w3.org/2000/09/xmldsig#&quot;);
        $xpath-&gt;registerNamespace(&#39;xades&#39;, &quot;http://uri.etsi.org/01903/v1.3.2#&quot;);
        // dd($digestValueXPath);
        // $canonicalizationInvoiceXML = $xml-&gt;C14N();
        // $digestValue = $xml-&gt;getElementsByTagNameNS(&quot;http://www.w3.org/2000/09/xmldsig#&quot;, &quot;ds:DigestValue&quot;);
        $digestValue = $xpath-&gt;query($digestValueXPath);
        dd($digestValue);
        $digestValue-&gt;item(0)-&gt;nodeValue = $encodedHashedCert;


and this is the xml file

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

Update:
When using this expression it is working

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

答案1

得分: 2

您的发票也位于命名空间中:xmlns=&quot;urn:oasis:names:specification:ubl:schema:xsd:Invoice-2&quot;

只需添加以下内容,带有一些前缀,如:

$xpath-&gt;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";
英文:

You Invoice is also in a namespace: xmlns=&quot;urn:oasis:names:specification:ubl:schema:xsd:Invoice-2&quot;

Just add that also with some prefix like this:

$xpath-&gt;registerNamespace(&#39;default-ns&#39;, &quot;urn:oasis:names:specification:ubl:schema:xsd:Invoice-2&quot;);

And than change your XPath to:

$digestValueXPath = &quot;//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&quot;;

huangapple
  • 本文由 发表于 2023年7月11日 13:37:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定