2009-12-08 49 views
4

我在嘗試在XSL樣式表中包含和訪問多個XML文檔時遇到問題。我指定的變量文檔節點,然後試圖訪問他們在我的xsl:模板,與此類似:使用WebKit瀏覽器的XSLT文檔()使用

<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的計數。有什麼想法?

+0

您是使用javascript來進行轉換,還是在XML處理指令中聲明的樣式表? – Flynn1179 2011-02-25 15:56:58

+2

您是通過從URL方案(http或https)還是文件方案(file://)加載文件來測試的。您可能會遇到安全規則,如果從file://方案加載,將不會加載文件。 – 2011-03-24 20:10:59

回答

0

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

谷歌已經決定,允許.xml文件讀取.xlst文件關閉文件:// URL是一個安全漏洞,他們已經封鎖。

chrome.exe命令行選項--allow-file-access-from-files將繞過此安全措施。