2014-11-16 54 views
0

這是我company.xml文件,:的XQuery不工作的一個有效的XML實例

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="company.xsl"?> 
<Company xsi:schemaLocation="http://localhost company.xsd" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://localhost"> 
<!--the Department Part--> 
<Department> 
    <deptId>ADM</deptId> 
    <deptName>Admin</deptName> 
    <deptAddr>LA</deptAddr> 
</Department> 

<Department> 
    <deptId>FIN</deptId> 
    <deptName>Finance</deptName> 
    <deptAddr>LA</deptAddr> 
</Department> 
</Company> 

這是我company.xsd文件

<?xml version="1.0"?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://localhost" 
    xmlns="http://localhost" 
    elementFormDefault="qualified"> 
......... 
</xsd:schema> 

這是我q1.xquery file:

xquery version "1.0"; 
<query1> 
{ 
for $x in doc("company.xml")//Company//Department 
return <result><deptId>{data($x//deptId)}</deptId><deptName>{data($x//deptName)}</deptName> </result> 

} 
</query1> 

如果我保留所有這三個文件,xquer Ÿ不會工作,但是,如果我刪除company.xml中的名稱空間聲明部分,xquery將工作。看來xquery與命名空間有關。有人可以幫我解決這個問題嗎?順便說一下,xml文件是該XML模式的有效實例。我嘗試過不同的方法,但沒有一個能夠正常工作。

回答

3

似乎xquery與命名空間有關。

是的。爲了匹配命名空間節點需要聲明的命名空間中查詢

xquery version "1.0"; 
declare namespace co = "http://localhost"; 
<query1> 
{ 
for $x in 
doc("company.xml")//co:Company//co:Department 
return ..... 

如果您有沒有必要指非命名空間的元素在您的查詢,然後代替聲明前綴,就可以

declare default element namespace "http://localhost"; 
+0

It works !!!!!!非常感謝你! – user3088554