首页 > 解决方案 > xQuery 使用命名空间的数据?

问题描述

菜鸟可能会向有经验的人寻求帮助,请:如何从包含名称空间的 XML 文件(示例中的“gsx:onoff”)中列出/测试一个值,例如字幕或 desc。

我的 xQuery 没有返回结果?无论像我这样的新手尝试什么 - 回报都不算什么。

尝试了数百万种查询变体,例如:

    declare namespace gs="http://www.w3.org/2005/Atom";
    declare namespace gsx="http://www.w3.org/2005/Atom";
    let $count := count(//gs:entry/onoff)
    for $x in //gs:entry
    let $onoff := $x/gsx:onoff
    return <datarow><icon> {data($x//gs:entry/gsx:onoff)} </icon> <hasdata>{ if ($count > 1) then 1 else 0 }</hasdata><whatdata>{ if ($onoff = "yes") then 1 else data($x/gsx:onoff) }</whatdata> </datarow>
    //gs:entry/gsx:onoff

XML 来源:链接

检查解析的 XML 源

<?xml version='1.0' encoding='UTF-8'?>
<feed
    xmlns='http://www.w3.org/2005/Atom'
    xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'
    xmlns:gsx='http://schemas.google.com/spreadsheets/2006/extended'>
    <id>https://spreadsheets.google.com/feeds/list/12ZuLAwWcuNmWWiS5bP0oThWjs66emSrJpw2LFly8bPg/1/public/values</id>
    <updated>2020-12-15T17:13:14.537Z</updated>
<entry>
        <id>https://spreadsheets.google.com/feeds/list/12ZuLAwWcuNmWWiS5bP0oThWjs66emSrJpw2LFly8bPg/1/public/values/cokwr</id>
        <updated>2020-12-15T17:13:14.537Z</updated>
        <category scheme='http://schemas.google.com/spreadsheets/2006' term='http://schemas.google.com/spreadsheets/2006#list'/>
        <title type='text'>Row: 2</title>
        <content type='text'>subtitle: Welcome to the, titles: 2020 CXL Experts’ Meeting, footer: Friday, December 18, 09:45–21:00 (CET), Saturday, December 19, 09:00–15:00 (CET), sponsor: blank.png, allsponsors: all-HALF.png, poruka: Welcome to the CXL Experts’ Meeting 2020.    Day 1 starts at 09:40 and ends at 21:00 (CET) and Day 2 starts at 09:00 and ends at 15:00 (CET).    Scheduled breaks (CET) are on Day 1: 13:00–13:45 and 16:45–17:00 and on Day 2, these are 10:20–10:30, 11:50–12:20 and 13:30–13:50.    Learn more about the meeting at www.cxlexpertsmeeting.com and the Light for Sight foundation at www.lightforsight.org.</content>
        <link rel='self' type='application/atom+xml' href='https://spreadsheets.google.com/feeds/list/12ZuLAwWcuNmWWiS5bP0oThWjs66emSrJpw2LFly8bPg/1/public/values/cokwr'/>
        <gsx:onoff>yes</gsx:onoff>
        <gsx:subtitle>Welcome to the</gsx:subtitle>
        <gsx:titles>2020 CXL Experts’ Meeting</gsx:titles>
        <gsx:desc></gsx:desc>
        <gsx:footer>Friday, December 18, 09:45–21:00 (CET), Saturday, December 19, 09:00–15:00 (CET)</gsx:footer>
        <gsx:sponsor>blank.png</gsx:sponsor>
</entry>

标签: xmlnamespacesxqueryxml-namespaces

解决方案


让我们看看这是否是您正在寻找的:

declare namespace gsx="http://schemas.google.com/spreadsheets/2006/extended";
declare namespace gs="http://www.w3.org/2005/Atom";
let $entries := //gs:entry
 
 for $entry in $entries
    let $onoff := $entry/gsx:onoff,
    $count := count($onoff)
return <datarow><icon> {data($entries//gs:entry/gsx:onoff)} </icon> <hasdata>{ if ($count > 1) then 1 else 0 }</hasdata><whatdata>{ if ($onoff = "yes") then 1 else data($entries/gsx:onoff) }</whatdata> </datarow>

编辑:

针对您的评论(我取出<icon>节点以简化):

let $entries := //gs:entry[.//gsx:onoff[text()="yes"]] 
 for $entry in $entries
    let $onoff := $entry/gsx:onoff[text()="yes"]    
return <datarow><hasdata>1</hasdata><whatdata>{$entry}</whatdata> </datarow>

推荐阅读