問題描述
我使用 xml 和 PHP 創建了一個發票管理系統,但最新的 PHP 版本不支持 XSLT 2.0 - 因此我必須尋找 sum() 替代方案.如何將有效的 xslt 2 函數sum(unitprice *quantity)"轉換為 xslt 1.0 ?
I create an invoice management system using xml and PHP but the latest PHP version does not support XSLT 2.0 - therefore I have to look for an sum() alternative. How can I transform the valid xslt 2 function "sum(unitprice * quantity)" to xslt 1.0 ?
XML 內容基于 John 的例子
我用節點集嘗試過它,但它不受支持xmlXPathCompOpEval:在中找不到函數節點集"
I tried it with a node-set, but it is not supported "xmlXPathCompOpEval: function node-set not found in"
推薦答案
實際上,sum(price*quantity)
不是有效的 XSLT 2.0 語法.我猜你的意思是類似 `sum(for $x in * return $x/price*$x/quantity).
Actually, sum(price*quantity)
isn't valid XSLT 2.0 syntax. I guess you mean something like `sum(for $x in * return $x/price*$x/quantity).
可以通過以下方式對 XSLT 1.0 中的計算值求和:
Summing over a computed value in XSLT 1.0 can be done in the following ways:
兩遍解決方案,創建一個節點集,其中節點保存計算值,然后對節點集求和
two pass solution, create a node-set in which the nodes hold the computed values, then sum over the node-set
遞歸解決方案:編寫一個遞歸模板,將total作為參數,為當前節點加上sum*quantity,然后遞歸處理剩余的節點
recursive solution: write a recursive template that passes the total so far as a parameter, adds sum*quantity for the current node, then recurses to process the remaining nodes
高階解決方案:使用 Dimitre Novatchev 的 FXSL 庫
higher-order solution: use Dimitre Novatchev's FXSL library
擴展解決方案:使用像saxon:sum()這樣的擴展函數
extension solution: use extension functions like saxon:sum()
這篇關于xslt 1 和 sum 函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!