JavaScript JavaScript and XML — “XSLT” Points for Attention

Time:2019-9-10

XSTL in IE

It’s not a formal specification, it’s another manifestation of XPath. IE was the first to support it.

Simple XSTL Conversion

The way XML documents are added to a DOM document, and then used

  • transformNode()

Method.

This method takes a parameter, the document containing the XSTL stylesheet. After calling this method, a string containing conversion information is returned.

xmldom.load("employees.xml");
xstldom.load("employees.xstl");
var result = xmldom.transformNodes(xstldom);

This transformNode parameter can be a DOM node of an XML document. If transformNode is not invoked on the document element, then the transformation starts on the calling node.

Complex XSLT Conversion

To use complex XSTL transformations, XSL templates and XSL processors must be used. First, load the XSTL stylesheet into a thread-safe XML document.

function createThreadSafeDocument() {
    if (typeof arguments.callee.activeXString != "string") {
        var versions = ["MSXML2.FreeThreadedDOMDocument.6.0", "MSXML2.FreeThreadedDOMDocument.3.0", "MSXML2.FreeThreadedDOMDocument"],
            i, len;
        for (i = 0, len = versions.length; i < len; i++) {
            try {
                new ActiveXObject(versions[i]);
                arguments.callee.activeXString = versions[i];
                break;
            } catch (ex) {
                //skip   
            }
        }
    }
    return new ActiveXObject(arguments.callee.activeXString);
}

Create XSTL templates:

function createXSTLTemplate() {
    if (typeof arguments.callee.activeXString != "string") {
        var versions = ["MSXML2.FreeThreadedDOMDocument.6.0", "MSXML2.FreeThreadedDOMDocument.3.0", "MSXML2.FreeThreadedDOMDocument"],
            i, len;
        for (i = 0, len = versions.length; i < len; i++) {
            try {
                new ActiveXObject(versions[i]);
                arguments.callee.activeXString = versions[i];
                break;
            } catch (ex) {
                //skip    
            }
        }
    }
    return new ActiveXObject(arguments.callee.activeXString);
}

The following is an example of use:

var xsltdom = createThreadSafeDocument();
xstldom.async = false;
xsltdom.load("employees.xslt");

var template = createXSTLTemplate();
template.stylesheet = xstldom;

var processor = tempalte.createProcessor();
processor.input = xmldom;
processor.transform();
var result = processor.output;

After the XSL processor is created, the node to be converted must be assigned to the input attribute. This value can be either a document or any node of the document. The transformation method is then called to execute the transformation and save the result as a string in the output attribute. These codes implement the same functionality as transformNode. The use of XSL processors allows for more control over the transformation, while also supporting more advanced XSTL features.

To assign parameters in the stylesheet, you can use the addParaneter () method, which receives two parameters: the name of the parameter to be set and the value to be specified.

processor.input = xmldom.documentElement;
processor.addParameter("message", "Hello wrold!");
processor.transform();

Another advanced feature of XSL processors is the ability to set an operating mode. In XSTL, you can use the model feature to define a pattern for a template, and if you don’t use < xsl: apply – tempaltes > with the matching model feature, you won’t run the template. If the mode of < xsl: apply – tempaltes > element is set to “title – first”, then when using javascript, you need to set it as follows:

processor.input = xmldom;
processor.addParameter("message", "Hello wrold!");
processor.setStartMode("title-first");
processor.transform();

If you plan to use an XSTL stylesheet for multiple transformations, you can call reset () method to reset the processor after each transformation.

processor.reset();

Because the processor has compiled the XSTL stylesheet, this conversion is faster than transformNode ().

XSLTProcessor type

Mozilla creates a new type through Firefox, which other non-IE browsers have learned from and implemented. The main function is to convert XSLT into XML documents.

The first step is to load DOM documents, one based on XML and the other based on XSLT. Then create a new XSLTProceesor object and specify an XSLT for it using the importStylesheet () method. Examples:

var processor = new XSLTProcessor();
processor.importStylesheet(xsltdom);

The last step is to perform the transformation. There are two different ways to do this step: if you want to return a complete DOM document, you can call it

  • transformToDocument()

If you want to return a document fragment, you can use

  • transformToFragment()

The parameter of transformToDocument () is an XML DOM. TransfToFragment () has two parameters, the first is the XML DOM, and the second is the document with the fragment.

  • setParameter()The method can be used to set the parameters of XSLT. The method receives three parameters: namespace URI, the internal name of the parameters and the values to be set. This method must be invoked before the final transformation.

  • getParameter()Method is used to get the value of the current parameter, which is the namespace URI and the internal name of the parameter.

  • removeParameter()Method is used to remove the value of the current parameter, which is the namespace URI and the internal name of the parameter.

The latter two methods are not commonly used. They are provided for convenience only.

The way to reset the processor isreset()Method.

Using XSLT across browsers

function transform(context, xslt) {
    if (typeof XSLTProcessor != "undefined") {
        var processor = new XSLTProcessor();
        processor.importStylesheet(xslt);
        var result = processor.transformToDocument(context);
        return (new XMLSerializer()).serializeToString(result);
    } else if (typeof context.transformNode != "undefined") {
        return caontext.transformNode(xslt);
    } else {
        throw new Error("No XSLT processor available.");
    }
}

This function takes two parameters: the context node to perform the transformation and the XSLT document object. Returns the serialized string.


var result = transform(xmldom, xsltdom);

Recommended Today

Hadoop MapReduce Spark Configuration Item

Scope of application The configuration items covered in this article are mainly for Hadoop 2.x and Spark 2.x. MapReduce Official documents https://hadoop.apache.org/doc…Lower left corner: mapred-default.xml Examples of configuration items name value description mapreduce.job.reduce.slowstart.completedmaps 0.05 Resource requests for Reduce Task will not be made until the percentage of Map Task completed reaches that value. mapreduce.output.fileoutputformat.compress false […]