Solved

Client-side XSL in Netscape

Posted on 2004-09-14
1
273 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML template problem 4 66
Get last 10 digits of an xsl value-of field 7 58
[PHP] Hash a XML file/string 3 66
Which XML parser should I used for my requirement 11 64
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now