如何从root元素中使用所有inscope名称空间从XML获取节点

发布于 2025-01-17 16:27:01 字数 6162 浏览 0 评论 0原文

这是我的XML文件内容,

<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:ds=" http://www.w3.org/2000/09/xmldsig#" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" 
xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2">
    <ext:UBLExtensions>
        <ext:UBLExtension>
            <ext:ExtensionURI>urn:oasis:names:specification:ubl:dsig:enveloped:xades</ext:ExtensionURI>
            <ext:ExtensionContent>
              <sig:UBLDocumentSignatures>
               <sac:SignatureInformation>
                  <cbc:ID>urn:oasis:names:specification:ubl:signature:1</cbc:ID>
                  <sbc:ReferencedSignatureID>urn:oasis:names:specification:ubl:signature:Invoicesadas</sbc:ReferencedSignatureID>
                  <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="signature">
                     <ds:SignedInfo>
                         <!-- SOME CONTENT HERE -->
                    </ds:SignedInfo>
                     <ds:SignatureValue></ds:SignatureValue>
                     <ds:KeyInfo>
                        <ds:X509Data>
                           <ds:X509Certificate></ds:X509Certificate>
                        </ds:X509Data>
                     </ds:KeyInfo>
                     <ds:Object>
                        <xades:QualifyingProperties xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" Target="signature">
                           <xades:SignedProperties Id="xadesSignedProperties">
                              <xades:SignedSignatureProperties>
                                 <xades:SigningTime>2020-12-25T12:57:51Z</xades:SigningTime>
                                 <xades:SigningCertificate>
                                    <xades:Cert>
                                       <xades:CertDigest>
                                          <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
                                          <ds:DigestValue> <!-- SOME VALUE HERE --></ds:DigestValue>
                                       </xades:CertDigest>
                                       <xades:IssuerSerial>
                                          <ds:X509IssuerName><!-- SOME VALUE HERE--></ds:X509IssuerName>
                                          <ds:X509SerialNumber><!-- SOME VALUE HERE--></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>
   <!-- SOME CONTENT HERE -->
</Invoice>

我想检索文件中所有范围XMLNS名称空间的签名Properties元素。 我需要的样本输出

<?xml version="1.0" encoding="UTF-8"?> <xades:SignedProperties **xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
 xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" 
xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2"** xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" Id="xadesSignedProperties">
                                        <xades:SignedSignatureProperties>
                                            <xades:SigningTime>020-12-25T12:57:51Z</xades:SigningTime>
                                            <xades:SigningCertificate>
                                                <xades:Cert>
                                                    <xades:CertDigest>
                                                        <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
                                                        <ds:DigestValue> <!-- SOME VALUE HERE --></ds:DigestValue>
                                                    </xades:CertDigest>
                                                    <xades:IssuerSerial>
                                                        <ds:X509IssuerName> <!-- SOME VALUE HERE --></ds:X509IssuerName>
                                                        <ds:X509SerialNumber> <!-- SOME VALUE HERE --></ds:X509SerialNumber>
                                                    </xades:IssuerSerial>
                                                </xades:Cert>
                                            </xades:SigningCertificate>
                                        </xades:SignedSignatureProperties>
                                    </xades:SignedProperties>

,但是我正在获取输出,而没有文件中的根元素中定义的名称空间。

我应该如何获得XML文件中范围中声明的所有名称空间的元素?

Here is my XML file content

<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:ds=" http://www.w3.org/2000/09/xmldsig#" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" 
xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2">
    <ext:UBLExtensions>
        <ext:UBLExtension>
            <ext:ExtensionURI>urn:oasis:names:specification:ubl:dsig:enveloped:xades</ext:ExtensionURI>
            <ext:ExtensionContent>
              <sig:UBLDocumentSignatures>
               <sac:SignatureInformation>
                  <cbc:ID>urn:oasis:names:specification:ubl:signature:1</cbc:ID>
                  <sbc:ReferencedSignatureID>urn:oasis:names:specification:ubl:signature:Invoicesadas</sbc:ReferencedSignatureID>
                  <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="signature">
                     <ds:SignedInfo>
                         <!-- SOME CONTENT HERE -->
                    </ds:SignedInfo>
                     <ds:SignatureValue></ds:SignatureValue>
                     <ds:KeyInfo>
                        <ds:X509Data>
                           <ds:X509Certificate></ds:X509Certificate>
                        </ds:X509Data>
                     </ds:KeyInfo>
                     <ds:Object>
                        <xades:QualifyingProperties xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" Target="signature">
                           <xades:SignedProperties Id="xadesSignedProperties">
                              <xades:SignedSignatureProperties>
                                 <xades:SigningTime>2020-12-25T12:57:51Z</xades:SigningTime>
                                 <xades:SigningCertificate>
                                    <xades:Cert>
                                       <xades:CertDigest>
                                          <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
                                          <ds:DigestValue> <!-- SOME VALUE HERE --></ds:DigestValue>
                                       </xades:CertDigest>
                                       <xades:IssuerSerial>
                                          <ds:X509IssuerName><!-- SOME VALUE HERE--></ds:X509IssuerName>
                                          <ds:X509SerialNumber><!-- SOME VALUE HERE--></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>
   <!-- SOME CONTENT HERE -->
</Invoice>

I want to retrieve the SignedProperties element with all the in scope xmlns namespaces in the file.
Sample Output that i need looks like

<?xml version="1.0" encoding="UTF-8"?> <xades:SignedProperties **xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
 xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" 
xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2"** xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" Id="xadesSignedProperties">
                                        <xades:SignedSignatureProperties>
                                            <xades:SigningTime>020-12-25T12:57:51Z</xades:SigningTime>
                                            <xades:SigningCertificate>
                                                <xades:Cert>
                                                    <xades:CertDigest>
                                                        <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
                                                        <ds:DigestValue> <!-- SOME VALUE HERE --></ds:DigestValue>
                                                    </xades:CertDigest>
                                                    <xades:IssuerSerial>
                                                        <ds:X509IssuerName> <!-- SOME VALUE HERE --></ds:X509IssuerName>
                                                        <ds:X509SerialNumber> <!-- SOME VALUE HERE --></ds:X509SerialNumber>
                                                    </xades:IssuerSerial>
                                                </xades:Cert>
                                            </xades:SigningCertificate>
                                        </xades:SignedSignatureProperties>
                                    </xades:SignedProperties>

But I am getting output without the namespaces defined in the root element in the file.

How should i get the element with all the namespaces declared in scope in the XML file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡 2025-01-24 16:27:01

似乎您需要在提取的 xades:SignedProperties 节点上应用规范化。
您可以尝试使用 Canonicalizer来自 Apache Santuario 库并对提取的节点进行规范化:

public static byte[] canonicalizeSubtree(final String canonicalizationMethod, final Node node) {
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        final Canonicalizer c14n = Canonicalizer.getInstance(getCanonicalizationMethod(canonicalizationMethod));
        c14n.canonicalizeSubtree(node, baos);
        return baos.toByteArray();
    } catch (Exception e) {
        // some logic here
    }
}

对于规范化方法,它更像是“包容性”规范化,例如: “http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments”(请参阅​​ALGO_ID_C14N_WITH_COMMENTS)。

It seems like you need to apply canonicalization on the extracted xades:SignedProperties node.
You may try to use Canonicalizer from Apache Santuario library and canonicalize the extracted node:

public static byte[] canonicalizeSubtree(final String canonicalizationMethod, final Node node) {
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        final Canonicalizer c14n = Canonicalizer.getInstance(getCanonicalizationMethod(canonicalizationMethod));
        c14n.canonicalizeSubtree(node, baos);
        return baos.toByteArray();
    } catch (Exception e) {
        // some logic here
    }
}

For the canonicalization method, it more looks like an "inclusive" canonicalization, e.g.: "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments" (see ALGO_ID_C14N_WITH_COMMENTS).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文