Forum Moderators: open
However I am looking for a method to wait until the resulting fragment
is appended (el.appendChild(fragment)).
The event-listener subtreeModified does not work. If it would, it would call Cont3().
When using more complicated xml- and xsl-files as in the examples here,
it can be seen that javascript proceeds without waiting for the transformation to finish.
When I am using el1=document.getElementById('x33');
after a call to doTrans() the program returns an error ("el1 has no properties"). If I insert an alert("") before the call to getElementbyId() everything works fine (because of the added time for transformToFragment() which allows it to finish?).
Could perhaps anyone enlighten me?
-------------
exa_proc.html
-------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>exa_proc.html</title>
<script type="text/javascript" src="exa_proc.js"></script>
</head>
<body id="aktbody" onLoad="doTrans();">
<p id="firstp">here firstp</p>
</body>
</html>
-----------
exa_proc.js
-----------
var gelem="firstp"
var gxmlfil="exa_proc.xml";
var gxslfil="exa_proc.xsl";
// --- process gxmlfil by gxslfil, fill gelem with resulting fragment
function doTrans()
{
xslRef=document.implementation.createDocument("", "", null);
xslRef.addEventListener("load", Cont1, false);
xslRef.load(gxslfil);
}
function Cont1()
{
xmlRef=document.implementation.createDocument("", "", null);
xmlRef.addEventListener("load", Cont2, false);
xmlRef.load(gxmlfil);
}
function Cont2()
{
el=document.getElementById(gelem);
el.innerHTML="";
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xslRef);
var fragment=xsltProcessor.transformToFragment(xmlRef, document);
el.addEventListener("subtreeModified", Cont3, false); // doesn't work
el.appendChild(fragment);
}
function Cont3() // --- is not called by appendChild
{
alert("Cont3");
}
------------
exa_proc.xml
------------
<?xml version="1.0" encoding="utf-8"?>
<mylist>
<item number="1"/>
<item number="2"/>
<item number="3"/>
</mylist>
------------
exa_proc.xsl
------------
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:template match="//item">
<xsl:element name='div'>
<xsl:attribute name='id'>x<xsl:value-of select='@number'/>
</xsl:attribute>
<xsl:value-of select='@number'/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>