1

受け取ったファイルは、次のように誤って生成されています。

<html>
    <body>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
            <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
                <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
            </p>
        </p>
    </body>
</html>

<p>要素は前のノードに埋め込まれています<p>。代わりに、次のようになります。

<html>
    <body>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
        <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
        <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
    </body>
</html>

ドキュメントを送信しているアプリケーションを制御することはできません。XSL を使用してこのドキュメントを変換し、代わりに子ノード(およびそのコンテンツ)のみが兄弟としてレンダリングされるようにするにはどうすればよいでしょうか?

4

2 に答える 2

2

この方法で不正に生成された唯一の要素が p である場合、最初にすべての属性と非 p の子に対して apply-templates を呼び出し、次に埋め込まれた p 要素にテンプレートを適用する p のテンプレートを作成する必要があります。XSLT 2.0 構文では:

<xsl:template match="p">
  <p><xsl:apply-templates select="node() except p"/></p>
  <xsl:apply-templates select="p"/>
</xsl:template>

スタイルシートの残りの部分は恒等変換を実行する必要があります。

他の要素も入力で不適切に自己ネストする場合は、それらを同様に処理する必要があります。

2.0 の代わりに XSLT 1.0 を使用している場合は、p の内部に属するものとその後に発生するものを区別する別の方法を見つける必要がありnode() except pます。私は自分でモードを使用します:

<xsl:template match="p">
  <p><xsl:apply-templates mode="para-contents"/></p>
  <xsl:apply-templates select="p"/>
</xsl:template>

<xsl:template match="node()" mode="para-contents">
  <xsl:apply-templates select="."/>
</xsl:template>
<xsl:template match="p" mode="para-contents"/>

node() except pまたは (Ian Roberts がコメントで提案したように) に置き換えるだけnode()[not(self::p)]です。

これは、入力の body 要素内に p 以外の要素が発生する可能性があることを前提としています。p しか発生しない場合は、Nils Werner が提供するソリューションで問題ありません。

しかし実際には、このような入力を処理する必要がある場合、Tidy が行うことのごく一部を行うために独自の XSLT スタイルシートを展開する代わりに、おそらく Tidy を実行するでしょう。

于 2013-07-03T16:13:38.903 に答える