Tuesday, November 16, 2021

[SOLVED] Adding header part and closing tags to XHTML content

Issue

I have a multiple XHTML pages without header part and closing HTML tags (file002.xhtml, file015.xhtml,etc). How I can add the header part and closing tags to all these pages, use some scripting, or macro? (Linux Debian). I need to add these parts before and after content:

<?xml version="1.0" encoding="UTF-8"?>
<html xml:lang="en-us" lang="en-us" xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:ns="http://www.w3.org/2001/10/synthesis">
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="1260143570.css"/>
</head>
<body>

= CONTENT =

</body>
</html>

Solution

You can just use an XSLT-1.0 processor like xsltproc(Linux), or any other.
A suitable XSLT-stylesheet could look like this:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ws="http://ws">
    <xsl:output method="xml" version="1.0" encoding="utf-8" omit-xml-declaration="no" indent="yes" />

    <xsl:template match="/">
        <html xml:lang="en-us" lang="en-us" xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:ns="http://www.w3.org/2001/10/synthesis">
            <head>
                <title></title>
                <link rel="stylesheet" type="text/css" href="1260143570.css"/>
            </head>
            <body>
                <!-- CONTENT -->
                <xsl:copy-of select="document('input.xml')" />   <!-- Place the input filename here! -->
            </body>
        </html>
    </xsl:template>
    
</xsl:stylesheet>

This works for 1 file.
Pay attention to the fact that you have to call this stylesheet with a bogus XML file as a parameter.


For many more files, you can create a loop with XSLT-2.0's result-docment function. So create an XML file with the files that should be included in your output, like this (a.xml, b.xml, c.xml):

<root>
    <file>a.xml</file>
    <file>b.xml</file>
    <file>c.xml</file>
</root>

Then, an XSLT-2.0

 stylesheet to handle this could look like this:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ws="http://ws">
    <xsl:output method="xml" version="1.0" encoding="utf-8" omit-xml-declaration="no" indent="yes" />

    <xsl:template match="/">
        <xsl:for-each select="/root/file">
            <xsl:result-document href="{substring-before(.,'.')}-WithHeader.html" method="xhtml" encoding="UTF-8" indent="yes">
                <html xml:lang="en-us" lang="en-us" xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:ns="http://www.w3.org/2001/10/synthesis">
                    <head>
                        <title></title>
                        <link rel="stylesheet" type="text/css" href="1260143570.css"/>
                    </head>
                    <body>
                        <!-- CONTENT -->
                        <xsl:copy-of select="document(.)" />
                    </body>
                </html>
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>
    
</xsl:stylesheet>

This stylesheet creates one new XML file for each input XML file specified in the XML passed as parameter and appends the string -WithHeader to the filename in /root/file. This approach probably comes closest to your requirement; just create the XML file with the filenames and run the stylesheet. That should be all.



Answered By - zx485