-- XSL module extracted from ITU-T T.805 (01/2012)

<?xml version="1.0" encoding="ISO-8859-1"?> <!-- XSL Script for translating from HTX to HTML. Tested with - Xalan-C++ 1.10.0 - Internet Explorer 6 - Mozilla Firefox 1.5 Author: Andreas Hirth, LuraTech Imaging GmbH, www.luratech.com Todo: - supporting polygonic bounding shapes Note: - altchar and altword are skipped - absolute positioning is used for every element with coordinates --> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xalan="http://xml.apache.org/xalan" xmlns:htx="http://www.jpeg.org/hiddentext/htx" exclude-result-prefixes="xsl xsi xhtml xalan htx"> <xsl:output method="html" indent="yes" xalan:indent-amount="4"/> <!-- Make the character enclosing <span> tags be written without linebreak or whitespaces in between. This is important because the browser will put a blank between each character otherwise. --> <xsl:strip-space elements="*"/> <!-- Make a <title> tag from a xhtml <title> tag > --> <xsl:template match="xhtml:head/xhtml:title"> <title><xsl:value-of select="text()"/></title> </xsl:template> <!-- Make a <style> tag from a xhtml <style> tag > --> <xsl:template match="xhtml:head/xhtml:style"> <style type="text/css"><xsl:value-of select="text()"/></style> </xsl:template> <!-- Translation entry point --> <xsl:template match="/"> <html> <head> <xsl:apply-templates select="//xhtml:title"/> <xsl:apply-templates select="//xhtml:style"/> </head> <body> <xsl:apply-templates select="htx:htx"/> </body> </html> </xsl:template> <!-- There is nothing to do with the root element --> <xsl:template match="htx:htx"> <xsl:apply-templates/> </xsl:template> <!-- Skip annotations --> <xsl:template match="htx:annotations"/> <!-- There is nothing to do with the hiddentext element --> <xsl:template match="htx:hiddentext"> <xsl:apply-templates/> </xsl:template> <!-- Translate the <region> element into a <div> tag with core attributes and positioning --> <xsl:template match="htx:region"> <xsl:element name="div"> <xsl:call-template name="outputCoreAttributes"/> <xsl:call-template name="outputPositionAttributes"/> <xsl:apply-templates/> </xsl:element> </xsl:template> <!-- Translate the <paragraph> element into a <p> tag with core attributes and positioning --> <xsl:template match="htx:paragraph"> <xsl:element name="p"> <xsl:call-template name="outputCoreAttributes"/> <xsl:call-template name="outputPositionAttributes"/> <xsl:apply-templates/> </xsl:element> </xsl:template> <!-- Translate the <line> element into a <span> tag with core attributes and positioning --> <xsl:template match="htx:line"> <xsl:element name="span"> <xsl:call-template name="outputCoreAttributes"/> <xsl:call-template name="outputPositionAttributes"/> <xsl:apply-templates/> </xsl:element> </xsl:template> <!-- Translate the <word> element into a <span> tag with core attributes and positioning --> <xsl:template match="htx:word"> <xsl:element name="span"> <xsl:call-template name="outputCoreAttributes"/> <xsl:call-template name="outputPositionAttributes"/> <xsl:apply-templates/> </xsl:element> </xsl:template> <!-- Translate the <char> element into a <span> tag with core attributes and special positioning --> <xsl:template match="htx:char"> <xsl:element name="span"> <xsl:call-template name="outputCoreAttributes"/> <xsl:call-template name="outputCharacterPositionAttributes"/> <xsl:value-of select="text()"/> </xsl:element> </xsl:template> <!-- Do not translate any other elements, like <altword> or <altchar> --> <xsl:template match="*"/> <!-- Parse and output the core attributes of an element --> <xsl:template name="outputCoreAttributes"> <xsl:if test="@class"> <xsl:attribute name="class"><xsl:value-of select="@class"/></xsl:attribute> </xsl:if> <xsl:if test="@lang"> <xsl:attribute name="lang"><xsl:value-of select="@lang"/></xsl:attribute> </xsl:if> <xsl:if test="@xml:lang"> <xsl:attribute name="xml:lang"><xsl:value-of select="@xml:lang"/></xsl:attribute> </xsl:if> <xsl:if test="@dir"> <xsl:attribute name="dir"><xsl:value-of select="@dir"/></xsl:attribute> </xsl:if> <xsl:if test="@id"> <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute> </xsl:if> </xsl:template> <!-- Parse and output positioning attributes --> <xsl:template name="outputPositionAttributes"> <xsl:choose> <xsl:when test="@shape=''poly''"> <xsl:message>Polygonic shapes are not supported yet. No positioning attributes are written for element <<xsl:value-of select="name()"/>>. </xsl:message> </xsl:when> <xsl:otherwise> <xsl:call-template name="outputRelativeCoordinatesAttribute"/> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- Parse and output positioning attributes especialy for characters --> <xsl:template name="outputCharacterPositionAttributes"> <xsl:choose> <xsl:when test="@shape=''poly''"> <xsl:message>Polygonic shapes are not supported yet. No positioning attributes are written for element <xsl:value-of select="name()"/></xsl:message> </xsl:when> <xsl:otherwise> <xsl:call-template name="outputRelativeCharacterCoordinatesAttribute"/> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- Calculate and output positioning attributes from coords. Must be recalculated to be relative to the nearest parent with coordinates. --> <xsl:template name="outputRelativeCoordinatesAttribute"> <xsl:variable name="horRes"> <xsl:choose> <xsl:when test="/htx:htx[(position()=1) and (@res)]"> <xsl:variable name="res"><xsl:value-of select="/htx:htx[1]/@res"/></xsl:variable> <xsl:choose> <xsl:when test="contains($res, '','')"> <xsl:value-of select="normalize-space(substring-before($res, '',''))"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$res"/> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="vertRes"> <xsl:choose> <xsl:when test="/htx:htx[(position()=1) and (@res)]"> <xsl:variable name="res"><xsl:value-of select="/htx:htx[1]/@res"/></xsl:variable> <xsl:choose> <xsl:when test="contains($res, '','')"> <xsl:value-of select="normalize-space(substring-after($res, '',''))"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$res"/> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="left"> <xsl:variable name="coord"><xsl:value-of select="number(normalize-space(substring-before(@coords, '','')))"/></xsl:variable> <xsl:choose> <xsl:when test="number($horRes) > 0"> <xsl:value-of select="$coord div $horRes"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$coord"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="top"> <xsl:variable name="coord"><xsl:value-of select="number(normalize-space(substring-before(substring-after(@coords, '',''), '','')))"/></xsl:variable> <xsl:choose> <xsl:when test="number($vertRes) > 0"> <xsl:value-of select="$coord div $vertRes"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$coord"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:if test="(@shape=''rect'') and (string-length($left)>0) and (string-length($top)>0)"> <xsl:variable name="parentLeft"> <xsl:choose> <xsl:when test="ancestor::*[(@shape=''rect'') and (@coords)]"> <xsl:variable name="coord"><xsl:value-of select="number(normalize-space(substring-before(ancestor::*[(@shape=''rect'') and (@coords)][1]/@coords, '','')))"/></xsl:variable> <xsl:choose> <xsl:when test="number($horRes) > 0"> <xsl:value-of select="$coord div $horRes"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$coord"/> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="parentTop"> <xsl:choose> <xsl:when test="ancestor::*[(@shape=''rect'') and (@coords)]"> <xsl:variable name="coord"><xsl:value-of select="number(normalize-space(substring-before(substring-after(ancestor::*[(@shape=''rect'') and (@coords)][1]/@coords, '',''), '','')))"/></xsl:variable> <xsl:choose> <xsl:when test="number($vertRes) > 0"> <xsl:value-of select="$coord div $vertRes"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$coord"/> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="unit"> <xsl:choose> <xsl:when test="(number($horRes) > 0) and (number($vertRes) > 0)">in</xsl:when> <xsl:otherwise>px</xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:attribute name="style">position:absolute; left:<xsl:value-of select="$left - $parentLeft"/><xsl:value-of select="$unit"/>; top:<xsl:value-of select="$top - $parentTop"/><xsl:value-of select="$unit"/>;</xsl:attribute> </xsl:if> </xsl:template> <!-- Calculate and output positioning attributes from coords especialy for characters. Must be recalculated to be relative to the nearest parent with coordinates. The top coordinate is used from the parent due to get strait lines because the bounding boxes of characters in HTML are equal height for large and small letters of a font. --> <xsl:template name="outputRelativeCharacterCoordinatesAttribute"> <xsl:variable name="horRes"> <xsl:choose> <xsl:when test="/htx:htx[(position()=1) and (@res)]"> <xsl:variable name="res"><xsl:value-of select="/htx:htx[1]/@res"/></xsl:variable> <xsl:choose> <xsl:when test="contains($res, '','')"> <xsl:value-of select="normalize-space(substring-before($res, '',''))"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$res"/> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="vertRes"> <xsl:choose> <xsl:when test="/htx:htx[(position()=1) and (@res)]"> <xsl:variable name="res"><xsl:value-of select="/htx:htx[1]/@res"/></xsl:variable> <xsl:choose> <xsl:when test="contains($res, '','')"> <xsl:value-of select="normalize-space(substring-after($res, '',''))"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$res"/> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="left"> <xsl:variable name="coord"><xsl:value-of select="number(normalize-space(substring-before(@coords, '','')))"/></xsl:variable> <xsl:choose> <xsl:when test="number($horRes) > 0"> <xsl:value-of select="$coord div $horRes"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$coord"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="top"> <xsl:variable name="coord"><xsl:value-of select="number(normalize-space(substring-before(substring-after(@coords, '',''), '','')))"/></xsl:variable> <xsl:choose> <xsl:when test="number($vertRes) > 0"> <xsl:value-of select="$coord div $vertRes"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$coord"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:if test="(string-length($left)>0) and (string-length($top)>0)"> <xsl:variable name="parentLeft"> <xsl:choose> <xsl:when test="ancestor::*[(@shape=''rect'') and (@coords)]"> <xsl:variable name="coord"><xsl:value-of select="number(normalize-space(substring-before(ancestor::*[(@shape=''rect'') and (@coords)][1]/@coords, '','')))"/></xsl:variable> <xsl:choose> <xsl:when test="number($horRes) > 0"> <xsl:value-of select="$coord div $horRes"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$coord"/> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="parentTop"> <xsl:choose> <xsl:when test="ancestor::*[(@shape=''rect'') and (@coords)]"> <xsl:variable name="coord"><xsl:value-of select="number(normalize-space(substring-before(substring-after(ancestor::*[(@shape=''rect'') and (@coords)][1]/@coords, '',''), '','')))"/></xsl:variable> <xsl:choose> <xsl:when test="number($vertRes) > 0"> <xsl:value-of select="$coord div $vertRes"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$coord"/> </xsl:otherwise> </xsl:choose> </xsl:when> </xsl:choose> </xsl:variable> <xsl:variable name="finalTop"> <xsl:choose> <xsl:when test="string-length($parentTop) > 0">0</xsl:when> <xsl:otherwise> <xsl:value-of select="$top"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="unit"> <xsl:choose> <xsl:when test="(number($horRes) > 0) and (number($vertRes) > 0)">in</xsl:when> <xsl:otherwise>px</xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:attribute name="style">position:absolute; left:<xsl:value-of select="$left - $parentLeft"/><xsl:value-of select="$unit"/>; top:<xsl:value-of select="number($finalTop)"/><xsl:value-of select="$unit"/>;</xsl:attribute> </xsl:if> </xsl:template> </xsl:stylesheet>