【问题标题】:Xslt to xsl-fo transformationXslt 到 xsl-fo 的转换
【发布时间】:2012-03-15 20:15:47
【问题描述】:

我想对 xsl-fo 进行 xslt 转换,但我不确定我能做到这一点。我尝试将 XML 列表转换为 xsl-fo 列表。谁能告诉我在哪里可以找到我在谷歌上搜索了很长时间没有很多这样的例子。我的 XML 是这样的。

 <p>TEXT</p>
 <ul>
   <li>Item1</li>
   <li>Item2</li>
 </ul>
 <p>ANOTHERTEXT</p>

我尝试使用模板进行此转换,但我的模板无法获取 xsl-fo 任何人都可以告诉我模板是否适用于此转换。如果他们工作可以给我一个例子,我找不到任何人。我的目标是得到一个 pdf whit fop

谢谢


这是我的 XML 文档的一部分,我以 HTML 格式接收了部分源代码,现在我将 HTML 更改为 XML,现在我尝试将 XML(带有列表)转换为带有 XSLT 的 XSL-FO。我的问题是我无法将模板应用于此转换。我的最终目标是获得一份带有 FOP 的 pdf。

谢谢

更新

这是我的 XML:

<Memoria>
  <name>TITLE</name>
  <Index>INDEX 2010</Index>
  <Seccion>
     <name>INFORMATION</name>
     <Contenido>
       <p>TEXT</p>
       <ul>
     <li>ITEM1</li>
     <li>ITEM2</li>
      </ul>
      <p>ANOTHER</p>
    </Contenido>
  </Seccion>
</Memoria>

我正在测试你的解决方案谢谢大家

【问题讨论】:

  • HTML 没有 100% 转换为 XSL-FO。
  • 确切的期望结果是什么?请编辑问题并提供。 XSLT 专家很少同时具备 xsl-fo - 认识。

标签: xml xslt xsl-fo


【解决方案1】:

如果您的模板无法正常工作,则可能是命名空间问题。您应该使用更准确的 XML 示例更新问题。

这是一个例子。

XML 输入(固定为格式正确)

<root>
  <p>TEXT</p>
  <ul>
    <li>Item1</li>
    <li>Item2</li>
  </ul>
  <p>ANOTHERTEXT</p>  
</root>

XSLT 1.0

<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:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/root">
    <fo:root>
      <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" margin-bottom="1.5in"/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="my-page">
        <fo:flow flow-name="xsl-region-body">
          <xsl:apply-templates/>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

  <xsl:template match="p">
    <fo:block>
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="ul">
    <fo:list-block padding="4pt">
      <xsl:apply-templates/>
    </fo:list-block>
  </xsl:template>

  <xsl:template match="li">
    <fo:list-item>
      <fo:list-item-label end-indent="label-end()">
        <fo:block>&#x02022;</fo:block>
      </fo:list-item-label>
      <fo:list-item-body start-indent="body-start()">
        <fo:block>
          <xsl:apply-templates/>
        </fo:block>
      </fo:list-item-body>
    </fo:list-item>    
  </xsl:template>
</xsl:stylesheet>

XSL-FO 输出

<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" margin-bottom="1.5in"/>
      </fo:simple-page-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="my-page">
      <fo:flow flow-name="xsl-region-body">
         <fo:block>TEXT</fo:block>
         <fo:list-block padding="4pt">
            <fo:list-item>
               <fo:list-item-label end-indent="label-end()">
                  <fo:block>•</fo:block>
               </fo:list-item-label>
               <fo:list-item-body start-indent="body-start()">
                  <fo:block>Item1</fo:block>
               </fo:list-item-body>
            </fo:list-item>
            <fo:list-item>
               <fo:list-item-label end-indent="label-end()">
                  <fo:block>•</fo:block>
               </fo:list-item-label>
               <fo:list-item-body start-indent="body-start()">
                  <fo:block>Item2</fo:block>
               </fo:list-item-body>
            </fo:list-item>
         </fo:list-block>
         <fo:block>ANOTHERTEXT</fo:block>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

Apache FOP 输出

【讨论】:

    【解决方案2】:

    首先,确保您有一个 xhtml 文件(没有
    等等)。然后应用 xslt 转换来创建 fo 文件,然后将其提供给 fop 并出现 pdf。

    xslt fo 样式的片段:

      <xsl:template match="html:body">
        <fo:page-sequence master-reference="all-pages">
          <fo:title>
            <xsl:value-of select="/html:html/html:head/html:title"/>
          </fo:title>
          <fo:static-content flow-name="page-header">
            <fo:block  font-weight="bold" font-size="16pt" space-before.conditionality="retain" xsl:use-attribute-sets="page-header"><!--  space-before="{$page-header-margin}"  -->
              <xsl:if test="$title-print-in-header = 'true'">
                <xsl:value-of select="/html:html/html:head/html:title"/>
              </xsl:if>
            </fo:block>
          </fo:static-content>
        </fo:page-sequence>
      </xsl:template>
    

    检查 http://www.w3schools.com/xslfo/default.asp 的 xslfo 语法。

    有关运行 fop 的信息,请参阅 http://xmlgraphics.apache.org/fop/trunk/running.html; fop.jar 的下载必须在附近。

    从vba运行例如如下:

    Set shell = CreateObject("WScript.Shell")
        cmd = "java -Dfop.home=" & baseDir & " -cp " & baseDir & "build\fop.jar org.apache.fop.cli.Main -fo " & foName & " -pdf " & pdfName
    Call shell.Run(cmd, vbWindowFrame, True)
    

    (与命令行类似)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-18
      • 1970-01-01
      相关资源
      最近更新 更多