我有这个DTD
<?xml version="1.0" encoding="utf-8"?>
<!ELEMENT MusicCatalog (Artist*,Album*,Genre+,Company*,Country*)>
<!ELEMENT Artist (Name)>
<!ELEMENT Name (FirstName,MiddleName*,LastName?)>
<!ELEMENT FirstName (#PCDATA)>
<!ELEMENT MiddleName (#PCDATA)>
<!ELEMENT LastName (#PCDATA)>
<!ATTLIST Artist ArtistID ID #REQUIRED
countryID IDREF #REQUIRED>
<!ELEMENT Album (Title,Price,Year)>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Price (#PCDATA)>
<!ELEMENT Year (#PCDATA)>
<!ATTLIST Album AlbumID ID #REQUIRED
ArtistID IDREF #REQUIRED
CompanyID IDREF #REQUIRED
GenreName IDREFS #REQUIRED >
<!ELEMENT Company (#PCDATA)>
<!ATTLIST Company CompanyID ID #REQUIRED>
<!ELEMENT Genre EMPTY>
<!ATTLIST Genre GenreName ID #REQUIRED>
<!ELEMENT Country (#PCDATA)>
<!ATTLIST Country countryID ID #REQUIRED>
我有这个基本的XML
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE MusicCatalog SYSTEM "Refined_DTD_For_catalog.dtd">
<MusicCatalog>
<Artist ArtistID="Ar_0000" countryID="US">
<Name>
<FirstName>Katey </FirstName>
<LastName>Berry</LastName>
</Name>
</Artist>
<Artist ArtistID="Ar_0001" countryID="US">
<Name>
<FirstName>Justine </FirstName>
<LastName>Temrilke</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</Price>
<Year>2012</Year>
</Album>
<Album AlbumID="AL_0001" ArtistID="Ar_0000" CompanyID="C_1" GenreName="Pop HipHop R_and_B">
<Title>Confessions</Title>
<Price>9</Price>
<Year>2008</Year>
</Album>
<Album AlbumID="AL_0002" ArtistID="Ar_0000" CompanyID="C_10" GenreName="Pop HipHop R_and_B">
<Title>Roar</Title>
<Price>13</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</Price>
<Year>2010</Year>
</Album>
<Album AlbumID="AL_0004" ArtistID="Ar_0001" CompanyID="C_4" GenreName="HipHop R_and_B">
<Title>Future of sex</Title>
<Price>8</Price>
<Year>2007</Year>
</Album>
<Album AlbumID="AL_0005" ArtistID="Ar_0001" CompanyID="C_5" GenreName="HipHop">
<Title>Mirros</Title>
<Price>8</Price>
<Year>2013</Year>
</Album>
<Album AlbumID="AL_0006" ArtistID="Ar_0001" CompanyID="C_5" GenreName="Electro">
<Title>Holly Grail</Title>
<Price>9</Price>
<Year>2014</Year>
</Album>
<Album AlbumID="AL_0007" ArtistID="Ar_0001" CompanyID="C_6" GenreName="HipHop Electro">
<Title>Give it to me</Title>
<Price>5</Price>
<Year>2005</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>
我对Xquery的问题是我没有得到预期的结果,在这个查询中我试图获得公司名称,创建带有名称的元素并且独立它以获得所有专辑标题但我只得到了公司ID,我尝试用很多查询来解决,但大多数查询不起作用
for $dc in distinct-values( //Album/@CompanyID )
return element {string ($dc)} {
for $j in //Album[@CompanyID = $dc] return $j/Title
}
这是另一次尝试
for $i in distinct-values(//MusicCatalog/Album/@CompanyID)
return
if(compare($i,//MusicCatalog/Company/@CompanyID))
then "element {string(//MusicCatalog/Company)}"
{
for $j in //MusicCatalog/Album[@CompanyID eq $i] return $j/Title
}
}
我得到了这个
<c_3>
<title>Calfornia Girls</title>
<title>Roar/title>
</c_3>
虽然我想得到
<WEA>
<title>Calfornia Girls</title>
<title>Roar/title>
</WEA>
另一个问题是获取流派专辑,因为它是多余的IDrefs。
答案 0 :(得分:3)
如果您希望将公司名称作为元素名称,则不应传递ID,而应传递名称。即使用其他路径表达式搜索名称:
for $i in distinct-values(//MusicCatalog/Album/@CompanyID)
return
element {translate(normalize-space(//MusicCatalog/Company[@CompanyID eq $i]), " ", "_")}
{
for $j in //MusicCatalog/Album[@CompanyID eq $i]
return $j/Title
}
}