关于 XSLT 的初学者问题
我刚刚开始学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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。基本上您需要的是:
在学习 XSLT 的同时学习 XSL-FO 将会很困难,但我将向您展示两种在 PDF 中输出 SVG 的方法。
第一种方法是使用
fo:external-graphic
引用 SVG 文件。第二种方法是使用
fo:instream-foreign-object
将 SVG XML 直接嵌入到 XSL-FO 中。由于 XML 输入是 SVG XML,因此我会选择第二个选项。但是,我不确定这对处理时间有什么影响以及哪种方式更有效。
我在下面展示了一个例子。由于我展示了两种输出 SVG 的方法,因此这将创建一个 2 页的 PDF。每个页面都有 SVG 图形。
注释
此外
XSLT 2.0
XSL-FO(由 Saxon 根据 SVG 输入和 XSL 样式表创建)
希望这会有所帮助。
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:
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
Also
XSLT 2.0
XSL-FO (created by Saxon from SVG input and XSL stylesheet)
Hope this helps.
如果您想将数据转换为PDF,您可以使用XSL:FO/FOP。您需要一个 FOP 模板,它是一个 xsl 文件并且可以转换为 PDF。该 xsl 文件可以引用 XML、图像文件等外部对象,或者您可以使用 freemarker/velocity 将数据放入 XSL 模板中。
您可以找到更多信息:
http://www.treebuilder.de/svg/extentSVG/artikel/tut.html
如果您使用 FOP 标签,您可以引用 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:
I think it is possible to add the svg file to saxon as a parameter
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.