1

How can I apply xslt2.0 to any html file on ubuntu?

I'm looking for something that'll make this work:

wget -qO- "https://www.amazon.com/" | ????? | saxonb-xslt -o:output.xml -xsl:transform20.xsl -s:-

It hast to be robust enough to work with html in the wild, e.g. https://www.nzz.ch/ http://www.spiegel.de/


transform20.xsl:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

1 Answer 1

1

The question

How can I apply xslt2.0 to any html file on ubuntu?

The short answer

You can't.

The long answer

HTML isn't XML. XSLT is applicable to XML, but not to HTML. It doesn't matter what OS you're running.

IFF you can ascertain that your given HTML file is in fact XHTML, then it is also valid as XML and can be processed using XSLT. However, most HTML is not XHTML, and valid HTML constructs like the following:

<html>
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>
    <p>This is content.<br>
       This is the second line.</p>
  </body>
</html>

... will cause an XML or XSLT engine to complain that this is invalid markup, and it will fail to process.

The key problem is that HTML allows for opening tags that have no close. In the example above, the <meta> and <br> tags don't close. To an XML processor, these tags start a construct that never ends. HTML processors have embedded lists of these elements, but XML processors do not.

The workaround

For the above HTML snippet above to be processable by an XML / XSLT engine, the <meta> and <br> tags would have to either have ending tags, as <meta></meta>, or they'd have to be self-closing, as <meta/>.

There are probably tools out there to convert HTML to XHTML. A quick Google search turns some up. You might be able to run your HTML through those first, and then process them using XSLT.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .