1

HTML コードを含む文字列 (@description) があり、2 つの要素間のコンテンツを抽出したいと考えています。こんな感じです

<b>Content title<b><br/>
*All the content I want to extract*
<a href="javascript:print()">

私はこのようなことをすることができました

@want = @description.match(/Content title(.*?)javascript:print()/m)[1].strip

しかし、明らかに、@want 文字列に不要な文字が含まれているため、このソリューションは完全にはほど遠いものです。

ご協力いただきありがとうございます

編集:

コメントで要求されているように、完全なコードは次のとおりです。

私はすでに次のコードで何かをしている HTML ドキュメントを解析しています:

@description = @doc.at_css(".entry-content").to_s
puts @description

戻り値:

<div class="post-body entry-content">
<a href="http://www.photourl"><img alt="Photo title" height="333"     src="http://photourl.com" width="500"></a><br><br><div style="text-align: justify;">
Some text</div>
<b>More text</b><br><b>More text</b><br><br><ul>
<li>Numered item</li>
<li>Numered item</li>
<li>Numered item</li>
</ul>
<br><b>Content Title</b><br>
Some text<br><br>
Some text(with links and images)<br>
Some text(with links and images)<br>
Some text(with links and images)<br>
<br><br><a href="javascript:print()"><img src="http://url.com/photo.jpg"></a>
<div style="clear: both;"></div>
</div>

テキストには、さらに段落、リンク、画像などを含めることができますが、常に「コンテンツ タイトル」部分で始まり、javascript 参照で終わります。

4

2 に答える 2

1

$vStartこの XPath 式は、ノードとの間のすべての (兄弟) ノードを選択します$vEnd

  $vStart/following-sibling::node()
           [count(.|$vEnd/preceding-sibling::node())
           =
            count($vEnd/preceding-sibling::node())
           ]

特定のケースで使用する完全な XPath 式を取得するには、次のように置き換えます$vStart

/*/b[. = 'Content Title']

次のように置き換え$vEndます。

/*/a[@href = 'javascript:print()']

置換後の最終的な XPath 式は次のとおりです。

/*/b[. = 'Content Title']/following-sibling::node()
         [count(.|/*/a[@href = 'javascript:print()']/preceding-sibling::node())
         =
          count(/*/a[@href = 'javascript:print()']/preceding-sibling::node())
         ]

説明:

$ns1これは、2 つのノードセットとの交点に対する Kayess の式の単純な帰結です$ns2

$ns1[count(.|$ns2) = count($ns2)]

この場合、ノード$vStartとの間のすべてのノードのセットは、 の後続のすべての兄弟と の先行するすべて$vEndの兄弟の 2 つのノードセットの共通部分です。$vStart$vEnd

XSLT ベースの検証:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vStart" select="/*/b[. = 'Content Title']"/>
 <xsl:variable name="vEnd" select="/*/a[@href = 'javascript:print()']"/>

 <xsl:template match="/">
     <xsl:copy-of select=
     "$vStart/following-sibling::node()
               [count(.|$vEnd/preceding-sibling::node())
               =
                count($vEnd/preceding-sibling::node())
               ]
     "/>
==============

     <xsl:copy-of select=
     "/*/b[. = 'Content Title']/following-sibling::node()
               [count(.|/*/a[@href = 'javascript:print()']/preceding-sibling::node())
               =
                count(/*/a[@href = 'javascript:print()']/preceding-sibling::node())
               ]
     "/>
 </xsl:template>
</xsl:stylesheet>

この変換が提供された XML ドキュメントに適用されると(整形式の XML ドキュメントに変換されます):

<div class="post-body entry-content">
    <a href="http://www.photourl">
        <img alt="Photo title" height="333"     src="http://photourl.com" width="500"/>
    </a>
    <br />
    <br />
    <div style="text-align: justify;">
    Some text</div>
    <b>More text</b>
    <br />
    <b>More text</b>
    <br />
    <br />
    <ul>
        <li>Numered item</li>
        <li>Numered item</li>
        <li>Numered item</li>
    </ul>
    <br />
    <b>Content Title</b>
    <br />
    Some text
    <br />
    <br />
    Some text(with links and images)
    <br />
    Some text(with links and images)
    <br />
    Some text(with links and images)
    <br />
    <br />
    <br />
    <a href="javascript:print()">
        <img src="http://url.com/photo.jpg"/>
    </a>
    <div style="clear: both;"></div>
</div>

2 つの XPath 式 (変数参照がある場合とない場合) が評価され、それぞれの場合に選択されたノードが適切に区切られて出力にコピーされます

<br/>
    Some text
    <br/>
<br/>
    Some text(with links and images)
    <br/>
    Some text(with links and images)
    <br/>
    Some text(with links and images)
    <br/>
<br/>
<br/>
==============

     <br/>
    Some text
    <br/>
<br/>
    Some text(with links and images)
    <br/>
    Some text(with links and images)
    <br/>
    Some text(with links and images)
    <br/>
<br/>
<br/>
于 2012-10-01T13:16:16.517 に答える
0

HTML をテストするために、コードの周りにタグを追加し、ファイルに貼り付けました

xmllint --html --xpath '/html/body/div/text()' /tmp/l.html

出力:

Some text
Some text
Some text
Some text

Xpath モジュールをruby使用して、Xpath 式を再利用できるようになりました。

stackoverflow の Web サイト検索で多くの例を見つけることができます。

于 2012-10-01T03:48:14.927 に答える