Subject: document('') and xsl from javascript strings
Posted By: AlexBirch Post Date: 8/29/2006 8:27:40 AM
I seem to be having troubles in IE and firefox (with Sarissa - although the example below uses activex msxml) when creating an xsl dom document from a string and using the following xsl from a variable:

<xsl:variable name="widths" select="document('')/*/x:widths"/>
<x:widths>
    <width>201</width>
    <width>208</width>
    <width>102</width>
</x:widths>    

<xsl:template match="/">
    --[<xsl:value-of select="$widths"/>]--
</xsl:template>

This above code works with an xsl dom document from a file as the browsers know what base-uri to use - however when I do it from a javascript string I think it takes the html document itself as the base-uri (I was getting a malformed xml error saying "</form> does not match starting tag: <input>". This <input> tag is in my html file not in the xsl - I fixed this error by putting closing tags on the input to make it well formed).

When I run the script in a browser I get the following output:
--{ --[]--}--

When I run the script from any xml file with the stylesheet also from a file I get the correct output:
--[ 201 208 102 ]--

When I run a slightly changed xsl in the browser with:
<xsl:variable name="widths" select="document('test.xsl')/*/x:widths"/>
it'll work but that doesn't solve my problem because the dynamic design file will contain the widths. As a last resort I can extract the widths from various places in the design file


question  Does anybody know how to set the base-uri for an xml created from a javascript string, or any other way of getting around the problem?

I need to do it from a string as I'm using xslt to render an xml design document to output another xsl document which I then use to process xml docs from the server.

thanks for your help in advance

Alex






here's my javascript code for IE:

<span style='font-family:courier'>
<html>
<head>
<script type="text/javascript">
var innerText;
if (!document.all)
   innerText = "textContent";
else
   innerText = "innerText";

var xmlString = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="mynamespace">' +
                '<xsl:output method="text"/>' +
                '<xsl:variable name="widths" select="document(\'\')/*/x:widths"/>' +
                '<x:widths>' +
                '<width>201</width>' +
                '<width>208</width>' +
                '<width>102</width>' +
                '</x:widths>' +
                '<xsl:template match="/">' +
                '    --[<xsl:value-of select="$widths"/>]--' +
                '</xsl:template>' +
                '</xsl:stylesheet>';

function parseXml()
{
    var xmlDoc = new ActiveXObject('MSXML2.DOMDocument.4.0');
    xmlDoc.async=false;
    xmlDoc.load("test.xml");

    var xslDoc =new ActiveXObject('MSXML2.FreeThreadedDOMDocument.4.0');
    xslDoc.async = false;
    xslDoc.loadXML(xmlString);

    // create XSLTemplate object and compile stylesheet into it
    var cache = new ActiveXObject("Msxml2.XSLTemplate.4.0");
    cache.stylesheet = xslDoc;

    var proc = cache.createProcessor();

    proc.input = xmlDoc;
    var result = proc.transform();
    
    document.getElementById('content')[innerText]= "--{" + proc.output + "}--";
}
</script>
</head>
<body>
<form>
<input type='button' id="someButton" onClick="parseXml();" value="Change content"/>
<input type='text' name='which' value='1'/>
<div id="content">Content here!</div>
</form>
</body>
</html>
</span>

Reply By: mhkay Reply Date: 8/29/2006 8:47:39 AM
Sorry, I don't know the answer to this one.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

Go to topic 48971

Return to index page 191
Return to index page 190
Return to index page 189
Return to index page 188
Return to index page 187
Return to index page 186
Return to index page 185
Return to index page 184
Return to index page 183
Return to index page 182