TheTechGuy
TheTechGuy

Reputation: 17354

Adding header and footer in an XSLT file

I have an XSLT file which transforms an XML into a word document. My problem is I want to add headers and footer in the document. How do I do it?

My question is basically similar to this Header and footer and page numbers using xslt.

More help about headers and footers can be found here. Basically I need to use these tags but how do I use it in my template? Do I put it in the end or somewhere in the middle? I would also like to change the word page layout to landscape which is portrait be default.

<fo:region-before> defines the top region (header)
<fo:region-after> defines the bottom region (footer)

Assume this is my XSLT

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
        <tr>
          <td><xsl:value-of select="title"/></td>
          <td><xsl:value-of select="artist"/></td>
        </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

The above example has been copied from w3school

Upvotes: 0

Views: 9613

Answers (1)

grtjn
grtjn

Reputation: 20414

As said in plenty comments, don't generate HTML, but either XSL-FO or Word XML entirely. Since Word XML is the most direct approach, I'll help you with the smallest sample I could think of that returns something similar to your HTML, but with header and footer. Actually, I just opened a resulting HTML in Word, saved that as Word XML, added header and footer, and got rid of most of the superflues XML tagging Word automatically adds.

Put the following inside your root xsl template, add a for each around the table rows, and fill header/footer dynamically the way you like..

<w:wordDocument xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no" xml:space="preserve">

<w:docPr>
<w:view w:val="print"/>
<w:zoom w:percent="100"/>
</w:docPr>

<w:styles>
<w:style w:type="paragraph" w:styleId="Heading2">
<w:name w:val="heading 2"/>
<wx:uiName wx:val="Heading 2"/>
<w:basedOn w:val="Standaard"/>
<w:pPr>
<w:spacing w:before="100" w:before-autospacing="on" w:after="100" w:after-autospacing="on"/>
<w:outlineLvl w:val="1"/>
</w:pPr>
<w:rPr>
<w:b/>
<w:sz w:val="36"/>
</w:rPr>
</w:style>

<w:style w:type="paragraph" w:styleId="Headertext">
<w:name w:val="header"/>
<wx:uiName wx:val="Header text"/>
<w:basedOn w:val="Standaard"/>
<w:pPr>
<w:tabs>
<w:tab w:val="center" w:pos="4536"/>
<w:tab w:val="right" w:pos="9072"/>
</w:tabs>
</w:pPr>
</w:style>

<w:style w:type="paragraph" w:styleId="Footertext">
<w:name w:val="footer"/>
<wx:uiName wx:val="Footer text"/>
<w:basedOn w:val="Standaard"/>
<w:pPr>
<w:tabs>
<w:tab w:val="center" w:pos="4536"/>
<w:tab w:val="right" w:pos="9072"/>
</w:tabs>
</w:pPr>
</w:style>
</w:styles>

<w:body>
<w:p>
<w:pPr>
<w:pStyle w:val="Heading2"/>
</w:pPr>
<w:r>
<w:t>My CD Collection</w:t>
</w:r>
</w:p>

<w:tbl>
<w:tblPr>
<w:tblW w:w="5000" w:type="pct"/>
<w:tblCellSpacing w:w="15" w:type="dxa"/>
<w:tblBorders>
<w:top w:val="outset" w:sz="6" wx:bdrwidth="15" w:space="0" w:color="auto"/>
<w:left w:val="outset" w:sz="6" wx:bdrwidth="15" w:space="0" w:color="auto"/>
<w:bottom w:val="outset" w:sz="6" wx:bdrwidth="15" w:space="0" w:color="auto"/>
<w:right w:val="outset" w:sz="6" wx:bdrwidth="15" w:space="0" w:color="auto"/>
</w:tblBorders>
<w:tblCellMar>
<w:top w:w="15" w:type="dxa"/>
<w:left w:w="15" w:type="dxa"/>
<w:bottom w:w="15" w:type="dxa"/>
<w:right w:w="15" w:type="dxa"/>
</w:tblCellMar>
<w:tblLook w:val="04A0"/>
</w:tblPr>
<w:tr>
<w:tc>
<w:p>
<w:r>
<w:rPr>
<w:b/>
</w:rPr>
<w:t>Title</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:rPr>
<w:b/>
</w:rPr>
<w:t>Artist</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
<w:tr>
<w:tc>
<w:p>
<w:r>
<w:t>title1</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t>artist1</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
<w:tr>
<w:tc>
<w:p>
<w:r>
<w:t>title2</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:p>
<w:r>
<w:t>artist2</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</w:tbl>

<w:p />

<w:hdr w:type="odd">
<w:p>
<w:pPr>
<w:pStyle w:val="Headertext"/>
</w:pPr>
<w:r>
<w:t>Header text</w:t>
</w:r>
<w:r>
<w:tab/>
<w:t>center</w:t>
</w:r>
<w:r>
<w:tab/>
<w:t>right</w:t>
</w:r>
</w:p>
</w:hdr>

<w:ftr w:type="odd">
<w:p>
<w:pPr>
<w:pStyle w:val="Footertext"/>
</w:pPr>
<w:r>
<w:t>left</w:t>
</w:r>
<w:r>
<w:tab/>
<w:t>Footer text</w:t>
</w:r>
<w:r>
<w:tab/>
</w:r>
<w:r>
<w:fldChar w:fldCharType="begin"/>
</w:r>
<w:r>
<w:instrText>PAGE   \* MERGEFORMAT</w:instrText>
</w:r>
<w:r>
<w:fldChar w:fldCharType="separate"/>
</w:r>
<w:r>
<w:t>1</w:t>
</w:r>
<w:r>
<w:fldChar w:fldCharType="end"/>
</w:r>
</w:p>
</w:ftr>
</w:body>
</w:wordDocument>

It also helps to make sure the following processing-instruction gets added before the root element:

<?mso-application progid="Word.Document"?>

HTH!

Upvotes: 1

Related Questions