Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何在生成pdf之前预处理xsl:fo文档?_Xml_Xslt_Xml Parsing_Xsl Fo - Fatal编程技术网

Xml 如何在生成pdf之前预处理xsl:fo文档?

Xml 如何在生成pdf之前预处理xsl:fo文档?,xml,xslt,xml-parsing,xsl-fo,Xml,Xslt,Xml Parsing,Xsl Fo,考虑以下xml: <book> <chapter> <title>chapter title 1</title> <id>chapterId1</id> <content>some content</content> </chapter> <chapter> <ti

考虑以下xml:

 <book>
    <chapter>
            <title>chapter title 1</title>
        <id>chapterId1</id>
        <content>some content</content> 
    </chapter>
    <chapter>
            <title>chapter title 2</title>
        <id>chapterId2</id>
        <content>some content</content> 
    </chapter>
</book>
如何预处理xsl:fo文件,以便fo:block元素已经设置了正确的ID


谢谢

您不必做任何预处理。XSLT样式表需要对每一章进行两次处理:生成主要内容时和生成TOC时。只要确保在这两种情况下使用相同的ID即可

下面的样式表说明了如何做到这一点(改编自)。它适用于示例源文档

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:apply-templates select="node()|@*"/>
  </xsl:template>

  <xsl:template match="/book">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
       <fo:layout-master-set>
          <fo:simple-page-master master-name="my-page" page-width="8.5in" 
                                 page-height="11in">
             <fo:region-body margin="1in" margin-top="1.5in"/>
          </fo:simple-page-master>
       </fo:layout-master-set>
       <fo:page-sequence master-reference="my-page">
         <fo:flow flow-name="xsl-region-body">
         <!-- Call the template that generates the TOC -->
           <xsl:call-template name="genTOC"/>
         </fo:flow>
        </fo:page-sequence>
        <!-- Then output the main content -->
        <xsl:apply-templates/>
    </fo:root>
  </xsl:template>

  <xsl:template name="genTOC">
    <fo:block break-before='page'>
      <fo:block font-size="16pt" font-weight="bold">TABLE OF CONTENTS</fo:block>
      <xsl:for-each select="chapter">
        <fo:block text-align-last="justify">
            <xsl:value-of select="count(preceding::chapter) + 1" />
            <xsl:text> </xsl:text>
            <xsl:value-of select="title" />
            <fo:leader leader-pattern="dots" />
            <fo:page-number-citation ref-id="{id}" />
        </fo:block>
      </xsl:for-each>
    </fo:block>
  </xsl:template>

  <xsl:template match="title|content">
    <fo:block><xsl:value-of select="."/></fo:block>
  </xsl:template>

  <xsl:template match="chapter">
    <fo:page-sequence master-reference="my-page" id="{id}">
       <fo:flow flow-name="xsl-region-body">
         <xsl:apply-templates/>
       </fo:flow>
    </fo:page-sequence>
  </xsl:template>

</xsl:stylesheet>

目录
<fo:block> <!-- the chapter container -->
  <xsl:attribute name="id">
    <xsl:value-of select="chapter/id" />
  </xsl:attribute>
  <fo:block>
      the chapter content.....
  </fo:block>
</fo:block>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:apply-templates select="node()|@*"/>
  </xsl:template>

  <xsl:template match="/book">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
       <fo:layout-master-set>
          <fo:simple-page-master master-name="my-page" page-width="8.5in" 
                                 page-height="11in">
             <fo:region-body margin="1in" margin-top="1.5in"/>
          </fo:simple-page-master>
       </fo:layout-master-set>
       <fo:page-sequence master-reference="my-page">
         <fo:flow flow-name="xsl-region-body">
         <!-- Call the template that generates the TOC -->
           <xsl:call-template name="genTOC"/>
         </fo:flow>
        </fo:page-sequence>
        <!-- Then output the main content -->
        <xsl:apply-templates/>
    </fo:root>
  </xsl:template>

  <xsl:template name="genTOC">
    <fo:block break-before='page'>
      <fo:block font-size="16pt" font-weight="bold">TABLE OF CONTENTS</fo:block>
      <xsl:for-each select="chapter">
        <fo:block text-align-last="justify">
            <xsl:value-of select="count(preceding::chapter) + 1" />
            <xsl:text> </xsl:text>
            <xsl:value-of select="title" />
            <fo:leader leader-pattern="dots" />
            <fo:page-number-citation ref-id="{id}" />
        </fo:block>
      </xsl:for-each>
    </fo:block>
  </xsl:template>

  <xsl:template match="title|content">
    <fo:block><xsl:value-of select="."/></fo:block>
  </xsl:template>

  <xsl:template match="chapter">
    <fo:page-sequence master-reference="my-page" id="{id}">
       <fo:flow flow-name="xsl-region-body">
         <xsl:apply-templates/>
       </fo:flow>
    </fo:page-sequence>
  </xsl:template>

</xsl:stylesheet>