输出中的重复数据 - xquery

时间:2013-04-25 02:26:22

标签: xml xquery

我是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,那么这两部电影应该与导演合并,而这部电影正在发生但是我这两次了(见输出)。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>