开发者

How to use one data.xml file to store data and multiple xsl stylesheets to view data in a site?

I am just learning xml and am a little confused. It just dawned on me that in order to write a web site with xml I would have to create multiple paired xml and xsl files so that I could link to another page ... make any since? How do I use one .xml file to store the data that I retrieve in multiple xsl stylesheets? Is the paired method the only way?

Breaking news this just in ... I figured out that to have multiple style sheets with one xml file you need to use some javascript !!! go figure. below is the code, you can call this code with an html file which is what you would link to in your menu. Make sure you remove the linking xsl style sheet declaration at the top of the associated xml file !!! And be sure you are using a server, so that appropriate http requests are sent. will not work locally when previewing a file on your hard drive.

<html>
<head>
<script>
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",dname,false);
xhttp.send("");
return xh开发者_JAVA技巧ttp.responseXML;
}

function displayResult()
{
xml=loadXMLDoc("cdcatalog.xml");
xsl=loadXMLDoc("cdcatalog.xsl");
// code for IE
if (window.ActiveXObject)
  {
  ex=xml.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(xml,document);
  document.getElementById("example").appendChild(resultDocument);
  }
}
</script>
</head>
<body onload="displayResult()">
<div id="example" />
</body>
</html>

After some testing I learned that in order to render the page accordingly you will need to add the script information to the head of the html file. The style sheet shows up fine but the scripts need to run before data is entered and I think they are both running at the same time and therefore canceling each other out. ? any thoughts?


There's really only three ways of doing this.

  1. As you've found out, use javascript on the client to transform the xml you've received from the server; download xml. download xsl. execute javascript to transform the xml using the xsl.
  2. Perform the transform on the server - create a handler that performs the above step, but on the server.
  3. Change the stylesheet reference in the xml document on the server on the fly, and serve that to the client.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜