【问题标题】:XSLT document() usage with WebKit browsersXSLT document() 在 WebKit 浏览器中的使用
【发布时间】:2010-12-24 12:46:13
【问题描述】:

我在尝试在 XSL 样式表中包含和访问多个 XML 文档时遇到问题。我将文档节点分配为变量,然后尝试在我的 xsl:template 中访问它们,类似于:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
  <xsl:output method="xml" omit-xml-declaration="yes" />

  <xsl:variable name="doc1" select="document('test.xml')" />

  <xsl:template match="/">
  <div>
    <span id="id_total">
      <xsl:value-of select="count($doc1//Root)"/>
    </span>
  </div>
  </xsl:template>

</xsl:stylesheet>

我在使用 IE 和 Firefox 时得到了正确的计数,但是任何 WebKit 浏览器(Safari、Chrome)给我的计数都是 0。有什么想法吗?

【问题讨论】:

  • 您是使用javascript进行转换,还是在XML处理指令中声明了样式表?
  • 您是通过从 URL 方案(http 或 https)还是文件方案(file://)加载文件进行测试。如果从 file:// 方案加载,您可能会遇到不会加载文件的安全规则。

标签: xml google-chrome xslt safari cross-domain-policy


【解决方案1】:

Chrome 有一个cross-origin-policy,可以防止包含本地文件。文档函数只能在本地引用自己:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="pitarget.xml"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"
                >
<xsl:variable name="gal" select="'howdy'"/>
<?var gal?><!--howdy-->
<?echo gal?>
<?html5 ?>

<xsl:output method="html" encoding="utf-8" version="" indent="yes" standalone="no" media-type="text/html" omit-xml-declaration="no" doctype-system="about:legacy-compat" />

<xsl:template match="xsl:stylesheet">
  <xsl:apply-templates select="processing-instruction()"/>
</xsl:template>

<xsl:template match="/">
  <xsl:value-of select="processing-instruction('html5')"/>
  <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    </head>
    <body>
      <xsl:apply-templates/>
    </body>
  </html>
</xsl:template>


<xsl:template match="processing-instruction('echo')">
  <xsl:value-of select="//xsl:variable/@select[../@name=current()]"/>
  <xsl:value-of select="count(document('pitarget.xml')//*) - 1"/>
</xsl:template>

<xsl:template match="processing-instruction('var')">
  <xsl:processing-instruction name="{.}">
    <xsl:value-of select="."/>
    <xsl:value-of select="./following-sibling::node()"/>
  </xsl:processing-instruction>
</xsl:template>

</xsl:stylesheet>

【讨论】:

    【解决方案2】:

    Google 已决定允许 .xml 文件从 file:// URL 读取 .xlst 文件是一个安全漏洞,他们已将其阻止。

    chrome.exe 命令行选项 --allow-file-access-from-files 将绕过此保护措施。

    【讨论】:

      猜你喜欢
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      相关资源
      最近更新 更多