我想知道这是否可行。
我有这样的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>
<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,它来自一个所见即所得,所以我几乎无法控制它。
我想要做的是在文档中搜索 [READMORE] ,删除所有父标记(在本例中为<font>
和<p>
标记)和用一个readmore链接替换它们,同时将文档的REST包装在一个巨大的......文章的其余部分......
我很确定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'");
我知道,我在做梦...但我希望这是有道理的。
答案 0 :(得分: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/>  <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>
<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>
答案 1 :(得分:0)
如果我是对的,你可以尝试一件事......就像我们在发送自定义HTML邮件时所做的一样