【问题标题】:Xquery, How to add an attribute to an XML element with out using a functionXquery,如何在不使用函数的情况下向 XML 元素添加属性
【发布时间】:2023-03-29 23:31:01
【问题描述】:

这是起始 XML:

<sec sec-type="bodytext">
  <title>Largest Cities (Population)</title>
  <list list-type="bullet">
    <list-item>
      <p> Billings (103,994)</p>
    </list-item>
    <list-item>
      <p> Missoula (57,053)</p>
    </list-item>
  </list>
</sec>

我想将此属性 list-content="largest_cities" 添加到元素“list”。 这是我想要的输出:

<sec sec-type="bodytext">
  <title>Largest Cities (Population)</title>
  <list list-type="bullet" list-content="largest_cities">
    <list-item>
      <p> Billings (103,994)</p>
    </list-item>
    <list-item>
      <p> Missoula (57,053)</p>
    </list-item>
  </list>
</sec>

到目前为止,这是我的 Xquery:

declare variable $current_file as xs:string external;
for $s in doc($current_file)//sec[@sec-type="bodytext"]/list
for $r in doc($current_file)//sec[@sec-type="bodytext"]
where starts-with($r/title,'Ten Largest Cities')
return rename node $s as list[@list-content="largest_cities", 
                             @list-content="largest_cities"]

有什么想法吗?

【问题讨论】:

  • 正如您已经指出的那样,您宁愿添加一个节点而不是重命名它。也许您可以使用“插入”表达式来回答您自己的问题?

标签: xml xquery


【解决方案1】:
for $sec in doc($current_file)//sec
where $sec/@sec-type eq "bodytext" and
      starts-with($sec/title,'Largest Cities')
return
  insert node
    attribute list-content { "largest_cities" }
  into
    $sec/list

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-28
    • 2023-03-27
    • 2011-02-13
    • 2010-11-18
    • 2017-05-01
    • 2015-07-21
    • 1970-01-01
    • 2020-09-18
    相关资源
    最近更新 更多