我有一个XML块,我需要扫描它并用解密的值替换所有加密的值。我有一个解密功能,需要解密的xml元素将有一个属性指示它们已加密。并非所有值都经过加密,并且返回的 XML 必须与起始 XML 相同,但新的解密值除外。我可以考虑是否无论如何都要这样做。 我是Xquery的新手。
下面的示例 XML
<book>
<title encrypted=true>0234534rdf;skdlfsd</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book>
<title encrypted=true>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
提前谢谢。
下面应该是一个 xQuery 1.0 安全解决方案。它本可以更紧凑,但留作全面审查。
xquery version "1.0";
declare function local:encrypt($node){
'an encrypted value'
};
declare function local:decrypt($node){
'a decrypted value'
};
declare function local:traverse-and-encrypt($nodes as node()*) as node()*{
for $n in $nodes
return if($n/@encrypted = 'true')
then element{fn:node-name($n)}{
for $a in $n/@*
return if(fn:local-name($a) = 'encrypted')
then attribute {'encrypted'} {'false'}
else $a,
local:decrypt($n/text())
}
else if($n/@encrypted = 'false')
then element{fn:node-name($n)}{
for $a in $n/@*
return if(fn:local-name($a) = 'encrypted')
then attribute {'encrypted'} {'true'}
else $a,
local:encrypt($n/text())
}
else element{fn:node-name($n)}{
$n/@*,
$n/text(),
for $child in $n/*
return local:traverse-and-encrypt($child)
}
};
let $doc :=
<books>
<book>
<title encrypted="true">0234534rdf;skdlfsd</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book>
<title encrypted="false">Another book</title>
<author test='testing attributes'>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</books>
return local:traverse-and-encrypt($doc)
返回:
<books>
<book>
<title encrypted="false">a decrypted value</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book>
<title encrypted="true">an encrypted value</title>
<author test="testing attributes">J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</books>