一个关于 XSLT 的初学者问题 [英] a beginner question on XSLT

查看:48
本文介绍了一个关于 XSLT 的初学者问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习 XSLT,现在我正在学习它的一些在线教程,现在我有一个简单的问题:

I just started learning XSLT,now I am following some on-line tutorial of it,and I just have a simple question now:

假设我们有一个原始的 xml 文件,我们是否需要编写一个 XSLT 样式表来配合它,或者我们只是简单地将 xml 文件传递​​到某些软件中,例如 Stylus Studio(Saxon Engine),然后它会完成所有这些自动为我们做事?

suppose we have a original xml file,do we need to write a XSLT Stylesheet to go with it,or do we just simply pass the xml file into some software like Stylus Studio(Saxon Engine),then it will do all these things automatically for us?

对不起,我的错误澄清.我需要将这个 .svg 文件转换为 pdf,我刚刚在开发的开始,所以对第一步真的很困惑.另外,我想知道,如果我的初始输入是 .svg 文件,在开始使用 XSLT 之前,我是否必须将其显式转换为 .xml?

Sorry for the mis clarification.I need to convert this .svg file to a pdf,I am just now at the beginning of the development,so really confused about the first step.Also,I would like to know,if my initial input is a .svg file,do I have to explicitly transform it into an .xml before I can start using XSLT?

提前致谢!

推荐答案

对不起,我的错误澄清.我需要将这个 .svg 文件转换为 pdf,我刚刚在开发的开始,所以对第一步真的很困惑.另外,我想知道,如果我的初始输入是 .svg 文件,在开始使用 XSLT 之前,我是否必须将其显式转换为 .xml?

Sorry for the mis clarification.I need to convert this .svg file to a pdf,I am just now at the beginning of the development,so really confused about the first step.Also,I would like to know,if my initial input is a .svg file,do I have to explicitly transform it into an .xml before I can start using XSLT?

SVG 文件 SVG 命名空间中的 XML 文件.是否需要转换该 XML 取决于您将如何使用它.如果您打算使用 Inkscape(一个 SVG 编辑器)之类的工具进行批量打印,则不会.

An SVG file is an XML file in the SVG namespace. Whether or not you need to transform that XML depends on how you're going to use it. If you were going to do a batch print using something like Inkscape (an SVG editor), you wouldn't.

如果您打算使用 XSL-FO 之类的东西,您会的.@Zoltan Hamori 的回答有点误导.您可以使用 saxon 来执行 XSLT 转换(创建 XSL-FO),但您仍然需要一个 XSL-FO 处理器来从 XSL-FO 创建 PDF.

If you are going to use something like XSL-FO, you would. The answer by @Zoltan Hamori is kind of misleading. You can use saxon to perform an XSLT transform (creating the XSL-FO), but you will still need an XSL-FO processor to create the PDF from the XSL-FO.

Zoltan 提到了 FOP(Apache 格式化对象处理器),但他认为 FOP 和 XSL-FO 是一样的;他们不是.他的代码示例是一个 XSL-FO 表(fo 命名空间中的 XML).您需要使用 FOP、RenderX、Antenna House 等处理器才能从 XSL-FO 创建 PDF.

Zoltan mentions FOP (Apache Formatting Objects Processor), but he makes it sound like FOP and XSL-FO are the same; they're not. His code example is an XSL-FO table (XML in the fo namespace). You would need a processor such as FOP, RenderX, Antenna House, etc. to create the PDF from the XSL-FO.

基本上你需要的是:

  1. XML 输入(这将是您的 SVG 文件)
  2. XSLT 转换以创建 XSL-FO.
  3. 用于从 XSL-FO 创建 PDF 的 XSL-FO 处理器

在学习 XSLT 的同时学习 XSL-FO 会很困难,但我将向您展示两种在 PDF 中输出 SVG 的方法.

Learning the XSL-FO at the same time you're learning XSLT is going to be tough, but I will show you two ways to output an SVG in a PDF.

第一种方法是使用 fo:external-graphic 引用 SVG 文件.

The first way is to reference the SVG file using fo:external-graphic.

第二种方法是使用 fo:instream-foreign-object 将 SVG XML 直接嵌入到 XSL-FO 中.

The second way is to embed the SVG XML directly into the XSL-FO using fo:instream-foreign-object.

由于 XML 输入是 SVG XML,我会选择第二个选项.但是,我不确定这对处理时间有什么影响,哪种方式会更有效.

Since the XML input is the SVG XML, I would go with the second option. However, I'm not sure what effect this has on processing time and which way would be more efficient.

我在下面展示了一个例子.由于我展示了两种输出 SVG 的方式,这将创建一个 2 页的 PDF.每个页面都有 SVG 图形.

I've shown an example below. Since I showed both ways to output an SVG, this will create a 2 page PDF. Each page will have the SVG graphic.

注意事项

  • 为了测试,我使用了 Inkscape 附带的示例 SVG 文件.(我从 XSL-FO 输出中删除了大部分 SVG XML,因为它非常大.)
  • 对于我的 XSLT 处理器,我使用了 Saxon-HE 9.2.0.6.
  • 对于我的 FO 处理器,我使用了 Apache FOP 0.95 版(尽管我更喜欢 RenderX).

还有

  • Saxon-HE 和 Apache FOP 都是免费的.
  • 如果你给我你的电子邮件,我可以向你发送我使用的 SVG 文件以及完整的 XSL-FO 输出.我还可以将创建的 PDF 发送给您.

XSLT 2.0

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

  <!-- 
    This is an "identity" template.
    It copies whatever node from input to the output without changing it. 
    Learn it. Use it. Love it. -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/">
    <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="my-page">
          <fo:region-body/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="my-page">
        <fo:flow flow-name="xsl-region-body">
          <fo:block>
            <!-- This is the first way to output an SVG; by referencing the graphic. -->
            <fo:external-graphic src="test.svg"/>
            <!-- This is the second way to output an SVG; by outputting the SVG XML directly. -->
            <fo:instream-foreign-object>
              <xsl:apply-templates/>
            </fo:instream-foreign-object>
          </fo:block>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

</xsl:stylesheet>

XSL-FO(由 Saxon 根据 SVG 输入和 XSL 样式表创建)

XSL-FO (created by Saxon from SVG input and XSL stylesheet)

<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:svg="http://www.w3.org/2000/svg">
   <fo:layout-master-set>
      <fo:simple-page-master master-name="my-page">
         <fo:region-body/>
      </fo:simple-page-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="my-page">
      <fo:flow flow-name="xsl-region-body">
         <fo:block>
            <fo:external-graphic src="test.svg"/>
            <fo:instream-foreign-object>
              <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
                    version="1.1"
                    width="595.99438"
                    height="491.50516"
                    id="svg2675">
                <!-- CONTENT REMOVED FOR STACKOVERFLOW.COM EXAMPLE -->
              </svg>
            </fo:instream-foreign-object>
         </fo:block>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

希望这会有所帮助.

这篇关于一个关于 XSLT 的初学者问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆