2017-08-23 4 views
0

私のソースHTML:これらのXSLT 1.0テンプレートがノードを削除できないのはなぜですか?

<!DOCTYPE html> 
<html class="no-js" lang="en-GB" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB"> 
    <head> 
     <meta name="generator" content="HTML Tidy for HTML5 for Linux version 5.2.0" /> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <title>Test</title> 
    </head> 
    <body> 
     <div class="lay-nav-primary"> 
      <ul class="TabMenu"> 
       <li> 
        <a href="http://example.com/">I am not wanted but am not removed.</a> 
       </li> 
      </ul> 
     </div> 
     <div class="lay-library--header"> 
      I am not wanted and am removed. 
     </div> 
     <p>I am not wanted but am not removed.</p> 
    </body> 
</html> 

私のXSLTスタイルシート:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/> 
    <xsl:strip-space elements="*"/> 

    <!-- Identity transform --> 
    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <!-- Remove unwanted elements --> 

    <!-- successfully removes node with the given class --> 
    <xsl:template match="//*[contains(concat(' ', normalize-space(@class), ' '), ' lay-library--header ')]"/> 

    <!-- fails to remove 'ul' child node of node with the given class --> 
    <xsl:template match="//*[contains(concat(' ', normalize-space(@class), ' '), ' lay-nav-primary ')]/ul"/> 

    <!-- fails to remove 'p' nodes --> 
    <xsl:template match="p | p/* | //p | //p/*"/> 

    <!-- fails to remove 'p' nodes --> 
    <xsl:template match="p | p/* | //p | //p/*" priority="9"/> 

</xsl:stylesheet> 

最初のときに私は期待どおり、最後の3つのテンプレートが働いていない理由を私は見ることができません。ありがとう。

+0

さておき - (ポストされた例では、整形式であるが)、マークアップ規則はより厳格なXML標準と異なるように私はHTMLにXSLTを実行しないでしょう。このHTMLへのソースファイルがある場合、出力ではなくそのドキュメントのスタイリングを検討してください。 – Parfait

+0

ノートをありがとう。 HTMLとしては、Webアプリケーションのテンプレートの出力として使用できる単一のソースはありません。私は、HTMLからXHTMLに変換するためにhttp://www.html-tidy.org/を使用しています。 –

答えて

1

HTML/XMLは、デフォルトのネームスペースhttp://www.w3.org/1999/xhtmlにあります。それを接頭辞にバインドし、XPathで使用します。

また、テンプレートマッチで//を使用する必要はありません。

例...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:x="http://www.w3.org/1999/xhtml"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/> 
    <xsl:strip-space elements="*"/> 

    <!-- Identity transform --> 
    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <!-- Remove unwanted elements --> 

    <!-- successfully removes node with the given class --> 
    <xsl:template match="*[contains(concat(' ', normalize-space(@class), ' '), ' lay-library--header ')]"/> 

    <!-- successfully removes 'x:ul' child node of node with the given class --> 
    <xsl:template match="*[contains(concat(' ', normalize-space(@class), ' '), ' lay-nav-primary ')]/x:ul"/> 

    <!--successfully removes x:p nodes--> 
    <xsl:template match="x:p"/> 

</xsl:stylesheet> 
関連する問題