0

XML を Word 文書に変換する XSLT ファイルがあります。私の問題は、ドキュメントにヘッダーとフッターを追加したいということです。どうすればいいのですか?

私の質問は、基本的にこのヘッダーとフッター、および xslt を使用したページ番号に似ています。

ヘッダーとフッターの詳細については、こちらを参照してください。基本的にこれらのタグを使用する必要がありますが、テンプレートでどのように使用すればよいですか? 最後に入れるか、途中に入れるか。また、単語のページ レイアウトをデフォルトでポートレートであるランドスケープに変更したいと思います。

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

これが私の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>

上記の例はw3schoolからコピーされたものです

4

1 に答える 1

1

多くのコメントで述べられているように、HTML を生成するのではなく、XSL-FO または Word XML を完全に生成してください。Word XML は最も直接的なアプローチであるため、HTML に似たものをヘッダーとフッターで返す、考えられる最小のサンプルを使用してお手伝いします。実際、結果の HTML を Word で開き、それを Word XML として保存し、ヘッダーとフッターを追加し、Word が自動的に追加する余分な XML タグ付けのほとんどを取り除きました。

以下をルート xsl テンプレート内に配置し、テーブル行の周りに for each を追加し、ヘッダー/フッターを好きなように動的に埋めます。

<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>

また、次の処理命令がルート要素の前に追加されることを確認するのにも役立ちます。

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

チッ!

于 2012-05-11T08:06:06.943 に答える