Dmitriy
Dmitriy

Reputation: 5565

How to combine several XSLT transformations?

I have an HTML file:

<!DOCTYPE html>
<html>
<head>
<title>A title of the article</title>
<style type="text/css">
body {
  font-family: Helvetica, arial, sans-serif;
  font-size: 14px;
  line-height: 1.6;
  padding-top: 10px;
  padding-bottom: 10px;
  background-color: white;
  padding: 30px; }

body > *:first-child {
  margin-top: 0 !important; }
body > *:last-child {
  margin-bottom: 0 !important; }
</style>
</head>
<body>
  <p>The page is an article about an article.</p>
  <p>This paragraph is not very good paragraph</p>
  <p>This paragraph is very good paragraph</p>
  <h4 id="toc_0">Page content</h4>
  <ul>
    <li>An itroduction</li>
    <li>An inline piece of code <code>select * from dual</code></li>
    <li>Buttons <kbd>OK</kbd> and <kbd>Cancel</kbd></li>
  </ul>
  <div>
    <pre>
      <code class="language-none">select * from dual
      </code>
    </pre>
  </div>

  <h4 id="toc_1">Usage</h4>
  <table>
    <thead>
      <tr>
        <th>Page ID</th>
        <th>Page name</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1234</td>
        <td>Page number 1234</td>
      </tr>
      <tr> 
        <td>5678</td>
        <td>Page number 5678</td>
      </tr>
      <tr>
        <td>90AB</td>
        <td>Page number 90AB</td>
      </tr>
    </tbody>
  </table>
</body>
</html>

I need to do the following:

  1. Replace tags code and kbd with span
  2. Add a class to each new span with previous tag name (i. e. <code> ... </code> should be transformed into <span class="code"> ... </span>, etc.)
  3. Keep the content of tag body and remove the rest.

I found how to make this partially. This transformation makes steps 1 and 2:

<xsl:stylesheet version="1.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:template match="code">
        <span class="code"><xsl:apply-templates select="@*|node()" /></span>
    </xsl:template>

    <xsl:template match="kbd">
        <span class="kbd"><xsl:apply-templates select="@*|node()" /></span>
    </xsl:template>
</xsl:stylesheet>

This transformation makes step 3:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/html/body">
        <xsl:copy-of select="node()"/>
    </xsl:template>

    <xsl:template match="text()" />
</xsl:stylesheet>

But each transformation does only its job. I can either replace tags, or cut the content of a body. I tried to combine this transformations, but failed.
Also, the second transformation doesn't add a class, if it already was there. For example, this

<code class="language-none">

turns into this

<span class="language-none">

while I would like to have the following (the sequence of class names doesn't matter):

<span class="language-none code">

And there is an annoying thing. When a source file contains the line

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

then I get an error "invalid XML". OK, I agree it's invalid, but after the second transformation my utility (I use a command-line utility xsltproc on Mac) inserts exactly the same line into the result file.

Upvotes: 0

Views: 245

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167401

To combine the three steps you can write templates for html and body that just process the children, for html/head that does nothing and then you need to find some way to add your new class and keep the existing, here is one:

<xsl:stylesheet version="1.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:template match="html | html/body">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="html/head"/>

    <xsl:template match="code">
        <span class="{@class} code"><xsl:apply-templates select="@*[not(local-name() = 'class')] | node()" /></span>
    </xsl:template>

    <xsl:template match="kbd">
        <span class="{@class} kbd"><xsl:apply-templates select="@*[not(local-name() = 'class')] | node()" /></span>
    </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bdxtqd

Upvotes: 1

Related Questions