Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed xsl into an XML file

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>
like image 661
Brad8118 Avatar asked Dec 11 '08 19:12

Brad8118


People also ask

How does XSL work with XML?

XSLT is used to transform XML document from one form to another form. XSLT uses Xpath to perform matching of nodes to perform these transformation . The result of applying XSLT to XML document could be an another XML document, HTML, text or any another document from technology perspective.

Does XML utilize XSL?

XSL uses a XML notation, CSS uses its own. In CSS, the formatting object tree is almost the same as the source tree, and inheritance of formatting properties is on the source tree.

Is XML and XSL the same?

XML documents have structure but no format. Extensible Stylesheet Language (XSL) adds formatting to XML documents. XSL provides a way of displaying XML semantics. It can map XML elements into other formatting langauges such as HTML.


1 Answers

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.

like image 111
Dimitre Novatchev Avatar answered Sep 20 '22 00:09

Dimitre Novatchev