Page 1 of 1

Outputting text content before HTML node in DITA/XHTML transformation

Posted: Tue Sep 12, 2017 12:11 pm
by d_croft
Hi there,

I'm trying to output content before the root HTML node in a DITA -> XHTML transformation. I located the XSLT section from dita2htmlImpl.xsl, where the <html> element is generated, but entering any text content before it produces the following error:

Code: Select all

System ID: /Applications/oxygen/frameworks/dita/DITA-OT2.x/plugins/org.mycashflow.xhtml.docs/faq-question.dita
Scenario: DITA XHTML MCF
Input file: /Applications/oxygen/frameworks/dita/DITA-OT2.x/plugins/org.mycashflow.xhtml.docs/faq-question.dita
Engine name: DITA-OT
Severity: fatal
Description: When 'standalone' or 'doctype-system' is specified, the document must be well-formed; but this document contains a top-level text node
Start location: 40:0


This is a model of the XSLT I was using:

Code: Select all

<!-- Matches /dita or a root topic -->
    <xsl:template match="*" mode="root_element" name="root_element">
        <xsl:call-template name="chapter-setup"/>
    </xsl:template>

    <xsl:template name="chapter-setup">
       
        <!-- This causes problems. -->
        <xsl:variable name="metafield">
            <xsl:value-of select="topic/element/@attribute"/>
        </xsl:variable>
        TEXT and {$metafield}
       
        <html>
            <xsl:call-template name="setTopicLanguage"/>
            <xsl:value-of select="$newline"/>
            <xsl:call-template name="chapterHead"/>
            <xsl:call-template name="chapterBody"/>
        </html>
    </xsl:template>


Do you know of any way to accomplish what I'm trying (ie. entering text and XSL variables before the root node of the HTML document)?

Re: Outputting text content before HTML node in DITA/XHTML transformation

Posted: Tue Sep 12, 2017 12:29 pm
by adrian
Hi,

What is the desired result? Why do you want to output text before the html root node?

Note that the output is not HTML, but XHTML which must be XML well formed, so this is not possible without changing the output type. You should be able to output there XML comments or processing instructions which do not break the XML well-formedness, but definitely not text.
e.g.

Code: Select all

<xsl:comment>TEXT and <xsl:value-of select="$metafield"/></xsl:comment>

Regards,
Adrian

Re: Outputting text content before HTML node in DITA/XHTML transformation

Posted: Tue Sep 12, 2017 1:33 pm
by d_croft
Thanks for the reply. We are experimenting with publishing DITA through Jekyll, which uses YAML variables that are placed before any other content in the HTML file. We would like to fetch metadata from DITA during the transformation to HTML and enter it into the YAML front matter.

I changed the transtype to "html5", which did output the content, but in the wrong place. This was the result:

Code: Select all

<html xmlns:dita2html="http://dita-ot.sourceforge.net/ns/200801/dita2html" lang="en-us">
<head></head>
<body id="question-id">TEXT and {$metafield}
       
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8"><meta name="copyright" content="(C) Copyright 2017">
<meta name="DC.rights.owner" content="(C) Copyright 2017">
<meta name="DC.Type" content="faq-question">
<meta name="description" content="Blaa"><meta name="DC.Format" content="XHTML">
<meta name="DC.Identifier" content="question-id">
<link rel="stylesheet" type="text/css" href="commonltr.css">
<title>Can I add attributes to specific element types?</title>


I don't know what happened, but obviously that won't work.

I then used <xsl:output method="html"></xsl:output> in my XSLT and did this:

Code: Select all

<xsl:template name="chapter-setup">
       
        <xsl:comment>TEXT and <xsl:value-of select="topic/element/@attribute"/></xsl:comment>
        <html>
            <xsl:call-template name="setTopicLanguage"/>
            <xsl:value-of select="$newline"/>
            <xsl:call-template name="chapterHead"/>
            <xsl:call-template name="chapterBody"/>
        </html>
    </xsl:template>

This managed to get the content between <html> and the Doctype declaration, which is still the wrong place, on top of which the content is in an HTML comment, which doesn't work (although I'm sure there's a workaround for this).

Re: Outputting text content before HTML node in DITA/XHTML transformation

Posted: Wed Sep 13, 2017 2:57 pm
by Radu
Hi,

So you have a DITA OT plugin customization which adds an HTML XSLT stylesheet customization, right?
And you set the xsl:output something like this in the custom stylesheet:

Code: Select all

<xsl:output method="html"/>


This should override the default xsl:output declaration. I'm not sure why you still get the DOCTYPE in the HTML output though.
You can probably use such a construct:

Code: Select all

<xsl:text disable-output-escaping="yes"><![CDATA[<abla></abla>]]></xsl:text>


to output some tags before the root element.

If all else fails you can post-process the result HTML documents using regular expressions.
You can write your own ANT build file which invokes the DITA OT processing and afterwards may alter the resulting HTML documents:

https://www.oxygenxml.com/doc/versions/19.0/ug-editor/topics/dita-map-use-your-own-build-file.html

Regards,
Radu