I am currently trying to get multiple xml files into my xsl file to then process them into useful html. This works fine for non webkit browsers but not for webkit browsers.
I am using JavaScript to perform the transformation and it works fine across all browsers apart from if I want multiple xml files in it.
I have tried various methods to try and get the extra xml files into the xsl, the files are all on the same server but in different folders.
1) I have tried adding the xml files directly as a variable. For example:
<xsl:variable name="structure" select="document('../structure.xml')"/>
I have since found that webkit browsers do not allow this as it’s considered a security risk. But if anyone is aware of any workarounds or different methods to achieve the same result that would be great
2) I have tried passing the xml file through into the processor as a parameter. Eg:
xml=loadXMLDoc("structure.xml);
xsl=loadXMLDoc("xsl/head.xsl");
structurexml =loadXMLDoc("/extranet/structure.xml");
xsltProcessor =new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
xsltProcessor.setParameter(null, "structure", structurexml);
process.transform();
loadXMLDoc is a simple piece of code that loads the xml and xsl files
function loadXMLDoc(fname) {
var isIE = (navigator.appName=="Microsoft Internet Explorer");
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
}
else {
xhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xhttp.open('GET', fname, false);
if (xhttp.overrideMimeType){
xhttp.overrideMimeType('text/xml');
}
xhttp.send(null);
if (isIE) {
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.load(fname);
}
else {
xmlDoc = xhttp.responseXML;
if (!xmlDoc) {
xmlDoc = (new DOMParser()).parseFromString(xhttp.responseText, 'text/xml');
}
}
return xmlDoc;
}
This doesn’t work as webkit doesn’t allow node-set as a parameter. Again any kind of work around would be brilliant
3) I have tried the above but transformed the xml into a string before adding as a parameter. This allows me to get the parameter to be passed through but then I can’t get the string to be transformed back into xml to then process it.
If anyone has any ideas about this that would also be great.
The other option I have thought of is to use JavaScript to merge the xml files into one giant one and process this but I have no idea how to do that.
Also I can’t do server side processing as I do not have control of the server so I am left with only client side.
As I said before I can solve the problem for non webkit browsers. I have been trying to solve this problem for a while now and can’t find any workarounds. I can’t see that I can be the only person with this problem and really hope that someone has had a similar problem and found a workaround or fix for this.
Thanks in advance!
Matt
UPDATE:
I have also thought about adding the xml files together in JavaScript.
xml=loadXMLDoc("structure.xml);
xsl=loadXMLDoc("xsl/head.xsl");
structurexml =loadXMLDoc("/extranet/structure.xml");
newxml = xml;
newxml.appendChild(sessionxml);
But get an error of Uncaught Error: HIERARCHY_REQUEST_ERR: DOM Exception 3 which I believe is due to the fact that there would be no root node in the new xml? Does anyone know how I would be able to successfully add them? The two files are
<?xml version="1.0" encoding="utf-8"?>
<structure>
<product>
<id>1</id>
<unit>
<id>1</id>
<name>Blah</name>
<code>Blah</code>
</unit>
</product>
</structure>
and
<?xml version="1.0" encoding="utf-8"?>
<user>
<emp>true</emp>
<first>Test</first>
<last>User</last>
</user>
Ideally I would like to have the file in the format
<?xml version="1.0" encoding="utf-8"?>
<root>
<structure>
<product>
<id>1</id>
<unit>
<id>1</id>
<name>Blah</name>
<code>Blah</code>
</unit>
</product>
</structure>
<user>
<emp>true</emp>
<first>Test</first>
<last>User</last>
</user>
</root>
Is there a simple way to achieve this in JavaScript or JQuery?
Saxon-CE is an XSLT 2.0 processor (compiled to JavaScript) that will run in webkit and all the major browsers and (naturally) supports the document() function. As well as atomic values it also supports documents, nodes, arrays or even JavaScript objects being passed as parameters.
You could run the transform using code very similar to your own, but using:
however the following JavaScript could also be used
The ‘structure’ parameter is just for illustrative purposes, in your case you just need to use the document() function normally within the XSLT.
I should also mention that with Saxon-CE updates to an HTML document can be done using the standard XSLT 2.0 xsl:result-document instruction. So if you want to update a DIV element with an id of ‘head’ by adding a paragrah you could use: