4

标题比它必须的要复杂,这里是问题查询。

SELECT * 
FROM query.multi 
WHERE queries="
    SELECT * 
        FROM html 
        WHERE url='http://www.stumbleupon.com/url/http://www.guildwars2.com' 
        AND xpath='//li[@class=\"listLi\"]/div[@class=\"views\"]/a/span';
    SELECT * 
        FROM xml 
        WHERE url='http://services.digg.com/1.0/endpoint?method=story.getAll&link=http://www.guildwars2.com';
    SELECT * 
        FROM json 
        WHERE url='http://api.tweetmeme.com/url_info.json?url=http://www.guildwars2.com';
    SELECT * 
        FROM xml 
        WHERE url='http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.guildwars2.com';
    SELECT * 
        FROM json 
        WHERE url='http://www.reddit.com/button_info.json?url=http://www.guildwars2.com'"

特别是这一行,

xpath='//li[@class=\"listLi\"]/div[@class=\"views\"]/a/span'

由于引用,这是有问题的,我必须将它们嵌套三层深,并且我已经用完了要使用的引用字符。我尝试了以下变体但没有成功:

//no attribute quoting
xpath='//li[@class=listLi]/div[@class=views]/a/span' 

//try to quote attribute w/ backslash & single quote
xpath='//li[@class=\'listLi\']/div[@class=\'views\']/a/span'

//try to quote attribute w/ backslash & double quote
xpath='//li[@class=\"listLi\"]/div[@class=\"views\"]/a/span'

//try to quote attribute with double single quotes, like SQL
xpath='//li[@class=''listLi'']/div[@class=''views'']/a/span'

//try to quote attribute with double double quotes, like SQL
xpath='//li[@class=""listLi""]/div[@class=""views""]/a/span'

//try to quote attribute with quote entities
xpath='//li[@class="listLi"]/div[@class="views"]/a/span'

//try to surround XPath with backslash & double quote
xpath=\"//li[@class='listLi']/div[@class='views']/a/span\"

//try to surround XPath with double double quote
xpath=""//li[@class='listLi']/div[@class='views']/a/span""

都没有成功。

我没有看到太多关于转义 XPath 字符串的内容,但我发现的所有内容似乎都是使用 concat 的变体(这无济于事,因为 ' 和 " 都不可用)或 html 实体。不使用属性的引号不会'不会抛出错误但会失败,因为它不是我需要的实际 XPath 字符串。

我在 YQL 文档中没有看到任何关于如何处理转义的内容。我知道这是多么的极端,但希望他们有某种逃避指南。

4

2 回答 2

4

您需要使用双反斜杠转义任何用于分隔 XPath 查询的字符......换句话说:

SELECT * FROM query.multi 
WHERE queries="
    SELECT * 
        FROM html 
        WHERE url='http://www.stumbleupon.com/url/http://www.guildwars2.com' 
        AND xpath='//li[@class=\\'listLi\\']/div[@class=\\'views\\']/a/span';
    SELECT * 
        FROM xml 
        WHERE url='http://services.digg.com/1.0/endpoint?method=story.getAll&link=http://www.guildwars2.com';
    SELECT * 
        FROM json 
        WHERE url='http://api.tweetmeme.com/url_info.json?url=http://www.guildwars2.com';
    SELECT * 
        FROM xml 
        WHERE url='http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.guildwars2.com';
    SELECT * 
        FROM json 
        WHERE url='http://www.reddit.com/button_info.json?url=http://www.guildwars2.com'"

在 YQL 控制台中试试这个

于 2010-05-28T21:54:52.263 回答
1

我想出了一个解决方案,它并没有真正回答我原来的问题,但确实解决了问题。

data.html.cssselect表将采用 CSS 选择器并将其解析为 XPath,避免令人讨厌的转义问题。

SELECT *
FROM query.multi 
    WHERE queries="
        SELECT * 
            FROM data.html.cssselect 
            WHERE url='http://www.stumbleupon.com/url/http://www.guildwars2.com' 
            AND css='li.listLi div.views a span';
        SELECT * 
            FROM xml 
            WHERE url='http://services.digg.com/1.0/endpoint?method=story.getAll&link=http://www.guildwars2.com';
        SELECT * 
            FROM json 
            WHERE url='http://api.tweetmeme.com/url_info.json?url=http://www.guildwars2.com';
        SELECT * 
            FROM xml 
            WHERE url='http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.guildwars2.com';
        SELECT * 
            FROM json 
            WHERE url='http://www.reddit.com/button_info.json?url=http://www.guildwars2.com'"
于 2010-05-28T21:54:28.830 回答