[英]xQuery update question
Here is my xml这是我的 xml
<book asin="0201100886"
created="128135928"
lastLookupTime="128135928">
<price>102.00</price>
<purchaseDate>2005-01-22</purchaseDate>
</book>
<book asin="0122513363" created="128135600" lastLookupTime="128136224">
<price>50.95</price>
<purchaseDate>2005-01-22</purchaseDate>
</book>
<book asin="0201441241"
created="128136896"
lastLookupTime="128136896">
<price>108.20</price>
<purchaseDate>2005-01-22</purchaseDate>
</book>
<book asin="0471250600"
created="128136896"
lastLookupTime="128136896">
<price>107.95</price>
<purchaseDate>2005-01-22</purchaseDate>
</book>
<book asin="0321193628"
created="128136896"
lastLookupTime="128136896">
<price>112.40</price>
</book>
I need to update all the books prices by 0.50 cents.我需要将所有书籍价格更新 0.50 美分。 How can I do this?
我怎样才能做到这一点?
If you are looking for a solution using XQuery Update Facility:如果您正在寻找使用 XQuery 更新工具的解决方案:
for $p in //price return
replace value of node $p with $p + 0.50
In XQuery, a recursive function is used to do the transformation.在 XQuery 中,使用递归 function 进行转换。
declare function local:reprice($nodes as node()*) {
for $node in $nodes
return
typeswitch($node)
case element(price)
return element price {xs:decimal($node) + 0.5}
case element(
return
element {name($node)} {
$node/@*,
local:reprice($node/node())}
default
return $node
};
then if the books document is referenced as $books:那么如果书籍文档被引用为 $books:
local:reprice($books)
Further discussion and examples are in http://en.wikibooks.org/wiki/XQuery/Typeswitch_Transformations .进一步的讨论和示例在http://en.wikibooks.org/wiki/XQuery/Typeswitch_Transformations中。
Alternatively, use an XML database such as eXist-db and update insitu:或者,使用 XML 数据库,例如 eXist-db 并就地更新:
for $price in $books//price
let $newprice := xs:decimal($price) + 0.5
return update replace $price with elememt price {$newprice}
Note that XQuery update extensions are mainly implementation-dependant until XQuery Update is more widely supported请注意,在 XQuery 更新得到更广泛的支持之前,XQuery 更新扩展主要依赖于实现
XSLT is significantly more appropriate to use than XQuery for such kind of tasks : XSLT 比 XQuery 更适合用于此类任务:
This transformation :这种转变:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="price/text()">
<xsl:value-of select="format-number(. + 0.50, '##0.00')"/>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document :当应用于提供的 XML 文档时:
<books>
<book asin="0201100886" created="128135928" lastLookupTime="128135928">
<price>102.00</price>
<purchaseDate>2005-01-22</purchaseDate>
</book>
<book asin="0122513363" created="128135600" lastLookupTime="128136224">
<price>50.95</price>
<purchaseDate>2005-01-22</purchaseDate>
</book>
<book asin="0201441241" created="128136896" lastLookupTime="128136896">
<price>108.20</price>
<purchaseDate>2005-01-22</purchaseDate>
</book>
<book asin="0471250600" created="128136896" lastLookupTime="128136896">
<price>107.95</price>
<purchaseDate>2005-01-22</purchaseDate>
</book>
<book asin="0321193628" created="128136896" lastLookupTime="128136896">
<price>112.40</price>
</book>
</books>
produces the wanted, correct result :产生想要的正确结果:
<books>
<book asin="0201100886" created="128135928" lastLookupTime="128135928">
<price>102.50</price>
<purchaseDate>2005-01-22</purchaseDate>
</book>
<book asin="0122513363" created="128135600" lastLookupTime="128136224">
<price>51.45</price>
<purchaseDate>2005-01-22</purchaseDate>
</book>
<book asin="0201441241" created="128136896" lastLookupTime="128136896">
<price>108.70</price>
<purchaseDate>2005-01-22</purchaseDate>
</book>
<book asin="0471250600" created="128136896" lastLookupTime="128136896">
<price>108.45</price>
<purchaseDate>2005-01-22</purchaseDate>
</book>
<book asin="0321193628" created="128136896" lastLookupTime="128136896">
<price>112.90</price>
</book>
</books>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.