具有名称空间的xpath查询根元素

我试图用命名空间寻址根元素,并提供对库xml-crypto的引用。

我没有正确给出路径,请告知。目的是对文档进行签名,以便可以在标签results = []; db.collection('schedules').find().forEach(function(d) { results.push(d); });

之后插入签名。
<samlp:Response

nodeJS代码

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

尝试

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());
  

错误:无法解析QName samlp

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

qq842663987 回答:具有名称空间的xpath查询根元素

如果您想击败/绕过命名空间,请更改

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

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

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

有关XPath中名称空间的全面解答,请参见How does XPath deal with XML namespaces?

本文链接:https://www.f2er.com/3136516.html

大家都在问