将 xsl 嵌入到 XML 文件中 [英] Embed xsl into an XML file

查看:31
本文介绍了将 xsl 嵌入到 XML 文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 xsl 嵌入到 XML 文件中.这样做的原因是创建一个可以移动到不同计算机的单个文件,这样就不需要移动 xsl 文件.

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

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

这是我的文档的布局方式:

]><文档><xsl:stylesheet id="4.1.0"版本=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">选项显式'此函数将返回十进制数的本地化小数点函数 GetLocalizedDecimalPoint()昏暗的 lDecPointlDecPoint = Mid(CStr(1.1),2,1)GetLocalizedDecimalPoint = lDecPoint结束函数</msxsl:script><msxsl:script language="javascript" implements-prefix="user"><![CDATA[//此样式表将不会显示值数组的表格而不是图形,如果//1.机器上没有安装TSGraph控件//2. 在 windows XP SP2 中使用样式表.安全设置防止样式表使用脚本创建 GraphControl.//有关更多信息,请参阅 TestStand 自述文件.//更多javascript函数//建立表格并从xml插入数据的代码</xsl:stylesheet><报告><Report Type='UUT' Title='UUT Report' Link='-1-2008-12-3-10-46-52-713' UUTResult='Failed' StepCount='51'>//其余的 xml</报告></报告></doc>

解决方案

虽然 W3C XSLT 规范支持将 XSLT 样式表嵌入到 XML 文档中,但 IE 和 Firefox 似乎不支持这一点.

更新:根据 Robert Niestroj 的评论,多年后,即 2014 年 10 月,这适用于 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"><文档><头部><身体><para id="foo">你好,我是 foo</para></xsl:变量><xsl:template match="para">

</xsl:模板><xsl:template match="xsl:template"/></xsl:样式表>

在IE中打开tis文件,浏览器显示想要的结果:

你好,我是 foo

请注意,有必要包含忽略大部分 XSLT 指令的模板(在这种情况下,我们忽略任何 没有模板正文.

I'm trying to embed an xsl into a XML file. The reason for doing this is to create a single file that could be moved to different computers, this would prevent the need to move the xsl file.

The xsl file is creating a table and grabbing a test step from the xml and whether it passed or failed, pretty simple.
The issue I'm having, I think, is that the xsl has javascript and its being displayed when the xml is loaded in IE.

When I load the xml file with IE, the javascript is displayed above the table and below the table the xml is displayed.

Here is how my document is laid-out :

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

解决方案

Although the W3C XSLT Spec supports embedding an XSLT stylesheet into an XML document, it seems that IE and Firefox do not support this.

UPDATE: As per the comment by Robert Niestroj, years later, in Oct. 2014, this works in FireFox 33.

However, there is a good alternative: embed the XML document into the XSLT stylesheet.

Below is an example.

An XSLT stylesheet containing an embedded XML document:

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

When tis file is opened in IE, the wanted result is displayed by the browser:

Hello I am foo

Do note, that it is necessary to include templates that ignore most of the XSLT instructions (in this case we are ignoring any <xsl:template> by simply having no template body.

这篇关于将 xsl 嵌入到 XML 文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆