使用XSL样式表创建到XML的链接 [英] Create link to XML with XSL stylesheet
问题描述
我有一个XML文件和3个xsl文件,可以转换相同的xml文件。我想创建一个带三个按钮的主页。每个按钮将重定向到三种变形之一。如何通过特定的转换创建到xml文件的链接。
假设我有:example.xml和t1.xsl,t2.xsl,t3.xsl和index.html以及按钮t1,t2,t3。当我按下t1按钮时,我想获取由t1.xsl转换的XML文件。
从您的描述(主页,...)我推断这一切应该发生在网络上;在这种情况下,答案很可能涉及配置Web服务器的规则,所以这将是一个关于Apache或IIS,或者nginx,Jetty或者其他服务器实际上为您的文档提供服务的问题。
实现目标的方法有很多,这些是我想到的前三或四个。具体地说,我假设你使用的是Apache(很多人都这样做),知道如何查找和编辑Apache配置文件,并且可以调整相关引用以适合你的目录布局。
-
假设您想要的正是@Stefan Hegny认为您不想要的东西。
您节省了三份XML文件。名为
example.1.xml
开头
<?xml -stylesheet href =t1.xsltype =text / xsl?>
<示例>
...
名为
example.2.xml
开始<?xml-stylesheet href =t2.xsltype =text / XSL>?;
<示例>
...
类似地
example.3.xml
首先引用t3.xsl
。
这三个按钮指向这三个文档。
如果
example.xml
仍然在变化,您将需要自动执行更新三个近乎相同的过程只要主文档发生变化,它的副本;我使用Make和sed来完成这些任务。 xml - 每当用户请求URI
example.1.xml
,服务文档example.xml
。 - 每当用户请求URI
example.2.xml
时,运行命令sed -es / t1 .xsl / t2.xsl /< example.xml
并将结果(stdout)发送到客户端。 - 每当用户请求URI
example.3.xml
时,运行命令sed -es / t1 .xsl / t3.xsl /< example.xml
并将结果(stdout)发送到客户端。 -
在浏览器中运行样式表
这个要求是使index.xhtml成为一个XForms文档,适用于支持transform()扩展函数的处理器(例如XSLTFor女士)。文档
example.xml
由xf:instance元素引用,三个按钮调用该实例的三个样式表。他们可能会更新辅助实例,或者他们可能会简单地导致显示xf:switch
中的不同情况。 (如果这对大多数人来说是有意义的,但是您需要更多细节,请提出一个标有XForms的问题;如果它对您没有意义,那么您可能不知道XForms,这不是您描述的目标的最简单路径。)b
$ b有些人会使用Javascript而不是XForms来完成这项任务,但是浏览器在他们希望调用内部XSLT处理器的方式上有很大差异,所以除非你喜欢在JavaScript中浏览器不一致的地方工作,你可能不想那样。
Assuming that what you want is precisely what @Stefan Hegny assumes you do not want.
You save three copies of the XML document. The one named
example.1.xml
begins<?xml-stylesheet href="t1.xsl" type="text/xsl"?> <example> ...
The one named
example.2.xml
begins<?xml-stylesheet href="t2.xsl" type="text/xsl"?> <example> ...
And similarly
example.3.xml
begins with a reference tot3.xsl
.The three buttons point to these three documents.
If
example.xml
is still changing, you will want to automate the process of updating the three near-identical copies of it whenever the master document changes; I use Make and sed for such tasks, myself.Another way to achieve the same thing, with a single copy of
example.xml
Another way to achieve the same effect is to maintain a single copy of
example.xml
, with a reference tot1.xsl
(so it looks like theexample.1.xml
described above), and tell your server- Whenever a user requests the URI
example.1.xml
, serve documentexample.xml
. - Whenever a user requests the URI
example.2.xml
, run the commandsed -e s/t1.xsl/t2.xsl/ < example.xml
and send the result (stdout) to the client. - Whenever a user requests the URI
example.3.xml
, run the commandsed -e s/t1.xsl/t3.xsl/ < example.xml
and send the result (stdout) to the client.
In Apache, I use the Rewrite module to redirect these three URIs to a CGI script which inspects the URI by which it was called and runs the appropriate command.
The three buttons continue to point to the three URIs
example.1.xml
,example.2.xml
, andexample.3.xml
.- Whenever a user requests the URI
Running the stylesheet on the server
If the three displays must work even with browsers that don't support XSLT, then you want to run the stylesheet on the server.
Here, again, I use Rewrite to redirect the URIs to a CGI script, but instead of running sed, the CGI script runs xsltproc, or whatever XSLT processor is available on my server.
Running the stylesheet in the browser
Another way to handle this requirement is to make index.xhtml be an XForms document, suited for a processor which supports the transform() extension function (e.g. XSLTForms). The document
example.xml
is referred to by an xf:instance element, and the three buttons invoke the three stylesheets on that instance. They might update an auxiliary instance, or they might simply cause different cases in anxf:switch
to display. (If this mostly makes sense to you but you need more details, ask a question tagged XForms; if it doesn't make sense to you, then you probably don't know XForms and this is not the simplest path to the goal you describe.)Some people would use Javascript instead of XForms for this task, but browsers vary a lot in how they want their internal XSLT processor to be invoked, so unless you enjoy working around browser inconsistencies in Javascript, you might not want to go that way, either.
实现同样效果的另一种方法是维护 在Apache中,我使用重写模块将这三个URI重定向到一个CGI脚本,该脚本检查它被调用的URI,运行相应的命令。 这三个按钮继续指向三个URI 如果即使浏览器不支持XSLT,三个显示器也必须运行,那么您需要运行样式表在这里,我再次使用Rewrite将URI重定向到CGI脚本,但不是运行sed,CGI脚本运行xsltproc或任何XSLT处理器在我的服务器上可用。 I have an XML file and 3 xsl files that tranform the same xml. I want to create a home page, with three buttons. Each button will redirect to one of three tranformations. How can I create a link to the xml file with a spesific transformation.
Let's say I have : example.xml and t1.xsl, t2.xsl, t3.xsl and index.html with buttons t1, t2, t3. When I press the t1 button I want to get the XML file transformed by t1.xsl. From your description ("home page, ...") I infer that all this should happen on the Web; in that case the answer will most likely involve the rules for configuring your Web server, so it's going to be a question about Apache, or IIS, or nginx, or Jetty, or whatever server is actually serving your documents. There are many ways to achieve the goal; these are the first three or four that occur to me. For concreteness I will assume you are using Apache (many people do), know how to find and edit Apache configuration files, and can adapt relative references to suit your directory layout. 这篇关于使用XSL样式表创建到XML的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! example.xml $ c的单个副本$ c>,引用
t1.xsl
(所以它看起来像上面描述的 example.1.xml
) ),并告诉你的服务器
example.1.xml
, example.2.xml
和 example.3.xml
。
在服务器上运行样式表