質問

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

私はそうするような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から来ているので、私はそれをほとんど制御できません。

私がやりたいのは検索です 続きを読む ドキュメントでは、親タグを削除します(この場合、 <font> そしてその <p> タグ)そして、それらをReadmoreリンクに置き換えます。

htmlagilitypackが私をそこの方法の一部にすると確信していますが、どこから始めればいいのかを把握しようとしています。

これまでのところ、私は使用しなければならないと確信しています htmlDoc.DocumentNode.SelectSingleNode(//p[text()="[READMORE]"]) か何か。私はXpathにあまり精通していません。

私の文書の場合、Readmoreはネストされている場合とそうでない場合があります font 鬼ごっこ。

また、場合によっては、タグではなく、ドキュメントルートにある場合があります。その場合、定期的な検索を行い、交換することができます。簡単にする必要があります。

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

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

node.Replace( "link here" );

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

私は知っています、私は夢を見ています...しかし、私はこれが理にかなっていることを願っています。

役に立ちましたか?

解決

これが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>

他のヒント

私が正しければ、あなたは一つのことを試すことができます...カスタムHTMLメールを送信する際に私たちがすることと同じこととして

  1. 静的な内容を持つHTMLページのテンプレートを作成します。
  2. Readmore]または{Readmore}、またはそれに似たものを述べたように、動的な内容の識別子を追加します。
  3. 次に、テンプレートHTMLファイルを行ごとに読み取り、識別子を目的のテキストに置き換えます。
  4. ここで、文字列全体を新しいHTMLファイルに保存するか、必要なことを何でも実行します。
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top