gpt4 book ai didi

node.js - 具有命名空间的 xpath 查询根元素

转载 作者:行者123 更新时间:2023-12-03 12:18:46 24 4
gpt4 key购买 nike

我正在尝试使用命名空间寻址根元素并提供对库 xml-crypto 的引用.

我给的路径不对,请指教。目标是签署文档,以便可以将签名插入标签 <samlp:Response 之后

<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ID="efedb3b0-909f-4b39-b8c0-57427ee8dc83" Version="2.0" IssueInstant="2019-11-08T15:34:51.272Z">

<saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">http://www.example.com</saml:Issuer>

</samlp:Response>

nodeJS 代码

var SignedXml = require('xml-crypto').SignedXml, fs = require('fs');
var sig = new SignedXml();

sig.addReference("//*[local-name(.)='samlp:Response']");

sig.signingKey = fs.readFileSync(__dirname + "/client.pem");
sig.computeSignature(xml);
fs.writeFileSync(__dirname + "/signed.xml", sig.getSignedXml());

尝试

sig.addReference("//samlp:Response");

Error: Cannot resolve QName samlp

它在 https://www.freeformatter.com/xpath-tester.html 运行良好虽然

最佳答案

如果你想打败/绕过命名空间,那么改变

sig.addReference("//*[local-name(.)='samlp:Response']");

sig.addReference("//*[local-name()='Response']");

因为命名空间前缀 samlp 不是本地名称 Response 的一部分。

有关 XPath 中 namespace 的全面解答,请参阅 How does XPath deal with XML namespaces?

关于node.js - 具有命名空间的 xpath 查询根元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58770257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com