首页 > 解决方案 > 使用 xquery (basex) 重命名 XML 中的节点

问题描述

这是端点匹配时运行的脚本%rest:path

XQuery

module namespace page = 'http://basex.org/examples/web-page';

import module namespace file = "http://expath.org/ns/file";

declare %updating
 %rest:path("/fnac")
 %rest:POST("{$body}")
 function page:fnac($body) {
   
 file:write("fnacData.xml",$body),
 db:create('prices', 'fnacData.xml', "./"),

 let $update := doc('C:\Users\franc\Desktop\basex\bin\fnacData.xml')
 for $n in $update//_
 return rename node $n as 'smartphone',

 update:output('Update Successful')
};

XML

(内容$body

<json type="array">
  <_ type="object">
    <name>Samsung Galaxy S10+</name>
    <brand>Samsung</brand>
    <pageUrl>https://www.fnac.pt/Samsung-Galaxy-S10-G975FZ-128GB-Verde-Telemovel-SmartPhone-Android/a6587862</pageUrl>
    <price>1029.99</price>
    <details>G975FZ - 128GB - Verde</details>
    <imgUrl>https://static.fnac-static.com/multimedia/Images/PT/NR/ab/5c/4d/5069995/1545-1.jpg</imgUrl>
    <dateTime type="number">1579132283666</dateTime>
    <store>fnac</store>
  </_>
  <_ type="object">
    <name>Xiaomi Mi 9 Lite</name>
    <brand>Xiaomi</brand>
    <pageUrl>https://www.fnac.pt/Smartphone-Xiaomi-Mi-9-Lite-64GB-Black-Telemovel-SmartPhone-Android/a7166673</pageUrl>
    <price>339.99</price>
    <details>64GB - Black</details>
    <imgUrl>https://static.fnac-static.com/multimedia/Images/PT/NR/ca/73/55/5600202/1545-1.jpg</imgUrl>
    <dateTime type="number">1579132283667</dateTime>
    <store>fnac</store>
  </
</json>

该脚本应该将节点重命名<_><smartphone>并更新 XML 文件,但我无法使其工作。它确实返回“更新成功”,但 XML 文件保持不变

标签: xmlapixquerybasex

解决方案


推荐阅读