gpt4 book ai didi

xml - 在 HTTP 查询中为 XML 文档指定自定义 XSL 文件

转载 作者:数据小太阳 更新时间:2023-10-29 02:27:18 24 4
gpt4 key购买 nike

我有一个 XML 目录数据和一个 XSL 文件来可视化该目录数据。我使用这一行来验证 XML。

<?xml-stylesheet type="text/xsl" href="presentation-list-catalog.xsl"?>

这部分效果很好。

我想为设计师提供一个 secret 链接,或者我需要以某种方式使用另一个 XSL 文件来验证 XML。基本上我只需要更改指向 XSL 文件的链接:

<?xml-stylesheet type="text/xsl" href="download-links-catalog.xsl"?>

此 XSL 文件是 XML 目录数据的另一种可视化,因此设计人员将能够下载高分辨率目录图片。为此,我想使用相同的 XML,但使用另一个自定义 XSL 文件进行转换。

是否可以使用如下 HTTP 请求指定自定义 XSL 文件:

http://example.com/catalog.xml?download-links-catalog.xsl

有哪些可能的解决方案?

最佳答案

如果您使用的是 PHP,一种解决方案如下:

Have catalog.xml point to a PHP file that serves up the correct XSL file based on the referral URL.

你可以把这个想法移植到其他server-side scripts ,例如 Ruby、ASP、JSP 等。

catalog.xml

catalog.xml 中,不是指向 XSL 文件,而是指向 PHP 文件。在此示例中,PHP 文件为 catalog.php

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="catalog.php"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>

catalog.php

catalog.php 根据引荐 URL 提供正确的 XSL 文件。

<?php
// Output the correct Content-Type, so that browsers know
// to treat this file as an XSL document
header("Content-Type: text/xsl; charset=utf-8");

// Example $referer: http://example.com/catalog.xml?download-links-catalog.xsl
$referer = parse_url($_SERVER['HTTP_REFERER']);

// Example $query: download-links-catalog.xsl
$query = $referer['query'];

// If the file exists, serve up $query.
// If not, serve up the default presentation-list-catalog.xsl.
$xslFile = file_exists($query) ? $query : "presentation-list-catalog.xsl";
echo file_get_contents($xslFile);
?>

为简洁起见,此示例不包括一些安全检查。例如,您应该验证 $query 实际上是一个 XSL 文件。如果不进行此检查,则黑客可以访问您服务器上的任意文件。

presentation-list-catalog.xsl

这个 XSL 文件没有什么奇怪的。请注意,h2 标记中的文本是 Presentation List Catalog

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Presentation List Catalog</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

下载链接目录.xsl

此 XSL 文件与 presentation-list-catalog.xsl 相同,只是 h2 标记中的文本是 下载链接目录 .

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Download Links Catalog</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

期待什么

使用上述设置,导航到 http://example.com/catalog.xml
将使用 presentation-list-catalog.xsl 提供 catalog.xml

导航到 http://example.com/catalog.xml?download-links-catalog.xsl
将使用 download-links-catalog.xsl 提供 catalog.xml

上面的示例 XML 和 XSL 文件取自 W3School 关于 "XSLT - Transformation." 的文章

关于xml - 在 HTTP 查询中为 XML 文档指定自定义 XSL 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10772110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com