2017-03-10 12 views
1

XSLTを使用してrssフィードから取得したデータをクリーンアップしようとしています.pタグ以外のすべてのタグを削除します。XSLT関数を使用して許可タグ以外のすべてのhtmlタグを削除します

Cows are kool.<p>The <i>milk</i> <b>costs</b> $1.99.</p> 

ここでは、1.0または2.0のいずれかでXSLTを使用してこれを解決する方法に疑問があります。

1)私はhttps://maulikdhorajia.blogspot.in/2011/06/removing-html-tags-using-xslt.html

この例を見てきました。しかし、私が存在することのpタグを必要としているため、私は同様の方法で、我々は文字列の前にマッチ機能を使用regex.Canを使用して行う必要がありますこの機能はxpathにはないと思います。

2)これは文字列を期待しているので置換機能を使用できないことを理解しています。ノードを渡すと内容を抽出して関数に渡します。この場合、タグを削除する目的はありません。

私はこの答えのように少し混乱していましたが、置き換えはhttps://stackoverflow.com/a/18528749/745018でした。

3)xsltを使用してnginxサーバーでこれを実行しています。

RSSフィードのbodyタグに入力するサンプルを以下に示します。

<p>The Supreme Court issued on Friday a bailable warrant against sitting Calcutta high court justice CS Karnan, an unprecedented order in a bitter confrontation between the judge and the top court.</p><p>A seven-judge bench headed by Chief Justice of India JS Khehar issued the order directing Karnan’s presence on <h2>March 31</h2> because the judge ignored an earlier court order summoning him.<i>Justice Karnan</i> had to appear</p> 

アップデート:また、私はあなたが、あなたはbody要素の内容にデビッド・カーライルのHTMLパーサ(https://github.com/davidcarlisle/web-xslt/blob/master/htmlparse/htmlparse.xsl)を適用し、処理できるXSLT 2.0を使用することができると仮定すると、この

+2

最小限で完全なXML入力のサンプルと、それに対応する結果をご記入ください。 RSSフィード内のHTMLがマークアップやテキスト(CDATAセクションの内側)として含まれているかどうかを確認する必要があります。また、HTMLをXMLとして、またはHTMLとしてのみ解析できるかどうかを知る必要があります。 –

+0

@MartinHonnenサンプル入力を更新しました.pタグ以外のHTMLタグなしで返されるcdata内のコンテンツが必要です。 – crackerplace

答えて

4

のためのXSLT関数を探しています入力

について

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
    xmlns:d="data:,dpc" 
    xmlns:xhtml="http://www.w3.org/1999/xhtml" 
    exclude-result-prefixes="d xhtml"> 

    <xsl:import href="htmlparse-by-dcarlisle.xsl"/> 

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

    <xsl:template match="body"> 
     <xsl:copy> 
      <xsl:apply-templates select="d:htmlparse(., '', true())" mode="strip"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="*[not(self::p)]" mode="strip"> 
     <xsl:apply-templates/> 
    </xsl:template> 

</xsl:transform> 

:すべての要素が、p要素を取り除きモードで得られたノード

:入力は、あなたがそれを解析する必要はありませんが内容だけにモードを適用することができます逃れではなく、入力におけるXMLとして含まれていない場合

<rss> 
    <entry> 
     <body><p>The Supreme Court issued on Friday a bailable warrant against sitting Calcutta high court justice CS Karnan, an unprecedented order in a bitter confrontation between the judge and the top court.</p><p>A seven-judge bench headed by Chief Justice of India JS Khehar issued the order directing Karnan’s presence on March 31 because the judge ignored an earlier court order summoning him.Justice Karnan had to appear</p></body> 
    </entry> 
</rss> 

を与える

<rss> 
    <entry> 
     <body><![CDATA[<p>The Supreme Court issued on Friday a bailable warrant against sitting Calcutta high court justice CS Karnan, an unprecedented order in a bitter confrontation between the judge and the top court.</p><p>A seven-judge bench headed by Chief Justice of India JS Khehar issued the order directing Karnan’s presence on <h2>March 31</h2> because the judge ignored an earlier court order summoning him.<i>Justice Karnan</i> had to appear</p>]]></body> 
    </entry> 
</rss> 

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 

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

    <xsl:template match="body"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()" mode="strip"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="*[not(self::p)]" mode="strip"> 
     <xsl:apply-templates/> 
    </xsl:template> 

</xsl:transform> 

http://xsltransform.net/gWEamMc/1

+0

thanks.Assume htmlはすでに解析済みです。他のタグを取り除くために働きます。 。 – crackerplace

+0

http://xsltransform.net/ejivdJaはうまく動作します。あなたの質問を編集し、XML、XSLT、必要な出力、必要な場合は正確なエラーメッセージの最小限の完全なサンプルを提供する必要があります助けて。 –

+0

はい、それは作品です。ちょうどそれがこの入力http://xsltransform.net/gWEamMcのために働かせる方法を見るために。質問が更新されました。 – crackerplace

関連する問題