[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Client-side XSL in Netscape

Posted on 2004-09-14
1
Medium Priority
?
287 Views
Last Modified: 2008-02-01
I have an HTML file with an embedded XML island. It contains a reference to an external XSL stylesheet. This works well in IE, but in Netscape the XSL outputs the data elements, but not the HTML. Here is what I have so far.

<html>
<head>
<title>Example 3</title>
<script language="JavaScript">
<!--
function transformIsland(xmlIsland,xslFile) {

if (document.implementation && document.implementation.createDocument) // Netscape
{
var xsltProcessor = new XSLTProcessor();
var myXMLHTTPRequest = new XMLHttpRequest();
myXMLHTTPRequest.open("GET", xslFile, false);
myXMLHTTPRequest.send(null);
var xslRef = myXMLHTTPRequest.responseXML;
xsltProcessor.importStylesheet(xslRef);
var xmlRef = document.implementation.createDocument("", "", null);
var myNode = document.getElementById(xmlIsland);
var clonedNode = xmlRef.importNode(myNode,true);
xmlRef.appendChild(clonedNode);
var fragment = xsltProcessor.transformToFragment(xmlRef, document);
document.getElementById("divResults").appendChild(fragment)              

}
else if (window.ActiveXObject) // Internet Explorer
{
xslRef = new ActiveXObject("Microsoft.XMLDOM");
xslRef.load(xslFile);
var xmlRef = document.getElementById(xmlIsland);
document.write(xmlRef.transformNode(xslRef));

}
else
{
alert('Your browser can\'t handle this script'); // unsupported browser
}

}
-->
</script>
</head>
<body onLoad="transformIsland('xmlExample','Example2.xsl');">
<div id="divResults">
</div>
<xml id="xmlExample" style="visibility:hidden;">
<Article>
<Header>My Article</Header>
<Authors>
<Author>Mr. Foo</Author>
<Author>Mr. Bar</Author>
</Authors>
<Body>This is my article text.</Body>
</Article>
</xml>
</body>
</html>

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="Article">
<html>
<head>
<title>Example 2</title>
</head>
<body>
<table border="1">
<tbody>
<tr>
<th>Article</th>
<th>Authors</th>
<th>Text</th>
</tr>
</tbody>
<tr>
<td>
<xsl:value-of select="Header"/>
</td>
<td>
<xsl:for-each select="Authors/Author">
<xsl:value-of select="."/>
<br/>
</xsl:for-each>
</td>
<td>
<xsl:value-of select="Body"/>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>


0
Comment
Question by:mariita
1 Comment
 
LVL 15

Accepted Solution

by:
dualsoul earned 2000 total points
ID: 12059465
hm...i'll answer to previous questoin, concerning catalog of CDs.

1. when you get embedded xml - your first node is XML, not catalog, so you need update your XSLT or get 1 child node, see code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="JavaScript">
function transformIsland(xmlIsland,xslFile) {

     if (document.implementation && document.implementation.createDocument) // Netscape
          {
          var xsltProcessor = new XSLTProcessor();
         
          // Load the xsl file using synchronous XMLHttpRequest
          var myXMLHTTPRequest = new XMLHttpRequest();
          myXMLHTTPRequest.open("GET", xslFile, false);
          myXMLHTTPRequest.send(null);
          var xslRef = myXMLHTTPRequest.responseXML;
          xsltProcessor.importStylesheet(xslRef);
         
          // create a new XML document in memory
          var xmlRef = document.implementation.createDocument("", "", null);
          var myNode = document.getElementById(xmlIsland);

          var clonedNode = xmlRef.importNode(myNode.childNodes.item(1), true);
         
          xmlRef.appendChild(clonedNode);
//          alert(xmlRef.childNodes.item(0).childNodes.item(1).nodeName);          
         
          // do the transform    
          var fragment = xsltProcessor.transformToFragment(xmlRef, document);
//          alert(fragment.childNodes.item(0).nodeName);
          var divNode=document.getElementById("divResults");
          divNode.innerHTML="";
//          divNode.innerHTML=fragment;
        divNode.appendChild(fragment);            

          }
     else if (window.ActiveXObject) // Internet Explorer
          {
          xslRef = new ActiveXObject("Microsoft.XMLDOM");
          xslRef.load(xslFile);
          var xmlRef = document.getElementById(xmlIsland);
          divResults.innerHTML = xmlRef.transformNode(xslRef);          
         
          }
     else
          {
          alert('Your browser can\'t handle this script'); // unsupported browser
          }

}
</script>    
</head>
<body>

<a href="javascript:transformIsland('XMLData','xslStyle1.xsl');">View 1</a><br>
<a href="javascript:transformIsland('XMLData','xslStyle2.xsl');">View 2</a><br>
<br><br>
<div id="divResults">
</div>

<xml id="XMLData" style="display:none;">
<catalog>
     <cd>
          <title>Empire Burlesque</title>
          <artist>Bob Dylan</artist>
          <country>USA</country>
          <company>Columbia</company>
          <price>10.90</price>
          <year>1985</year>
     </cd>
     <cd>
          <title>Hide your heart</title>
          <artist>Bonnie Tyler</artist>
          <country>UK</country>
          <company>CBS Records</company>
          <price>9.90</price>
          <year>1988</year>
     </cd>
     <cd>
          <title>Greatest Hits</title>
          <artist>Dolly Parton</artist>
          <country>USA</country>
          <company>RCA</company>
          <price>9.90</price>
          <year>1982</year>
     </cd>
</catalog>

</xml>

</body>
</html>

, ok, now we have trouble with <title> element - i think Netscape treats it like html <title> element and therefore doesn't include it in XML data island - so your stylesheet won't show it.

May be you need to read mode about XMLIslands in Netscape at the developers edge.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question