Adding new element to Docbook

186 Views Asked by At

I’m new to docbook and XSLT, I’m trying to add a new element to my docbook I have an XML, something like this:

 <?xml version='1.0' encoding='UTF-8'?>
    <book  version="5.0"
      xmlns="http://docbook.org/ns/docbook"> 
    <d:chapter>
            <d:title/>
            <d:section>
              <d:title>Section 1</d:title>
              <d:simpara>texte   </d:simpara>
            </d:section>
          </d:chapter>
        </d:book>

I need to do multiples transformations in one step; First transformation is adding a new section with title and some text in the docbook xml structure then generate the transformation to get my output file as a pdf file in the second transformation. I create new element inside a variable, convert the variable's content to a node-set using the exlt:node-set() function, and then process the node-set with the standard DocBook stylesheets to get pdf output

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:fo="http://www.w3.org/1999/XSL/Format"
        xmlns:set="http://exslt.org/sets"
        xmlns:saxon="http://icl.com/saxon"
        xmlns:exsl="http://exslt.org/common"
        extension-element-prefixes="exsl"
        xmlns:d="http://docbook.org/ns/docbook"
        exclude-result-prefixes="d"
        version="1.0">

    <?cco_xsltype addon="DocBook5.0" toolchain="DocBook_to_PDF_FOP"?>
    <!-- Import standard -->

    <xsl:import href="http://docbook.sourceforge.net/release/xsl/current/fo/profile-docbook.xsl"/>
    <!-- DocBook XSL Parameters -->

    <xsl:template match="node() | @*" mode="change"> 
        <xsl:copy>  
            <xsl:apply-templates select="@* | node()" mode="change"/>  
        </xsl:copy>
    </xsl:template>         
    <xsl:template match="d:chapter" mode="change">

        <xsl:copy> 
            <xsl:copy-of select="@*"/>
            <xsl:copy-of select="*"/>
            <d:section>
                <d:title> Cette section a été ajoutée </d:title>
                <d:para> ce texte a été ajouté </d:para>
            </d:section>
        </xsl:copy>
    </xsl:template>
<xsl:template match="d:book" >

<xsl:variable name="content"> 
        <xsl:apply-templates mode="change"/> 
    </xsl:variable>

    <xsl:apply-templates select="exsl:node-set($content)/*"  /> 

</xsl:template> 

</xsl:stylesheet>

As result I expect a pdf file with 2 section.

Thanks for any help.

1

There are 1 best solutions below

0
Verhagen On

Not sure if this might answer your question. But it looks as if you want to achieve to add some standard paragraph optionally, to every document.

If the input document contains a certain variable, it gets replaced by a standard paragraph. This paragraph is part of the XSL.

If that is indeed the case, why not combine XML documents and fragments using XInclude? This is a good possibility for including XML document into other XML documents. A good start is Understanding XInclude.