0

これが可能かどうか疑問に思っています。

私は次のようなhtmlを持っています:

<p>
  <font face="Georgia">
    <b>History</b><br>&nbsp; <br>Two of the polysaccharides used in the manufacture of...</font>
    <a title="PubMed" href="http://www.www.gov/pubmed/" target="_blank">
    <font face="Georgia">) and this web site for new development by...well as Self Affirmed Medical Food GRAS status.&nbsp; 
    </font>
</p>

<p>
  <font face="Georgia">[READMORE]</font>
</p>

<p><font face="Georgia"><br><strong>Proprietary Composition</strong><br>
   <br>The method in which soluble fibres are made into... REST OF ARTICLE...
</p>

はい、それは醜い html であり、WYSIWYG から来ているので、私はそれをほとんど制御できません。

私がやりたいことは、ドキュメント内で[READMORE]を検索し、親タグ (この場合は<font><p>タグ) を削除し、ドキュメントの REST を巨大な `... でラップしながら readmore リンクに置き換えます。残りの記事...

HtmlAgilityPack がその道のりの一部になると確信していますが、どこから始めればよいかを考えているところです。

htmlDoc.DocumentNode.SelectSingleNode(//p[text()="[READMORE]"])これまでのところ、何かを使用する必要があると確信しています。私は XPATH にあまり詳しくありません。

font私のドキュメントでは、readmore がネストされたタグにある場合とない場合があります。

また、場合によっては、タグではなく、ドキュメント ルートにあることもあります。その場合、通常の検索と置換を行うだけで、簡単なはずです。

私の理想的な状況は次のようなものです (PSEUDOCODE)

var node = SelectNodeContaining("[READMORE]").

node.Replace( "link here" );

node.RestOfDocument().Wrap("<div class='wrapper'");

夢を見ているのはわかっています...でも、これが理にかなっているといいのですが。

4

2 に答える 2

3

XSLT ソリューションは次のとおりです。

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

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="p[descendant::text()[. = '[READMORE]']]">
  <a href="#ReadmoreWrapper">READMORE</a>
  <div class="wrapper" id="#ReadmoreWrapper">
   <xsl:apply-templates select="following-sibling::node()" mode="copy"/>
  </div>
 </xsl:template>

 <xsl:template match=
  "node()[ancestor::p[descendant::text()[. = '[READMORE]']]
         or
          preceding::p[descendant::text()[. = '[READMORE]']]
          ]
  "/>

  <xsl:template match="node()|@*" mode="copy">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*" mode="copy"/>
      </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

この変換が次の XML ドキュメントに適用された場合:

<html>
<p>
  <font face="Georgia">
    <b>History</b><br/>&#xA0; <br/>Two of the polysaccharides used in the manufacture of...</font>
    <a title="PubMed" href="http://www.www.gov/pubmed/" target="_blank"/>
    <font face="Georgia">) and this web site for new development by...well as Self Affirmed Medical Food GRAS status.&#xA0;
    </font>
</p>

<p>
  <font face="Georgia">[READMORE]</font>
</p>

<p><font face="Georgia"><br/><strong>Proprietary Composition</strong><br/>
   <br/>The method in which soluble fibres are made into... REST OF ARTICLE...
   </font>
</p>

</html>

必要な結果が生成されます:

<html>
    <p>
        <font face="Georgia"><b>History</b><br/>  <br/>Two of the polysaccharides used in the manufacture of...</font>
        <a title="PubMed" href="http://www.www.gov/pubmed/" target="_blank"/>
        <font face="Georgia">) and this web site for new development by...well as Self Affirmed Medical Food GRAS status. 
    </font>
    </p>
    <a href="#ReadmoreWrapper">READMORE</a>
    <div class="wrapper" id="#ReadmoreWrapper">
        <p>
            <font face="Georgia"><br/><strong>Proprietary Composition</strong><br/><br/>The method in which soluble fibres are made into... REST OF ARTICLE...
   </font>
        </p>
    </div>
</html>
于 2010-08-19T17:22:33.380 に答える
0

私が正しければ、1 つのことを試すことができます... カスタム HTML メールを送信するのと同じことです。

  1. 静的コンテンツを含む HTML ページのテンプレートを作成します。
  2. [ReadMore] または {ReadmOre} またはそれに類似したものを述べたように、動的コンテンツの識別子を追加します。
  3. テンプレートの html ファイルを 1 行ずつ読み、識別子を目的のテキストに置き換えます。
  4. 文字列全体を新しい html ファイルに保存するか、好きなようにします。
于 2010-08-19T04:43:17.277 に答える