【问题标题】:xquery beginner- how to use an if-then-else within a FLWOR statementxquery 初学者 - 如何在 FLWOR 语句中使用 if-then-else
【发布时间】:2012-08-06 15:36:28
【问题描述】:

我想使用 XQuery 从网页的 XML 等效项中提取一些内容。

在这里,我想使用 if-then-else -- 如果满足特定条件,则 xquery 表达式返回一个值,否则返回另一个值。

这是我的表情--

我正在从网页中提取一些内容。

下面给出的是我正在使用的 Xquery 代码——我正在使用 XQuery 库来解析 XML,该 XML 是通过将 HTML 网页转换为 XML 获得的...之后我从该 XML 页面中提取一些特定内容...

                    declare variable $doc as node() external;

                    let $select_block := $doc//div[@class="randomclass" and contains(.,'MatchingText')]
                    for $select_link in $select_block/a 
                    for $select_link_url in $select_link/@href
                        where  contains($assignee_link_url,'inassignee')  
                     return data($select_link)

现在如何在这个表达式中使用 if-then-else?

我尝试在“return”关键字后立即添加一个 if-then,但出现错误... 基本上,如果上面的$select_block找到了一些内容,那么应该返回data($select_link),否则应该返回静态文本'Missing Value'。

在上述 xquery 表达式中使用 if-then-else 的正确方法是什么?

【问题讨论】:

    标签: xquery flwor


    【解决方案1】:

    如果我理解您要正确实现的目标,那么以下内容应该有效:

    declare variable $doc as node() external;
    let $select_block := $doc//div[@class="randomclass" and contains(.,'MatchingText')]
    return 
        if ($select_block) then
         for $select_link in $select_block/a 
            for $select_link_url in $select_link/@href
                where  contains($select_link_url,'inassignee') 
                    return data($select_link)
        else 'Missing Value'
    

    您可以稍微简化一下,并通过选择锚元素的谓词过滤器消除嵌套的 for 循环:

    declare variable $doc as node() external; 
    let $select_block := $doc//div[@class="randomclass" and contains(.,'MatchingText')]
    return 
        if ($select_block) then
         for $select_link in $select_block/a[@href[contains(., 'inassignee')]]
             return data($select_link)
        else 'Missing Value'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多