关于 XSLT 的初学者问题

发布于 2024-09-30 17:25:07 字数 304 浏览 2 评论 0原文

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

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

抱歉错误的澄清。我需要将此 .svg 文件转换为 pdf,我现在才刚刚开始开发,所以对第一步感到非常困惑。另外,我想知道,如果我的初始输入是.svg 文件,是否必须先将其显式转换为 .xml,然后才能开始使用 XSLT?

提前致谢!

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

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?

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?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏别ゝ 2024-10-07 17:25:07

抱歉错误的澄清。我需要将此 .svg 文件转换为 pdf,我现在才刚刚开始开发,所以对第一步感到非常困惑。另外,我想知道,如果我的初始输入是 .svg 文件,我是否必须将其显式转换为 .xml 才能开始使用 XSLT?

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

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

Zoltan 提到了 FOP(Apache 格式化对象处理器),但他听起来好像 FOP 和 XSL-FO 是一样的;他们不是。他的代码示例是一个 XSL-FO 表(fo 命名空间中的 XML)。您需要一个处理器(例如 FOP、RenderX、Antenna House 等)来从 XSL-FO 创建 PDF。

基本上您需要的是:

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

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

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

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

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

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

注释

  • 为了进行测试,我使用了 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 样式表创建)

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

希望这会有所帮助。

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?

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.

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

Basically what you need is:

  1. XML input (this would be your SVG file)
  2. XSLT transform to create the XSL-FO.
  3. XSL-FO processor to create the PDF from the XSL-FO

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.

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

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

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.

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.

Notes

  • For testing, I used an example SVG file that came with Inkscape. (I removed most of the SVG XML from the XSL-FO output because it's pretty big.)
  • For my XSLT processor, I used Saxon-HE 9.2.0.6.
  • For my FO processor, I used Apache FOP Version 0.95 (although I much prefer RenderX).

Also

  • Both Saxon-HE and Apache FOP are free.
  • If you give me your email, I can send you the SVG file I used along with the full XSL-FO output. I can also send you the PDF that was created.

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

Hope this helps.

未央 2024-10-07 17:25:07

如果您想将数据转换为PDF,您可以使用XSL:FO/FOP。您需要一个 FOP 模板,它是一个 xsl 文件并且可以转换为 PDF。该 xsl 文件可以引用 XML、图像文件等外部对象,或者您可以使用 freemarker/velocity 将数据放入 XSL 模板中。

您可以找到更多信息:
http://www.treebuilder.de/svg/extentSVG/artikel/tut.html

如果您使用 FOP 标签,您可以引用 SVG 文件,例如:

   <fo:table text-align="left" table-layout="fixed"
                                      background-image="SVG_file.svg">

我认为可以将 svg 文件作为参数添加到 saxon

If you want to convert data to PDF, you may use XSL:FO/FOP. You need a FOP template which is an xsl file and that can be converted to PDF. This xsl file can reference to XML, external objects like image files or you can use freemarker/velocity to put data into the XSL template.

You can find more info:
http://www.treebuilder.de/svg/extentSVG/artikel/tut.html

If you use FOP tags, you can reference to the SVG file like:

   <fo:table text-align="left" table-layout="fixed"
                                      background-image="SVG_file.svg">

I think it is possible to add the svg file to saxon as a parameter

悲歌长辞 2024-10-07 17:25:07

Svg 是一个 Xml 文件。因此,您可以开始编写自己的 XSLT 样式表,将其转换为所需的格式。

但快速搜索 Google 向我展示了很多可以执行此转换的程序你。

Svg is an Xml-File. So, you could start writing your own XSLT stylesheet to transform it into the desired format.

But a quick search on Google showed me a lot of programs that can do this conversion for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文