4

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?

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
Roger V
  • 63
  • 1
  • 5
  • Are you using javascript to do the transform, or is the stylesheet declared in an XML processing instruction? – Flynn1179 Feb 25 '11 at 15:56
  • 2
    Are you testing by loading the file from a URL scheme (http or https) or the file scheme(file://). You may be running into security rules that won't load the files if loaded from the file:// scheme. – Mads Hansen Mar 24 '11 at 20:10

2 Answers2

0

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.

Ryan Williams
  • 1,465
  • 15
  • 19
0

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>
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265