问题描述
我意识到我可能会后悔在我的余生中询问这个问题,但是...是否有某种方法可以将 XSLT 应用于 XML 文件 XML 文件没有明确的引用到 XSLT 文件?
I realise that I will probably regret asking about this for the rest of my life, but... Is there some way of applying XSLT to an XML file without the XML file having an explicit reference to the XSLT file?
就我个人而言,我认为 XSLT 的全部要点是您可以对同一个原始 XML 文件应用多种不同的转换,从而从中产生多种不同的结果.但是,如果必须在源 XML 文件中指定转换,那实际上就行不通了.似乎要改变转换,你必须改变底层的原始数据文件,这似乎是错误的......
Personally, I thought the whole point of XSLT is that you can apply several different transformations to the same raw XML file to produce several different results from it. But that doesn't really work if the transformation has to be specified in the source XML file. It seems that to change the transformation, you have to change the underlying raw data file, which just seems wrong...
那么有没有办法创建某种文件,上面写着使用这个 XML 和这个 XSLT 并在浏览器窗口中呈现结果"?
So is there some way to create some sort of file that says "take this XML and this XSLT and render the result in a browser window"?
也许我的问题不清楚.
如果我打开记事本,编写一个 XML 文件,并在其中提及 XSLT 文件的名称,那么当我双击该 XML 文件时,Web 浏览器会应用指定的 XSLT.有什么方法可以说服浏览器在不改变原始 XML 文件的情况下 执行此操作吗?还是我将被迫搜索命令行 XSLT 处理器?
If I open Notepad, write an XML file, and mention the name of an XSLT file within it, then when I double-click the XML file, the web browser applies the specified XSLT. Is there some way I can persuade the browser to do this without altering the original XML file? Or am I going to be forced to search for a command-line XSLT processor?
推荐答案
当然.事实上,XSLT 规范根本不依赖(提及)XML 文件引用 XSLT 样式表来处理它.
因此,许多不同的 XSLT 转换可以处理同一个 XML 文件.
Thus it is possible for the same XML file to be processed by many, different XSLT transformations.
在 XSLT 2.0 及更高版本中,XSLT 转换甚至不需要应用相应的 XML 文档.
In XSLT 2.0 and up it isn't even required for an XSLT transformation to have a corresponding XML document to be applied upon.
如何做到这一点?
简短的回答:这取决于实现——阅读相应的 XSLT 处理器文档(例如,用于 .NET 的 XslCompiledTransform、用于 Saxon 的 Saxonica 等).
The short answer: This is implementation dependent -- read the corresponding XSLT processor documentation (e.g. XslCompiledTransform for .NET, Saxonica for Saxon, ..., etc).
此外,几乎每个 XSLT 处理器都有一个命令行实用程序,用于从控制台窗口调用转换 -- 再次检查相应的文档(msxsl.exe 用于 MSXML,nxslt.exe 用于 XslCompiledTransform,..., 等)
Also, almost every XSLT processor has a command-line utility for invoking the transformation from the console window -- again check the respective documentation (msxsl.exe for MSXML, nxslt.exe for XslCompiledTransform, ..., etc.)
以下是我使用的 XSLT 处理器的一些命令行:
这将调用 MSXML 3 处理器:
This invokes the MSXML 3 processor:
msxsl.exe %xml% %xsl% -o %out% -u '3.0' -t %param[ name="value"]%
这会调用 MSXML 4 处理器:
This invokes the MSXML 4 processor:
msxsl.exe %xml% %xsl% -o %out% -u '4.0' -t %param[ name="value"]%
这将调用 MSXML 6 处理器:
This invokes the MSXML 6 processor:
msxsl.exe %xml% %xsl% -o %out% -u '6.0' -t %param[ name="value"]%
这会调用 .NET XslCompiledTransform:
This invokes .NET XslCompiledTransform:
nxslt2.exe %xml% %xsl% -t -o %out% %param[ name="value"]%
这会为 XSLT 10 调用 AltovaXML (XML-SPY):
This invokes AltovaXML (XML-SPY) for XSLT 10:
AltovaXML.exe -xslt1 %xsl% -in %xml% -out %out%%param[ name="value"]%
这会为 XSLT 2.0 调用 AltovaXML (XML-SPY):
This invokes AltovaXML (XML-SPY) for XSLT 2.0:
AltovaXML.exe -xslt2 %xsl% -in %xml% -out %out%%param[ name="value"]%
这将调用 Saxon 9.x(用于 XSLT 2.0):
This invokes Saxon 9.x (for XSLT 2.0):
java.exe -Xms512M -Xmx512M -jar C:\xml\Parsers\Saxon\Ver.9.1.0.5\J\saxon9.jar -t -repeat:1 -o %out% %xml% %xsl% %param[ name=\"value\"]%
这将调用 XQSharp (XSLT 2.0):
This invokes XQSharp (XSLT 2.0):
XSLT.exe -s %xml% -o %out% -r 1 -t %xsl% %param[ name="value"]%
在以上所有内容中,%xml%
是 XML 文件的路径,%xsl%
是主要 XSLT 文件的路径,%out%
是包含转换输出的文件的路径.
In all of the above, %xml%
is the path to the XML file, %xsl%
is the path to the primary XSLT file, %out%
is the path to the file that will contain the output from the transformation.
%param[ name="value"]%
是 name = value
参数规范的列表,这不是必须使用.
%param[ name="value"]%
is a list of name = value
parameter specifications and this isn't mandatory to use.
这篇关于将 XSLT 应用到 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!