0

我正在编写此代码以根据专辑的年份列出专辑,并提供与每个标题关联的 id 和属性,但我得到的 id 为空。

<Production_by_years>
{
    for $i in distinct-values(//Album/Year/text())
    return element {"Year"}
    {
        element {"year"}{$i},
        for $o in //Album/Year[text() eq $i]
        return element {"Title"}{attribute {"id"}{//Album/Year[text() eq $i]/../@ALbumID},$o/text()}
    }
}
</Production_by_years>

这是xml文件

      <Artist ArtistID="Ar_0000" countryID="US">
        <Name>
            <FirstName>Katey </FirstName>
            <LastName>Berry</LastName>           
        </Name>
         <pic>Dev.JPG</pic>
    </Artist>
    <Artist ArtistID="Ar_0002" countryID="UK">
        <Name>
            <FirstName>Selena  </FirstName>
            <LastName>Gomez</LastName>

        </Name>

    <pic/></Artist>
    <Artist ArtistID="Ar_0003" countryID="US">
        <Name>
            <FirstName>Avril  </FirstName>
            <LastName>Lavigne</LastName>

        </Name>
      <pic>avrail.JPG</pic>
    </Artist>
    <Artist ArtistID="Ar_0014" countryID="UK">
        <Name>
            <FirstName>Marlon </FirstName>
            <LastName>Roudette</LastName>

        </Name>

    </Artist>
    <Artist ArtistID="Ar_0017" countryID="US">
        <Name>
            <FirstName>Julio </FirstName>
            <LastName>Iglesias</LastName>

        </Name>

    </Artist>
    <Artist ArtistID="Ar_0015" countryID="US">
        <Name>
            <FirstName>Dev </FirstName>
            <LastName>Dev</LastName>

        </Name>

    </Artist>
    <Artist ArtistID="Ar_0005" countryID="FR">
        <Name>
            <FirstName>Kelly </FirstName>
            <LastName>Clarkson </LastName>


        </Name>

    </Artist>
    <Artist ArtistID="Ar_0006" countryID="FR">
        <Name>
            <FirstName>Charles  </FirstName>
            <LastName> Aznavour</LastName>

        </Name>

    </Artist>
    <Artist ArtistID="Ar_0007" countryID="ME">
        <Name>
            <FirstName>Serebro </FirstName>
            <LastName>Serebro</LastName>

        </Name>

    </Artist>  
    <Artist ArtistID="Ar_0008" countryID="US">
        <Name>
            <FirstName>Justine </FirstName>
            <LastName>beiber</LastName>

        </Name>


    <pic>b.jpg</pic></Artist> 
    <Artist ArtistID="Ar_0016" countryID="FR">
        <Name>
            <FirstName>Paul</FirstName>
            <LastName>Mauria</LastName>

        </Name>

    </Artist>

    <Artist ArtistID="Ar_0010" countryID="US">

        <Name>
            <FirstName>Elissa </FirstName>
            <LastName>Elissa</LastName>

        </Name>

    </Artist>
    <Artist ArtistID="Ar_0001" countryID="US">
        <Name>

        <FirstName>farpus </FirstName>
        <LastName>ff</LastName>
        </Name>


    </Artist>   
    <Artist ArtistID="Ar_0009" countryID="FR">
        <Name>
            <FirstName>Mireille </FirstName>
            <LastName>Mathieu</LastName>
        </Name>

        </Artist>

    <Artist ArtistID="Ar_0013" countryID="US">
        <Name>
            <FirstName>Rami</FirstName>
            <LastName>Ayash</LastName>

        </Name>

    </Artist>
    <Artist ArtistID="Ar_0011" countryID="US">
        <Name>
            <FirstName>Nancy </FirstName>
            <LastName>Ajram</LastName>

        </Name>

    </Artist>  
     <Artist ArtistID="Ar_0012" countryID="US">
        <Name>
            <FirstName>Wafiq </FirstName>
            <LastName>Habib</LastName>

        </Name>

    </Artist> 
    <Album AlbumID="AL_0000" ArtistID="Ar_0000" CompanyID="C_3" GenreName="Pop HipHop R_and_B">
        <Title>Calfornia Girls</Title>
        <Price>12.00</Price>      
        <Year>2012</Year>
    </Album>  
  <Album AlbumID="AL_0017" ArtistID="Ar_0001" CompanyID="C_3" GenreName="Pop HipHop R_and_B">
        <Title>CCéline Dion</Title>
        <Price>12.00</Price>      
        <Year>2009</Year>
    </Album>
    <Album AlbumID="AL_0001" ArtistID="Ar_0002" CompanyID="C_1" GenreName="Pop HipHop R_and_B">
        <Title>confessions</Title>
        <Price>9.00</Price>
        <Year>2008</Year>
    </Album>
    <Album AlbumID="AL_0800" ArtistID="Ar_0000" CompanyID="C_5" GenreName="Pop HipHop R_and_B">
        <Title> Mourir d'aimer</Title>
        <Price>13.00</Price>
        <Year>2014</Year>
    </Album><Album AlbumID="AL_0801" ArtistID="Ar_0002" CompanyID="C_8" GenreName="Pop HipHop R_and_B">
        <Title>Diseny girl</Title>
        <Price>13.00</Price>
        <Year>2014</Year>
    </Album><Album AlbumID="AL_0802" ArtistID="Ar_0006" CompanyID="C_3" GenreName="Pop HipHop R_and_B">
        <Title>also this</Title>
        <Price>13.00</Price>
        <Year>2009</Year>
    </Album><Album AlbumID="AL_0804" ArtistID="Ar_0006" CompanyID="C_8" GenreName="Pop HipHop R_and_B">
        <Title>now </Title>
        <Price>6</Price>
        <Year>2010</Year>
    </Album><Album AlbumID="AL_0806" ArtistID="Ar_0006" CompanyID="C_3" GenreName="Pop HipHop R_and_B">
        <Title>for love</Title>
        <Price>6</Price>
        <Year>2006</Year>
    </Album><Album AlbumID="AL_0807" ArtistID="Ar_0006" CompanyID="C_1" GenreName="Pop HipHop R_and_B">
        <Title>I can do this</Title>
        <Price>13.00</Price>
        <Year>2014</Year>
    </Album><Album AlbumID="AL_0002" ArtistID="Ar_0006" CompanyID="C_6" GenreName="Pop HipHop R_and_B">
        <Title>Here adn now</Title>
        <Price>13.00</Price>
        <Year>2014</Year>
    </Album>
    <Album AlbumID="AL_0003" ArtistID="Ar_0000" CompanyID="C_4" GenreName=" HipHop R_and_B">
        <Title>Teenge Dream</Title>
        <Price>11.00</Price>
        <Year>2010</Year>
    </Album>  
  <Album AlbumID="AL_0005" ArtistID="Ar_0008" CompanyID="C_4" GenreName="HipHop">
        <Title>Future of sex</Title>
        <Price>8.00</Price>
        <Year>2007</Year>
    </Album><Album AlbumID="AL_1000" ArtistID="Ar_0008" CompanyID="C_4" GenreName="HipHop">
        <Title>Love Stond</Title>
        <Price>8.00</Price>
        <Year>2011</Year>
    </Album><Album AlbumID="AL_1001" ArtistID="Ar_0008" CompanyID="C_4" GenreName="HipHop">
        <Title>Baby</Title>
        <Price>8.00</Price>
        <Year>2012</Year>
    </Album><Album AlbumID="AL_1002" ArtistID="Ar_0008" CompanyID="C_4" GenreName="HipHop">
        <Title>never say never</Title>
        <Price>8.00</Price>
        <Year>2007</Year>
    </Album>
    <Album AlbumID="AL_0450" ArtistID="Ar_0016" CompanyID="C_5" GenreName="HipHop">
        <Title>Toccata</Title>
        <Price>12.00</Price>
        <Year>2013</Year>
    </Album>
  <Album AlbumID="AL_0006" ArtistID="Ar_0001" CompanyID="C_5" GenreName="HipHop">
        <Title>shade</Title>
        <Price>12.00</Price> 
        <Year>2011</Year>
    </Album>
  <Album AlbumID="AL_0032" ArtistID="Ar_0010" CompanyID="C_5" GenreName="HipHop">
        <Title>shade</Title>
        <Price>12.00</Price> 
        <Year>2014</Year>
    </Album>

  <Album AlbumID="AL_0007" ArtistID="Ar_0002" CompanyID="C_3" GenreName="Metal">
        <Title>Love You Like A Love Song</Title>
        <Price>19.00</Price>      
        <Year>2011</Year>
    </Album>
    <Album AlbumID="AL_0008" ArtistID="Ar_0002" CompanyID="C_1" GenreName="Metal">
        <Title>Wish You Were Here</Title>
        <Price>9.00</Price>
        <Year>2008</Year>
    </Album>
    <Album AlbumID="AL_0009" ArtistID="Ar_0002" CompanyID="C_3" GenreName="Metal">
        <Title>I Like How It Feel</Title>
        <Price>13.00</Price>
        <Year>2013</Year>
    </Album>
    <Album AlbumID="AL_0010" ArtistID="Ar_0002" CompanyID="C_3" GenreName="Metal">
        <Title>J'entends la musique</Title>
        <Price>11.00</Price>
        <Year>2008</Year>
    </Album> 
  <Album AlbumID="AL_0011" ArtistID="Ar_0011" CompanyID="C_2" GenreName="Metal">
        <Title>AL7B KLO</Title>
        <Price>57.00</Price>
        <Year>2007</Year>
    </Album>
  <Album AlbumID="AL_0240" ArtistID="Ar_0012" CompanyID="C_5" GenreName="Metal">
        <Title>LAEK LAKE</Title>
        <Price>12.00</Price>
        <Year>2013</Year>
  </Album><Album AlbumID="AL_0012" ArtistID="Ar_0012" CompanyID="C_5" GenreName="Metal">
        <Title>I like it</Title>
        <Price>12.00</Price>
        <Year>2013</Year>
    </Album>
    <Album AlbumID="AL_0013" ArtistID="Ar_0013" CompanyID="C_5" GenreName="Electro">
        <Title>I like you</Title>
        <Price>12.00</Price> 
        <Year>2010</Year>
    </Album><Album AlbumID="AL_0234" ArtistID="Ar_0002" CompanyID="C_5" GenreName="Electro">
        <Title>Far more</Title>
        <Price>12.00</Price> 
        <Year>2010</Year>
    </Album><Album AlbumID="AL_0233" ArtistID="Ar_0002" CompanyID="C_5" GenreName="Electro">
        <Title>Now here</Title>
        <Price>12.00</Price> 
        <Year>2010</Year>
    </Album>
    <Album AlbumID="AL_0090" ArtistID="Ar_0002" CompanyID="C_6" GenreName="HipHop Electro">
        <Title>HABEBE</Title>
        <Price>12.00</Price> 
        <Year>2010</Year>
    </Album><Album AlbumID="AL_0100" ArtistID="Ar_0002" CompanyID="C_4" GenreName="HipHop Electro">
        <Title>say no more</Title>
        <Price>12.00</Price> 
        <Year>2010</Year>
    </Album>

    <Album AlbumID="AL_0345" ArtistID="Ar_0006" CompanyID="C_2" GenreName="HipHop Electro">



        <Title>We Found Love</Title>
        <Price>12.00</Price>
        <Year>2005</Year>
    </Album><Album AlbumID="AL_0015" ArtistID="Ar_0002" CompanyID="C_4" GenreName="HipHop Electro">



        <Title>love is here</Title>
        <Price>6</Price>
        <Year>2005</Year>
    </Album>

  <Album AlbumID="AL_0016" ArtistID="Ar_0010" CompanyID="C_1" GenreName="Metal">



        <Title>top of top </Title>
        <Price>5</Price>
        <Year>2011</Year>
  </Album><Album AlbumID="AL_0066" ArtistID="Ar_0003" CompanyID="C_2" GenreName="Metal">



        <Title>take care</Title>
        <Price>5</Price>
        <Year>2005</Year>
  </Album><Album AlbumID="AL_0067" ArtistID="Ar_0003" CompanyID="C_6" GenreName="Metal">



        <Title>bad girl</Title>
        <Price>5</Price>
        <Year>2011</Year>
    </Album><Album AlbumID="AL_0076" ArtistID="Ar_0003" CompanyID="C_6" GenreName="Metal">



        <Title>it's me</Title>
        <Price>5</Price>
        <Year>2012</Year>
    </Album><Album AlbumID="AL_0056" ArtistID="Ar_0003" CompanyID="C_9" GenreName="Metal">



        <Title>I can say</Title>
        <Price>5</Price>
        <Year>2010</Year>
    </Album><Album AlbumID="AL_0055" ArtistID="Ar_0003" CompanyID="C_9" GenreName="Metal">



        <Title>when that</Title>
        <Price>5</Price>
        <Year>201</Year>
    </Album><Album AlbumID="AL_0043" ArtistID="Ar_0003" CompanyID="C_8" GenreName="Metal">



        <Title>top of top </Title>
        <Price>5</Price>
        <Year>2013</Year>
    </Album><Album AlbumID="AL_0039" ArtistID="Ar_0005" CompanyID="C_7" GenreName=" HipHop R_and_B">



        <Title>shit happens</Title>
        <Price>4</Price>
        <Year>2011</Year>
    </Album>

    <Genre GenreName="Rap"/>
    <Genre GenreName="Country"/>
    <Genre GenreName="R_and_B"/>
    <Genre GenreName="HipHop"/>
    <Genre GenreName="House"/>
    <Genre GenreName="Pop"/>
    <Genre GenreName="Electro"/>
    <Genre GenreName="Blues"/>
    <Genre GenreName="Punck"/>
    <Genre GenreName="Rock"/>
    <Genre GenreName="Metal"/>
    <Genre GenreName="Alternative_Rock"/>

  <Company CompanyID="C_1">
   CBS Records
  </Company>
  <Company CompanyID="C_2">
  RCA
  </Company>
  <Company CompanyID="C_3">
    WEA   
  </Company>
  <Company CompanyID="C_4">
  Cloumbia
  </Company>
  <Company CompanyID="C_5">
   Virgin Records 
  </Company>
  <Company CompanyID="C_6">   
  Pickwick
  </Company>
  <Company CompanyID="C_7">
   Atlantic
  </Company>
  <Company CompanyID="C_8">
   Mega
  </Company>
  <Company CompanyID="C_9">
   Grammy
  </Company>
  <Company CompanyID="C_10">
   Wordo
  </Company>
  <Company CompanyID="C_11">
    Fox
  </Company>


  <Country countryID="US">
    United State  
  </Country>
  <Country countryID="UK">
    United Kingdom
  </Country>
  <Country countryID="FR">
    France
  </Country>
  <Country countryID="GR">
    Germany
  </Country>
  <Country countryID="ME">
    Mexico
  </Country>
  <Country countryID="SP">
    Spain
  </Country>
  <Country countryID="JP">
    Japneas
  </Country>

</MusicCatalog>
4

2 回答 2

1

选择器是区分大小写的,但即使你修正了拼写错误:@ALbumID=> @AlbumID,你仍然在迭代,而Year不是。因此,当您选择 时,它实际上将匹配当年的所有 s,并返回所有这些值的空格分隔字符串。Album//Album/Year[...]@AlbumID@AlbumID

但听起来您只想按年份对专辑进行分组。这应该是一个好的开始:

<Production_by_years>
{
    for $i in distinct-values(//Album/Year)
    return element {"Year"}
    {
        element year { $i },
        for $album in //Album[Year eq $i]
        return element Title {
          attribute id { $album/@AlbumID },
          $album/*
        }
    }
}
</Production_by_years>
于 2013-11-07T16:24:13.313 回答
1

您有一个错字:@ALbumID -> @AlbumID,但我建议您更改内部循环以使用专辑作为上下文:

for $album in //Album[Year = $i]
return (: something :)
于 2013-11-07T16:17:35.253 回答