为什么这个查询没有返回数据(在XQuery,Java中)?

时间:2013-03-13 17:56:42

标签: java xpath xquery

这适用于Java中使用的XQuery。我的代码正在处理其他XML文件,但这次它没有返回所需的数据。错误的代码如下。这有什么问题?感谢。

String queryString =
                    "declare variable $docName as xs:string external;" + sep +
                    "      for $TRACK in doc($docName)/playlist/tracklist/track " +
                    "   return " +
                    " <track><title>{$TRACK/title/text()}</title>" +
                    " <location>{$TRACK/location/text()}</location></track>";

这是目标XML:

    <?xml version="1.0"?>
    -<playlist xmlns="http://xspf.org/ns/0/" version="1">
       -<trackList>-<track><location>http://radiotool.com/242.mp3</location><title>New                   York</title></track>
                   -<track><location>http://radiotool.com/243.mp3</location> <title>Chicago Repeater</title></track>
</trackList></playlist>

1 个答案:

答案 0 :(得分:1)

这可能是因为源XML使用命名空间而XPath没有。怎么样:

String queryString =
                "declare namespace xsp='http://xspf.org/ns/0/'; " + 
                "declare variable $docName as xs:string external;" + sep +
                "  for $TRACK in doc($docName)/xsp:playlist/xsp:trackList/xsp:track " +
                "   return " +
                " <track><title>{$TRACK/xsp:title/text()}</title>" +
                " <location>{$TRACK/xsp:location/text()}</location></track>";