【问题标题】:Ampersand URL parameters in document function文档函数中的和号 URL 参数
【发布时间】:2013-05-28 07:58:52
【问题描述】:

如果我想将文档加载到变量中,我可以使用

<xsl:variable name="var1" select="document('http://www.mySite.com/file1.xml')" />

=> 有效,我可以使用 -

<xsl:value-of select="$var1//myNodeName"/>

但现在我需要使用带参数的 URL。 并且以下测试不起作用 -

<xsl:variable name="var2" select="document('http://www.mySite.com/getfile.cgi?param1=val1&param2=val2')" />

=> 不起作用 - EntityRef: 期待 ';'

<xsl:variable name="var2" select="document('http://www.mySite.com/getfile.cgi?param1=val1&amp;param2=val2')" />

=> 不起作用 - 警告错误的 URL - http://www.mySite.com/getfile.cgi?param1=val1&param2=val2:1: 为什么要添加:1: ???

我的问题: 如何替换 & 实体以使 document() 函数正常工作?


更新: 根据 - http://our.umbraco.org/forum/developers/xslt/5421-Ampersand-in-XSLT 解决方案是将 URL 存储为变量,然后将其输出禁用输出转义。 例如

<xsl:variable name="test" select="http://www.mysite.com/mypage.aspx?param1=1&amp;param2=2" />
<xsl:value-of select="$test" disable-output-escaping="yes" />

但是,如何使用 document() 参数应用此解决方案?


更新 2:

我使用 PHP 解析它 -

<?php
Header('Content-type: text/xml');

// http://php.net/manual/en/xsltprocessor.transformtoxml.php

$xml = new DOMDocument;
$xml->load('http://www.mySite.com/devices.xml');

$xsl = new DOMDocument;
$xsl->load('mergedocs.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

echo $proc->transformToXML($xml);
?>

我得到的错误 [注意 URL 中的 amp]:

XML Parsing Error: junk after document element
Location: http://mySite/myPhpFile.php
Line Number 2, Column 1:<b>Warning</b>:  XSLTProcessor::transformToXml() [<a href='xsltprocessor.transformtoxml'>xsltprocessor.transformtoxml</a>]: 
http://siteIp/cgi-bin/getfile.cgi?p1=v1&amp;p2=v2:1: parser error : Document is empty 
in <b>/home/.../myPhpFile.php</b> on line <b>16</b><br />

【问题讨论】:

  • 您使用的是什么 XSLT 处理器?将&amp;amp; 编码为&amp;amp; 正确的方法,处理器检索到的URL 将是正确的带有val1&amp;param2 的URL。我怀疑:1: 不是URL 的一部分,而是警告消息的一部分,即problem at &lt;url&gt;:&lt;line-number&gt;: &lt;description of problem&gt;。打印的警告信息到底是什么?
  • “解析器错误:文档为空” - 如果您自己检索http://siteIp/cgi-bin/getfile.cgi?p1=v1&amp;p2=v2,您会得到什么?同样,amp; 不一定在 PHP 尝试检索的 URL 中 - 因为错误消息本身就是 HTML,所以消息中的任何 &amp;amp; 字符都需要在错误的位置转义为 &amp;amp;消息被发送到浏览器。
  • 我确实得到了文件。有没有办法使用 disable-output-escaping 和参数来记录功能?
  • 不,即使您不需要 - XSLT 处理器已经在请求正确的 URL(使用 &amp;amp;,而不是 &amp;amp;)。一定有其他东西阻碍了,例如运行 PHP 的服务器上的防火墙规则阻止它获取文件,或者可能是某种内容协商或 cgi 脚本中的用户代理嗅探(如果处理器使用与您的浏览器不同的 Accept 标头)。
  • 你是对的。我收到空文档可能是因为远程站点的权限策略。

标签: xslt


【解决方案1】:

将 & 编码为 &是正确的方法,处理器检索到的 URL 将是具有 val1&param2 的正确 URL。我怀疑 :1: 不是 URL 的一部分,而是警告消息的一部分,即 ::

的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-23
    • 2019-06-19
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 2011-06-22
    相关资源
    最近更新 更多