2012-01-26 77 views
2

我應該編寫一個基於XQuery的SQL腳本來將來自3個不同XML文件的數據合併爲一個統一格式 。 輸出應該是以下格式:XQuery - 需要幫助

<Courses> 
<Course school="NYU"> 
<Number>30144</Number> 
<Title>C‐PAC II</Title> 
<Instructor>Lewis</Instructor> 
</Course> 
... 
<Course school="Harvard"> 
<Number>4949</Number> 
<Title>Computer Science 50. Introduction to Computer Science I</Title> 
<Instructor>Michael D. Smith</Instructor> 
</Course> 
... 
</Courses> 

我寫了下面的腳本來實現上述:

SELECT catalog.query(' 
<Courses> 
{ 
(<Course school = "NYU"> 
{ 
for $x in (/nyu/Course) 
return 
<Number> {$x/CallNo/text()} </Number> 
<Title> {$x/Name/text()} </Title> 
<Instructor> {$x/Instructor/text()} </Instructor> 
} 
</Course>) 

union  

(<Course school = "Harvard"> 
{ 
for $y in (/harvard/Course) 
return 
<Number> {$y/Number/text()} </Number> 
<Title> {$y/Title/text()} </Title> 
<Instructor> {$y/Instructor/text()} </Instructor> 
}    
</Course>) 

union 

(<Course scool = "Umich"> 
{ 
for $z in (/umich/Course) 
return 
<Number> {$z/@catalognumber} </Number> 
<Title> {$z/name/text()} </Title> 
} 
</Course>) 
}      
</Courses> 
')  
from catalogs 

誰能請告訴我在哪裏我都去錯了嗎?

回答

1

XQuery operators使用節點序列,而不是文件。語法是這樣的:

document("umich.xml")/umich/Course UNION document("harvard.xml")/harvard/Course UNION document("nyu.xml")/nyu/Course 

使用document function從文件中返回節點,並union operator創建結果集。