如何将 XML 文件与 XSLT 文件链接起来?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3456697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to link up XML file with XSLT file?
提问by nonopolarity
The examples at
示例在
http://en.wikipedia.org/wiki/XSLT
http://en.wikipedia.org/wiki/XSLT
or
或者
http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog
http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog
seem to be independent XML and XSLT files. Don't they have to be linked? Or do you somehow put them into a same file? Otherwise, how does one file know how to suck in data from the other file?
似乎是独立的 XML 和 XSLT 文件。他们不需要链接吗?或者你以某种方式将它们放入同一个文件中?否则,一个文件如何知道如何从另一个文件中吸收数据?
回答by user414661
You can add this after the xml declaration
您可以在 xml 声明之后添加它
<?xml-stylesheet type="text/xsl" href="yourxsl.xsl"?>
回答by Jon Hanna
One file doesn't know to "suck in data" the other file, because the files aren't what will do the processing.
一个文件不知道“吸入数据”另一个文件,因为这些文件不会进行处理。
Some sort of XSLT processor will do this, and the way it will be told what to work on varies so it can handle different use cases.
某种 XSLT 处理器将执行此操作,并且它被告知要处理的内容的方式各不相同,因此它可以处理不同的用例。
In the case of rendering the entire transform of an XML document when it is displayed in a browser, then processing-instruction:
如果在浏览器中显示 XML 文档时呈现整个转换,则处理指令:
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?>
(Really it should have been "text/xml" for the type as that's the mime-type of an XSL document, but this was in the tail-end of the browser wars and browser feature implementation was still often happening faster than the speed of common-sense).
(实际上它应该是“text/xml”类型,因为它是 XSL 文档的 mime 类型,但这是浏览器War的尾声,浏览器功能实现的速度仍然经常比常识)。
If you are controlling the transform programatically using a library of some sort (there are objects for client-side javascript and libraries in any language you're likely to want to do this from), then you've got enough control to detail what gets transformed by what. Some interesting cases here include.
如果您使用某种库以编程方式控制转换(客户端 javascript 的对象和您可能想要使用的任何语言的库),那么您就有足够的控制权来详细说明得到的内容由什么转化。这里的一些有趣案例包括。
You could even have a document with a node of content and a node of transforms, pick them out and run the transform.
If you are running the same transformation on multiple XML documents, it is very often more efficient to call some sort of "PreCompile()" method or similar, which takes a hit on that call to benefit all the subsequent transforms.
You can pass in values to top-level parameters in the XSLT.
您甚至可以拥有一个包含内容节点和转换节点的文档,将它们挑选出来并运行转换。
如果您在多个 XML 文档上运行相同的转换,则调用某种“PreCompile()”方法或类似方法通常会更有效,这会对该调用产生影响,从而使所有后续转换受益。
您可以将值传递给 XSLT 中的顶级参数。
回答by Ido Weinstein
You can also make the transformation in an html page:
您还可以在 html 页面中进行转换:
<script type="text/javascript">
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("some_xml.xml")
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("some_xsl.xsl")
document.write(xml.transformNode(xsl))
</script>
回答by Justin
You need an external tool or library to apply an Xslt transform to Xml. How you do this depends on your programming environment, however for .Net the XslCompiledTransformis the class used to apply an Xslt transform to a piece of Xml (either a file or Xml in memory).
您需要一个外部工具或库来将 Xslt 转换应用到 Xml。如何执行此操作取决于您的编程环境,但是对于 .Net,XslCompiledTransform是用于将 Xslt 转换应用于一段 Xml(文件或内存中的 Xml)的类。
Alternative you can use the Microsoft command line tool xslt.exe- you will need to research for yourself how to do the same thing in other programming languages / operating systems.
或者,您可以使用 Microsoft 命令行工具xslt.exe- 您需要自己研究如何在其他编程语言/操作系统中执行相同的操作。

