0

我有这个 XML:

<movie>
    <title>A History of Violence</title>
    <year>2005</year>
    <country>USA</country>
    <genre>Crime</genre>
</movie>
<movie>
    <title>Another Film</title>
    <year>2001</year>
    <country>USA</country>
    <genre>Crime</genre>
</movie>

我只会检索年份>2002 的电影。我对 XQuery 的标题和年份感兴趣,保留标签<movie>。我的预期结果示例:

<movie>
        <title>A History of Violence</title>
        <year>2005</year>
</movie>

我写了这个:

let $ms:=doc("movies.xml")
for $film in $ms/movie
where $film/year>2002
return $film

所以我只得到:

<movie>
    <title>A History of Violence</title>
    <year>2005</year>
    <country>USA</country>
    <genre>Crime</genre>
</movie>

如何从修改该查询的结果中“删除”国家和类型?

4

2 回答 2

2

吉尔卡是正确的。以下是一些额外的选择:

回报也可以表述为:

return element {'movie'} {
        $film/title,
        $film/year
     }

另一种选择是使用 functx 库:http ://www.xqueryfunctions.com/xq/functx_remove-elements.html 。

xquery version "1.0";

import module namespace  functx = "http://www.functx.com" at "functx.xqm";

let $data := <movies><movie>
    <title>A History of Violence</title>
    <year>2005</year>
    <country>USA</country>
    <genre>Crime</genre>
</movie>
<movie>
    <title>Another Film</title>
    <year>2001</year>
    <country>USA</country>
    <genre>Crime</genre>
</movie></movies>

for $film in $data/movie
where $film/year>2002
return functx:remove-elements($film, ('country', 'genre'))
于 2013-10-13T15:31:35.453 回答
1

我只是尝试

let $ms:=doc("movies.xml")/movies
for $film in $ms/movie
where $film/year>2002
return <movie>
            {$film/title}
            {$film/year}
         </movie>

对于输入(我添加了周围<movies>元素)

<?xml version="1.0" encoding="UTF-8"?>
<movies>
    <movie>
        <title>A History of Violence</title>
        <year>2005</year>
        <country>USA</country>
        <genre>Crime</genre>
    </movie>
    <movie>
        <title>Another Film</title>
        <year>2001</year>
        <country>USA</country>
        <genre>Crime</genre>
    </movie>
</movies>

结果

<movie>
    <title>A History of Violence</title>
    <year>2005</year>
</movie>
于 2013-10-13T13:49:47.057 回答