Xquery,如何使用函数将属性添加到XML元素

时间:2014-11-17 22:58:17

标签: xml xquery

这是起始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"]

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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