2017-08-23 2 views
0

Meine Quelle HTML:Warum können diese XSLT 1.0-Vorlagen Knoten nicht entfernen?

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

My Stylesheet:

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

Ich kann nicht sehen, warum die letzten drei Vorlagen nicht arbeiten, wie ich erwarten, wenn der erste ist. Vielen Dank.

+0

Abgesehen - ich würde nicht XSLT auf HTML laufen (obwohl geposteten Beispiel ist wohlgeformt) als Markup-Regeln aus den strengeren XML-Standards unterscheiden. Wenn für diesen HTML-Code eine Quelldatei vorhanden ist, sollten Sie das Format dieses Dokuments und nicht die Ausgabe anpassen. – Parfait

+0

Danke für die Notiz. Es gibt keine einzige Quelle, die ich verwenden kann, da HTML die Ausgabe der Vorlagen einer Web-App ist. Ich verwende http://www.html-tidy.org/, um von HTML zu XHTML zu konvertieren. –

Antwort

1

Ihr HTML/XML befindet sich im Standardnamespace http://www.w3.org/1999/xhtml. Binden Sie das an ein Präfix und verwenden Sie es in Ihren XPaths.

Außerdem müssen Sie // in den Vorlagenübereinstimmungen nicht verwenden.

Beispiel ...

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