XSLT. O XSL Transformations is a W3C organization standard that presents a way to transform XML documents into other and even non-XML formats. The leaves XSLT style – although the term style sheet is not applied to the direct role of the XSLT – perform the transformation of the document using one or more rules template.
These template rules, together with the source document to be transformed, feed an XSLT processor, which performs the desired transformations by putting the result in an output file, or, as in the case of a web page, does them directly on a presentation device. as the monitor of the user.
Currently, XSLT is widely used in web publishing, generating HTML or XHTML pages. The union of XML and XSLT allows to separate content and presentation, thus increasing productivity.
The XSLT programming language
XSLT (or XSL Transformations according to AAA) is a declarative programming language that allows you to generate documents from XML documents. The XML document is the initial document from which the result will be generated. The XSLT style sheet is the document that contains the source code of the program, that is, the transformation rules to be applied to the initial document.
The XSLT processor is the computer program that applies the transformation rules included in the XSLT style sheet to the initial document and generates the final document. The result of the program execution is a new document (which may or may not be an XML document). XSLT is used to obtain other documents (XML or not) from an XML document. Different XSLT style sheets can be applied to an XML document to obtain different results, and the same XSLT style sheet can be applied to different XML documents.
XSLT stylesheets
XSLT is a declarative language. Therefore, the sheets XSLT style not written as a sequence of instructions, but as a collection of templates (template rules). Each template establishes how a certain element (defined by XPath expressions) is transformed. The transformation of the document is carried out as follows:
- The processor analyzes the document and builds the tree of the document.
- The processor goes through all the nodes from the root node, applying a template to each node, substituting the node for the result.
- When the processor has traversed all nodes, the transformation is complete.
An XSLT stylesheet is an XML document that contains at least the following tags: <? Xml version = “1.0” encoding = “UTF-8″?> <Xsl: stylesheet xmlns: xsl = ” http: //www.w3.org / 1999 / XSL / Transform “version =” 1.0 “> </ xsl: stylesheet>
These tags are:
- the xml <? xml> declaration, typical of any XML document.
- the <xsl: stylesheet> instruction is the root tag of the stylesheet, its attributes indicate the version and the corresponding namespace.
Statement <xsl: stylesheet>
Inside the <xsl: stylesheet> instruction you can find so-called high-level elements and templates, as in the following example: <? Xml version = “1.0” encoding = “UTF-8″?> <Xsl: stylesheet xmlns: xsl = ” http://www.w3.org/1999/XSL/Transform ” version = “1.0”> </ xsl: stylesheet>
These tags are:
- The high-level element <xsl: output> indicates the type of output produced.
- The <xsl: template> statement is a template.
– The match attribute indicates the elements affected by the template and contains an XPath expression. – The content of the instruction defines the transformation to be applied (if the instruction does not contain anything, as in the previous example, it will replace the node with nothing).
XSLT transformation from the XML Editor
The XML Editor allows to associate a sheet XSLT style a document XML, perform the transformation and see the result. The result of the XSLT transformation is displayed in a new document window.
The Results property specifies the file name of the result. If this Results property is blank, a file name is generated in the temporary directory. The file extension is based on the style sheet xsl: output element and can be.xml,.txt, or.htm.
If the Results property specifies a file name with an.htm or.html extension, you can preview the XSLT result using Microsoft Internet Explorer. All other file extensions open with the default editor of Microsoft Visual’s choice. For example, if the file extension is.xml, Visual Studio uses the XML Editor.
Using variables in XSLT
When writing XSLT filters it is possible to use variables as in traditional programming languages, in order to simplify the code and optimize some processes. However, there are some fundamental differences to consider that can complicate the life of the user.
Description
In XSLT, the tag to assign variables is: <xsl: variable name = “name_of_the_variable” select = “expressionXPath” /> Where of course the “expressionXPath” corresponds to the value to be assigned. In this way we can use the variable in the following ways:
- Using <xsl: value-of select = “$ variable_name” />
- Concatenating with, for example, argument values in the HTML to be generated. For instance,
- In any XPath expression always calling it as $ variable_name.
As you can see, the stored value is obtained by calling the name of the variable with the $ sign in front of it. In the case of concatenation, the key is in the curly braces (“{}”) that surround the expression.