XSLT document() usage with WebKit browsers

房东的猫 提交于 2019-12-19 08:57:15

问题


I'm having an issue when attempting to include and access multiple XML documents in an XSL stylesheet. I'm assigning document nodes as variables and then attempting to access them in my xsl:template, similar to this:

<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>

I get the correct count when using IE & Firefox, however any WebKit browser (Safari, Chrome) gives me a count of 0. Any thoughts?


回答1:


Chrome has a cross-origin-policy that prevents includes of local files. The document function can only reference itself locally:

<?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 have decided that allowing the .xml file to read the .xlst file off a file:// URL is a security hole and they have blocked it.

The chrome.exe command line option --allow-file-access-from-files will circumvent this safeguard.



来源:https://stackoverflow.com/questions/1868699/xslt-document-usage-with-webkit-browsers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!