Frage

Ich frage mich, ob dies möglich ist.

Ich habe html wie folgt:

<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>

Ja, es ist hässlich html und es kommt von einem WYSIWYG so dass ich wenig Kontrolle darüber haben.

Was soll ich tun Suche nach [READMORE] in dem Dokument, entfernen Sie alle übergeordneten Tags (in diesem Fall die <font> und die <p> Tags) und ersetzen sie durch eine readmore Link während Verpackung der Rest des Dokuments in einem riesigen `... der Rest des Artikels ...

Ich bin ziemlich sicher, dass der HtmlAgilityPack wird mich dort einen Teil des Weges, aber ich versuche nur herauszufinden, wo zu beginnen.

Bisher bin ich ziemlich sicher, dass ich Gebrauch htmlDoc.DocumentNode.SelectSingleNode(//p[text()="[READMORE]"]) oder etwas haben. Ich bin nicht allzu vertraut mit XPATH.

Für meine Dokumente kann der readmore oder nicht in einem verschachtelten font-Tag sein.

Auch in einigen Fällen kann es gar nicht in einem Tag, sondern eher an dem Dokumenten-Root. Ich kann nur eine regelmäßige Suche tun und in diesem Fall ersetzen und es sollte einfach sein.

Meine ideale Situation so etwas wie dieser (Pseudo-Code) sein würde

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

node.Replace( "link here" );

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

Ich weiß, ich träume ... aber ich hoffe, das macht Sinn.

War es hilfreich?

Lösung

Hier ist eine XSLT-Lösung :

<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>

, wenn diese Transformation auf das folgende XML-Dokument angewendet wird, :

<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>

das gewünschte Ergebnis erzeugt :

<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>

Andere Tipps

Wenn ich rechts, dann bin, kann Sie eine Sache, versuchen ... als die gleiche Sache, die wir beim Senden von eigenen HTML-Mails

tun
  1. Erstellen Sie eine Vorlage Ihrer HTML-Seite mit statischen Inhalten.
  2. Anfügen Identifikatoren für dynamische Inhalte, wie Sie haben erklärt, [Weiter lesen] oder {} readmore oder etwas ähnlich.
  3. Geben Sie nun die Vorlage HTML-Datei Zeile für Zeile lesen und die Kennungen mit dem gewünschten Text ersetzen.
  4. Speichern Sie nun die gesamte Zeichenfolge in eine neue HTML-Datei oder tun, was Sie wollen.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top