xquery无法处理有效的xml实例

时间:2014-11-16 02:21:48

标签: xml xsd xquery xml-namespaces

这是我的company.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文件:

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>

如果我保留这样的所有三个文件,xquery将不起作用,但是,如果我删除company.xml中的名称空间声明部分,xquery将起作用。似乎xquery与命名空间有关。有人可以帮我解决这个问题吗?顺便说一句,xml文件是该xml架构的有效实例。我尝试了不同的方法,但没有一种方法有效。

1 个答案:

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