2013-07-03 65 views
0

這個查詢:YQL列投影使用XPATH

SELECT * 
FROM html 
WHERE url='http://wwww.example.com' 
AND xpath='//tr[@height="20"]' 

返回XML:

<results> 
    <tr height="20"> 
     <td height="20" width="425"> 
      <p>Institution 0</p> 
     </td> 
     <td width="134"> 
      <p>Minneapolis</p> 
     </td> 
     <td width="64"> 
      <p>MN</p> 
     </td> 
    </tr> 
    ... 
</results> 

問題:

  • 是否有使用XPath創建單獨列的方式嗎?
  • 有沒有辦法創建列別名?

例(無效的語法):

SELECT td[position()=1]/p/. AS name, td[position()=2]/p/. AS city, td[position()=3]/p/. AS region 
FROM ... 

目標:

<results> 
    <tr height="20"> 
     <name>Institution 0</name> 
     <city>Minneapolis</city> 
     <region>MN</region> 
    </tr> 
    ... 
</results> 

回答

1

不使用XPath,因爲你正在嘗試做的。然而,可以使用YQL將XSL Transformations應用於XML/HTML文檔。這裏有一個例子:

XSLT

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="/"> 
     <rows> 
      <xsl:apply-templates select="descendant::tr" /> 
     </rows> 
    </xsl:template> 
    <xsl:template match="//tr"> 
     <row> 
      <name> 
       <xsl:value-of select="td[1]/p" /> 
      </name> 
      <city> 
       <xsl:value-of select="td[2]/p" /> 
      </city> 
      <region> 
       <xsl:value-of select="td[3]/p" /> 
      </region> 
     </row> 
    </xsl:template> 
</xsl:stylesheet> 

HTML

<html> 
    <body> 
     <table> 
      <tr height="20"> 
       <td height="20" width="425"> 
        <p>Institution 0</p> 
       </td> 
       <td width="134"> 
        <p>Minneapolis</p> 
       </td> 
       <td width="64"> 
        <p>MN</p> 
       </td> 
      </tr> 
      <tr height="20"> 
       <td height="20" width="425"> 
        <p>Institution 1111</p> 
       </td> 
       <td width="134"> 
        <p>Minneapolis 1111</p> 
       </td> 
       <td width="64"> 
        <p>MN 11111</p> 
       </td> 
      </tr> 
     </table> 
    </body> 
</html> 

YQL查詢

select * from xslt where stylesheet="url/to.xsl" and url="url/to.html" 

YQL結果

<results> 
    <rows> 
     <row> 
      <name>Institution 0</name> 
      <city>Minneapolis</city> 
      <region>MN</region> 
     </row> 
     <row> 
      <name>Institution 1111</name> 
      <city>Minneapolis 1111</city> 
      <region>MN 11111</region> 
     </row> 
    </rows> 
</results> 

»See an example running in the YQL console.

+0

好主意!我希望我在文檔中看到了這一點。也有一個upvote。 – craig

+0

我是否也應該能夠在YQL查詢中添加一個'xpath'過濾器?例如'AND xpath ='// tr [@ height =「20」]''? – craig

+0

「xslt」表沒有'xpath'參數。 – salathe