2009-03-19 15 views
10

YQLが提供するYahoo Query Languageとxpath機能を使ってhtmlを解析しようとしているときに、「text()」や属性値を抽出できないという問題がありました。

perma linkYahoo YQLを使ってhtmlを照会

select * from html where url="http://stackoverflow.com" 
and xpath='//div/h3/a' 

私は

select * from html where url="http://stackoverflow.com" 
and xpath='//div/h3/a/text()' 

を使用してノードの値を抽出しようとすると、今、私は結果が連結ではなく、取得したXML

<results> 
    <a class="question-hyperlink" href="https://stackoverflow.com/questions/661184/filling-the-text-area-with-the-text-when-a-button-is-clicked" title="In ASP.net, I need the code to fill the text area (in the form) when a button is clicked. Can you help me through by showing a simple .aspx code containing the script tag? ">Filling the text area with the text when a button is clicked</a>... 
</results> 

としてアンカーのリストを与えますノードリスト

<results>Xcode: attaching to a remote process for debuggingWhy is b 
…… </results> 

どのようにしてノードリストに分離んし、どのように私は属性が値選択のですか?

この

select * from html where url="http://stackoverflow.com" 
and xpath='//div/h3/a[@href]' 

のようなクエリがdiv/h3/a

答えて

20

YQLを照会するための私と同じ結果が得られたがitemPathではなく、ノードのテキストに評価するXPath式が必要です。しかし、一度itemPathを設定すると、ツリーからさまざまな値を投影できます

つまり、ItemPathは、テキストコンテンツ/属性ではなく結果のHTMLでノードを指す必要があります。 YQLは、データから*を選択すると、一致するすべてのノードとその子を返します。

select * from html where url="http://stackoverflow.com" and xpath='//div/h3/a' 

これは、すべてのAのXPathのマッチングを返します。テキストコンテンツを投影するには

select content from html where url="http://stackoverflow.com" and xpath='//div/h3/a' 

"content"はノード内に保持されているテキストコンテンツを返します。

属性を投影する場合は、xpath式を基準に属性を指定できます。この場合、aとの相対的なhrefが必要なためです。

select href, content from html where url="http://stackoverflow.com" and xpath='//div/h3/a' 

リターン:

<results> <a href="https://stackoverflow.com/questions/663950/double-pointer-const-issue-issue">double pointer const issue issue</a>... </results> 
あなたは属性 'のhref' とのTextContentの両方を必要に応じて

select href from html where url="http://stackoverflow.com" and xpath='//div/h3/a' 

これは <results> <a href="https://stackoverflow.com/questions/663973/putting-a-background-pictures-with-leds"/> <a href="https://stackoverflow.com/questions/663013/advantages-and-disadvantages-of-popular-high-level-languages"/> .... </results>

を返し、その後、次のようなYQLクエリーを実行することができます

希望は役立ちます。もしあなたがYQLに関する質問があれば教えてください。

+0

魅力的な作品です! – Cherian

関連する問題