Howto customize the transformation of span

This chapter shows the customized transformation of HTML span elements with a certain class attribute to markup elements in the resulting DocBook XML. The path of the input HTML file is ./span.html and the content looks like this:

<html> 
  <body> 
    <p>Lorem ipsum <span class="tag">dolor</span> sit <span>amet</span> consectetur, adipisci velit.</p> 
  </body> 
</html>

To customize the transformation, a groovy script must be created which manipulates the HTML-Tree and gives a editor instruction to the transformation engine. The script is located in the directory ./scripts/html and its name is span.groovy:

import org.dbdoclet.trafo.tag.docbook.Markup
import org.dbdoclet.xiphias.dom.NodeImpl
import org.dbdoclet.xiphias.dom.TransformInstruction
import org.w3c.dom.Node

String attr = node.getAttribute("class") 

if (attr != null && attr == "tag") {
  Markup markup = new Markup() 
  TransformInstruction ti = new TransformInstruction()
  ti.setReplacement(markup) 
  node.setTransformInstruction(ti) 
} 

The script is called for every span element while traversing the HTML tree. Herold passes a parameter named node of type org.dbdoclet.xiphias.dom.NodeImpl to the groovy code. In the next step the script checks wether an attribute class with a value of tag exists in the node. If this test returns true, an object of type org.dbdoclet.trafo.tag.docbook.Markup is created. Then this markup object is stored as replacement in an also newly created TransformInstruction object. Finally the transform instruction is added to the node. Later, when the transformation engine is traversing the tree, the instruction will be valuated and the span element will be transformed to a markup tag. To execute herold with this customization run the command:

herold -i span.html --html-script-path=./scripts/html/

The result shoul look like this:

<?xml version="1.0" encoding="UTF-8"?> 
<article version="5.0" xmlns="http://docbook.org/ns/docbook" xmlns:xl="http://www.w3.org/1999/xlink">
  <info>
    <title>Lorem ipsum </title>
  </info> 
  <para>Lorem ipsum <markup>dolor</markup> sit amet consectetur, adipisci velit.</para>
</article>