Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help applying XSLT to ebmedded XML within HTML page

0 views
Skip to first unread message

jackpod

unread,
Nov 5, 2009, 8:28:36 AM11/5/09
to
Hi All,

I'm trying to transform xml that is embedded in my html page using
XSLT to display HTML elements. The issue is that somewhere the
transformation strips out some data. If I load the same xml through a
file on my hard-drive, the transformation works fine. Any ideas? Below
is a simplified example of my code.

HTML PAGE:

<html>
...
<a href="javascript:displayResultfromDOM()">Click here</a>
<div id="xmldata">
<car><name>Honda</name><year>1999</year><price>$2000</
price><condition>excellent</condition></car>
</div>
</html>


JAVASCRIPT:
<script>
function displayResultFromDOM(){
xmlT = loadXMLString(document.getElementById
("xmldata").innerHTML);
return applyTransformation(xmlT);
}

function applyTransformation(xmlObj){
xsl=loadXMLDoc("myxsl.xsl");
// code for IE
if (window.ActiveXObject)
{
ex=xmlObj.transformNode(xsl);
document.getElementById("example").innerHTML=ex;
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation &&
document.implementation.createDocument)
{
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xmlObj,document);
document.getElementById("example").appendChild(resultDocument);
}

OUTPUT:
Only a name and condition are included in the output. The
others were removed at time of transformation.


Your help is much appreciated.

Thanks

Martin Honnen

unread,
Nov 5, 2009, 9:15:19 AM11/5/09
to
jackpod wrote:

> I'm trying to transform xml that is embedded in my html page using
> XSLT to display HTML elements. The issue is that somewhere the
> transformation strips out some data. If I load the same xml through a
> file on my hard-drive, the transformation works fine. Any ideas? Below
> is a simplified example of my code.
>
> HTML PAGE:
>
> <html>
> ...
> <a href="javascript:displayResultfromDOM()">Click here</a>
> <div id="xmldata">
> <car><name>Honda</name><year>1999</year><price>$2000</
> price><condition>excellent</condition></car>
> </div>

> Only a name and condition are included in the output. The
> others were removed at time of transformation.

Put the XML into a file served as XML so that it is parsed and treated
as XML. If you put XML into your HTML then the HTML parser parses it and
then innerHTML serializes it back as HTML and not as XML. That way
results are hardly reliable as the case of element or attributes might
be changed from lower case to upper case for instance.
And we would need to see the code of your functions like loadXMLString
and of course of the stylesheet to be able to understand what exactly
goes wrong.
Also tell us which browser you get the wrong result with.

--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/

0 new messages