JavaScript – XSL Transformation

JavaScript – XSL Transformation.

XSL Transformation mit dem Browser

parameters

Ein Array der möglichen Parameter des XSLT Templates
returnAsDocument
Boolscher Wert, ob das Ergebnis als valides XML Dokument (true) oder Teilbaum ohne XML Header (false) verarbeitet werden soll
outputCallback
Die Funktion, mit der das Ergebnis verarbeitet werden soll
xslUrl
URL zum XSLT Template
xmlDom
DOM Dokument der zu verarbeitenden XML Daten

Gecko Engine

var xsltProcessor = new XSLTProcessor();
var myXSLHTTPRequest = new XMLHttpRequest();
myXSLHTTPRequest.open("GET", xslUrl, false);
myXSLHTTPRequest.send(null);
xsltProcessor.importStylesheet(myXSLHTTPRequest.responseXML);

xsltProcessor.clearParameters()

if (parameters){
  for (var key in parameters){
    xsltProcessor.setParameter("", key, parameters[key]);
  }
}

if (returnAsDocument) {
  if (typeof(xmlDom.responseXML)!='undefined') {
    outputCallback(xsltProcessor.transformToDocument(
      xmlDom.responseXML)
    );
  } else {
    outputCallback(xsltProcessor.transformToDocument(xmlDom));
  }
} else {
  if (typeof(xmlDom.responseXML)!='undefined') {
    outputCallback(xsltProcessor.transformToFragment(
      xmlDom.responseXML ,document)
    );
  } else {
    outputCallback(xsltProcessor.transformToFragment(
      xmlDom ,
      document)
    );
  }
}

Internet Explorer

var xslDocument = new ActiveXObject(
  'MSXML2.FreeThreadedDOMDocument'
);
xslDocument.async = false;
xslDocument.load(xslUrl);

var xslTemplate = new ActiveXObject("MSXML2.XSLTemplate");
xslTemplate.stylesheet = xslDocument;

xslProcessor = xslTemplate.createProcessor();

if (typeof(xmlDom.responseXML)=='undefined') {
  xslProcessor.input = xmlDom;
} else {
  xslProcessor.input = xmlDom.responseXML;
}

if (parameters){
  for (var key in parameters){
    xslProcessor.addParameter(key, parameters[key], "");
  }
}

if (returnAsDocument){
  xslProcessor.output = new ActiveXObject(
    'MSXML2.DOMDocument'
  );
}

xslProcessor.transform();
outputCallback(xslProcessor.output);
This entry was posted in Code Snippets, XML / XSL. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s