1

我对 xQuery 很陌生,我必须编写以下查询;

显示每个导演的 DVD 标题。

我的 xQuery 是;

let $doc := doc("DVDStore.xml")//DVDTitle
return <Directors>{
    for $dir in $doc/Directors/Name    
        let $t := $doc[$dir = Directors/Name]/Title
        return <DVDTitles> {$dir} {$t} </DVDTitles>
    } </Directors>

DVDStore.xml 是;

<?xml version="1.0" encoding="UTF-8"?>
<DVDStore>
    <DVDTitles>
        <DVDTitle>
            <Title>A. I. Artificial Intelligence</Title>
            <Date>2002-03-05</Date>
            <Studio>Universal Studios</Studio>
            <Rated>PG-13</Rated>
            <Code>1</Code>
            <Directors>
                <Name>Steven Spielberg</Name>                
            </Directors>
            <Starrings>
                <Name>Haley Joel Osment</Name>
                <Name>Jude Law</Name>
            </Starrings>
        </DVDTitle>
        <DVDTitle>
            <Title>Terminator 2-Judgment Day</Title>
            <Date>2001-01-01</Date>
            <Studio>Artisan Entertainment</Studio>
            <Rated>R</Rated>
            <Code>6</Code>
            <Directors>
                <Name>James Cameron</Name>                
            </Directors>
            <Starrings>
                <Name>Arnold Schwarzenegger</Name>
                <Name>Linda Hamilton</Name>
            </Starrings>
        </DVDTitle>
        <DVDTitle>
            <Title>Titanic</Title>
            <Date>1999-08-31</Date>
            <Studio>Paramount Home Video</Studio>
            <Rated>PG-13</Rated>
            <Code>2</Code>
            <Directors>
                <Name>James Cameron</Name>  
            </Directors>
            <Starrings>
                <Name>Leonardo DiCaprio</Name>
                <Name>Kate Winslet</Name>
            </Starrings>
        </DVDTitle>
    </DVDTitles>
</DVDStore>

我得到以下输出;

<?xml version="1.0" encoding="UTF-8"?>
<Directors>
   <DVDTitles>
      <Name>Steven Spielberg</Name>
      <Title>A. I. Artificial Intelligence</Title>
   </DVDTitles>
   <DVDTitles>
      <Name>James Cameron</Name>
      <Title>Terminator 2-Judgment Day</Title>
      <Title>Titanic</Title>
   </DVDTitles>
   <DVDTitles>
      <Name>James Cameron</Name>
      <Title>Terminator 2-Judgment Day</Title>
      <Title>Titanic</Title>
   </DVDTitles>
</Directors>

问题是,当一位导演导演了两部电影或多部电影时,就像James Cameron导演了一样Terminator 2-Judgment DayTitanic然后这两个标题都应该与发生正确的导演分组,但我得到了两次(见输出)。

有人可以帮忙吗?

4

2 回答 2

1

如果您的处理器支持 XQuery 3.0,您可能需要查看该group by语句,因为在我看来它非常优雅,并且还可以阅读更多您实际想要做的事情:处理每个 DVD 标题,按导演分组并输出一些信息.

let $doc := doc("DVDStore.xml")//DVDTitle
return <Directors>{
  for $title in $doc
  let $director := $title/Directors/Name
  group by $director
  return <DVDTitles><Name>{$director}</Name>{$title/Title}</DVDTitles>
} </Directors>
于 2013-04-25T11:37:19.947 回答
1

我找到了解决方案。我错过了关键字 distinct-values。工作查询是;

let $doc := doc("DVDStore.xml")//DVDTitle
return <Directors>{
    for $dir in distinct-values($doc/Directors/Name)
        let $t := $doc[$dir = Directors/Name]/Title
        return <DVDTitles> {$dir} {$t} </DVDTitles>
    } </Directors>
于 2013-04-25T03:44:30.100 回答