2017-10-16 63 views
0

我有一塊XML,我需要掃描並用解密值替換所有加密值。我有一個解密函數,並且需要解密的xml元素將具有一個屬性,表明它們已被加密。並非所有的值都被加密,並且除了新的解密值之外,返回的XML必須與起始XML相同。 我可以認爲,如果無論如何要做到這一點。我是xquery的新手。下面預先XQuery - 用解密值替換xml值(db2)

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

感謝

例如XML。

+0

這是在DB2 for z/OS上或在Linux/Unix/Windows的?你試過什麼了? –

+0

因此,您想要替換一個元素,該元素(正確地)說明其內容是使用內容被解密但說明(錯誤地)內容仍然被加密的元素加密的元素?找到指定這種行爲的設計師並執行嚴重的諮詢。 –

+0

代碼將作爲數據庫包在數據庫中運行,所以我一直在嘗試使用xquery,但是我對如何解決這個問題有點難以理解。主要的問題是,我不會事先沒有加密哪些柱子,所以程序需要是通用的。 –

回答

0

下面應該是一個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>