2008-12-11 87 views
6

我试图将xsl嵌入到XML文件中。这样做的原因是创建一个可以移动到不同计算机的单个文件,这将防止需要移动xsl文件。将xsl嵌入XML文件

xsl文件正在创建一个表并从xml获取测试步骤,以及它是否通过或失败,非常简单。
我遇到的问题是,xsl具有javascript,并且在xml在IE中加载时显示它。

当我用IE加载xml文件时,javascript显示在表格的上方,在表格下方显示xml。

这里是我的文档是如何布局的:

<!DOCTYPE doc [ 
<!ATTLIST xsl:stylesheet 
    id ID #REQUIRED> 
]> 

<doc>  

<xsl:stylesheet id="4.1.0" 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:user="http://www.ni.com/TestStand" 
    xmlns:vb_user="http://www.ni.com/TestStand/" > 

<xsl:template match="xsl:stylesheet" /> 
    <xsl:text disable-output-escaping="yes"> 

    <msxsl:script language="vbscript" implements-prefix="vb_user"> 
     option explicit 
     'This function will return the localized decimal point for a decimal number 
     Function GetLocalizedDecimalPoint() 
      dim lDecPoint 
      lDecPoint = Mid(CStr(1.1),2,1) 
      GetLocalizedDecimalPoint = lDecPoint 
     End Function 
    </msxsl:script> 
    <msxsl:script language="javascript" implements-prefix="user"><![CDATA[ 
     // This style sheet will not show tables instead of graphs for arrays of values if 
     // 1. TSGraph control is not installed on the machine 
     // 2. Using the stylesheet in windows XP SP2. Security settings prevent stylesheets from creatign the GraphControl using scripting. 
     //  Refer to the TestStand Readme for more information. 

//more javascript functions 
//code to build table and insert data from the xml 

</xsl:stylesheet> 

<Reports> 
<Report Type='UUT' Title='UUT Report' Link='-1-2008-12-3-10-46-52-713' UUTResult='Failed' StepCount='51'> 

// rest of xml 

</Report> 

</Reports> 
</doc> 

回答

11

Although the W3C XSLT Spec supports embedding an XSLT stylesheet为XML文档,似乎IE和Firefox并不支持这一点。

UPDATE:按照由罗伯特·尼斯特罗杰评论,多年以后,10月份到2014年,这一工程在Firefox 33

然而,有一个很好的选择:嵌入XML文档插入XSLT样式表

下面是一个例子。

包含嵌入XML文档 XSLT样式表:

 
<?xml-stylesheet type="text/xsl" href="myEmbedded.xml"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes"/> 
    <xsl:variable name="vEmbDoc"> 
     <doc> 
      <head></head> 
      <body> 
       <para id="foo">Hello I am foo</para> 
      </body> 
     </doc> 
    </xsl:variable> 
    <xsl:template match="para"> 
     <h1><xsl:value-of select="."/></h1> 
    </xsl:template> 
    <xsl:template match="xsl:template"/> 
</xsl:stylesheet> 

当那朵文件中的IE被打开,通缉的结果是由浏览器显示:

你好,我是富

请注意,它需要包含忽略大部分XSLT指令的模板(在这种情况下,我们忽略了任何<xsl:template>通过简单地没有模板身体。

+1

当您在样式表中嵌入文档时的重要注意事项 - 文件名编码在样式表的第一行。这意味着如果您重命名文件,文档将不再解析。 – Colen 2010-06-15 16:53:48

+0

它在Firefox 33中工作。今天检查。 – 2014-10-30 14:38:27