Friday, December 22, 2023

XSLT Overview

XSLT, or eXtensible Stylesheet Language Transformations, is a language used to transform XML documents into different formats. It is a part of the XML family of technologies and is widely used for various purposes such as converting XML data into HTML, plain text, or other XML formats, as well as for data manipulation and extraction.

Key Components of XSLT:

XML Input Document:

XSLT transformations start with an XML document, which serves as the input data that needs to be transformed.

XSLT Stylesheet:

An XSLT stylesheet is a separate XML document that contains transformation rules written in XSLT. The stylesheet defines how the input XML document should be transformed into the desired output format.

Templates:

XSLT transformations are guided by templates. Templates specify how elements and other nodes in the XML document should be transformed. XSLT processors use these templates to match elements in the input XML and apply the specified transformations.

XPath:

XPath is used within XSLT to navigate XML documents and select nodes for processing. It allows for precise targeting of elements, attributes, and other XML nodes within the XSLT templates.

Output Specification:

XSLT allows you to define the structure and format of the output document. This can be HTML, XML, text, or any other format. XSLT provides elements and attributes to specify the structure of the output document.

Basic Structure of an XSLT Stylesheet:

An XSLT stylesheet typically consists of templates, which define how specific elements or patterns in the input XML document should be transformed. Here's the basic structure of an XSLT stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- Template for matching a specific element -->
  <xsl:template match="elementName">
    <!-- Transformation rules for 'elementName' -->
  </xsl: template>
  <!-- More templates can be defined for different elements or patterns -->
</xsl:stylesheet>

Example of an XSLT Transformation:

Consider the following XML input:

<person>
  <name>John Doe</name>
  <age>30</age>
</person>

An XSLT stylesheet to transform this XML into an HTML paragraph might look like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="person">
    <p>
      Name: <xsl:value-of select="name"/><br/>
      Age: <xsl:value-of select="age"/>
    </p>
  </xsl:template>
</xsl:stylesheet>

In this example, the <xsl: template> element matches the <person> element in the input XML. The <xsl:value-of> elements are used to extract the values of <name> and <age> elements, transforming the data into an HTML paragraph.

XSLT is a powerful tool for transforming and manipulating XML data, allowing for the conversion of complex XML structures into various output formats. It is widely used in web development, data integration, and document processing applications. 

The <xsl:stylesheet> element is the root element of every XSLT language document. 

<xsl:stylesheet
id = identifier+
version =number
xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"
extension-element-prefixes=list-of-prefixes
exclude-result-prefixes=list-of-prefixes>top-level-element*
</xsl:stylesheet>

The attributes in the above syntax are:

id: Represents the name to a stylesheet and identify a stylesheet. This attribute also helps in importing a stylesheet into another XSLT document. This attribute is optional.

version: Specifies the version number of the XSLT.

xmlns:xsl: Enables you to identify the XSLT stylesheet. Within a stylesheet element, the default namespace uri http://www.w3.org/1999/XSL/Transform is used.

extension-element-prefixes: This enables you to identify an extension element. You should separate the value of the elements by white spaces.

Example:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="org.apache.xalan.xslt.extensions.Redirect"
extension-element-prefixes="xalan" version="1.0">

Instead of the <xsl: stylesheet> element, you can also use the <xsl: transform> element to transform the XSLT document. The syntax for the <xsl: transform> element is:

<xsl:transform
    id =identifier
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    extension-elements-prefixes=list-of-prefixes
    exclude-result-prefixes=list-of-prefixes >
    top-level-element*
</xsl:transform> 
  • XSLT is a declarative language, which describes the relationship among variables in terms of functions and rules. 

  • XSLT contains several elements that help select data from an XML document, apply a template to the retrieved data, and generate the required output format. 

  • XSLT is a template-based or rule-based language. 

  • In XSLT, you can obtain an output when the pattern is matched with the data in XML documents.

  •  tag <XSL: for-each-group> enables you to group items by values and group in a sequence. 

The Node Tree

the node tree is a bunch of nodes arranged in a tree. Nodes are a general term for the components of an XML document, such as

Element nodes

Attribute nodes

Text nodes

Comment nodes

Processing-instruction node

the nodes that an element contains are called its children and an element node is its children's parent. Similarly, all the children of an element node are siblings. 

At the very top of the node tree is the root node.

 ELEMENT NODE

  • <xsl:element name={QName} namespace={uri} use-attribute-sets = list-of-Qnames> template body </xsl:element>

    The attributes in the syntax are:

    • name: Specifies the name of an element that has to be created in the resultant document. Qname indicates that the name of the element should be a qualified one.

    • namespace: Contains the Uniform Resource Identifier (URI) specific to the prefix in the Qname of name attribute. If there is no prefix for Qname, the value of the namespace will be null.

    • use-attribute-sets: Indicates the list of attribute names to be used by the element.

    Naming Conventions

    Names are used in several places in XML, the most important of which are element names, attribute names, and entity names. In general, the names that you use in XML only have to follow a few rules:

    • Names can contain letters, digits, hyphens (-), periods (.), colons (:), or underscores (_), but they must start with a letter, colon, or underscore.

    • Names cannot start with xml in any case combination (that is, they can't start with XML or Xml either) as these names are reserved for XML standards from the W3C.

    • Names should only use a colon if they use namespaces, which are ways of indicating the markup language that a particular element or attribute comes from. 

    Valid and Invalid XML Names

    Invalid XML Names

    Valid XML Names

    2nd

    second

    XmlDoc

    Doc

    tv:castlist:member

    tv.castlist.member

Attributes in XML
XML attributes are name-value pairs located within an element's start tag.
You can use either single or double quotes for any particular attribute value.

Like element names, attribute names must be valid XML names. However, unlike elements,
some attributes are built in to XML:
  • xml:lang—Indicates the language of the element, its attributes, and its contents

  • xml:space—Controls whether whitespace is retained (preserve) or dealt with by the application (default)

  • xml:base—Provides the base URI for the element, its attributes, and its contents

  • xml:id—Assigns a unique ID to the element, which can then be used for linking to that element

Other Components of XML

There are three other components of XML documents :

  • Comments

  • Processing instructions

  • CDATA sections

Comments

They start with the characters <!-- and end with the characters -->
Comments can go just about anywhere in the document body, but not inside a start tag or an end tag, nor within an empty element tag. You can even spread a comment over several lines if you wish.

Processing Instructions

 Processing instructions start with <?, are immediately followed by a name and then some content, and end with ?>.

Summary

Processing instructions can control the processing of an XML document.

CDATA Sections

CDATA stands for "character data," and a CDATA section indicates that a piece of text only contains characters, so the meaning of the characters is no longer significant. 

CDATA sections begin with the special series of characters <![CDATA[ and end with the sequence ]]>.

CDATA sections are a way of saving you from having to escape characters using entities.

Summary

The <xsl:value-of> instruction gives the result of evaluating the XPath held in its select attribute as some text in the result document.

The <xsl:value-of> element usually takes an attribute called select. 

qualified name or QName for short—a name that is qualified by the namespace to which the element or attribute belongs. 
 If a qualified name contains a colon (:), then the part before the colon indicates a namespace prefix. The part after the colon is known as the local part or local name of the element. 

 So in the case of <xsl:value-of>, the prefix is xsl and the local part is value-of.

Functions : function()

elements : <element>

variables as $variable.

  • type: Indicates the type of link, such as simple, extended, locator, arc, resource, and title.

  • href: Indicates the location of a Uniform Resource Identifier (URI).

The <xsl:stylesheet> element is the root element of every XSLT language document.

  • <xsl:stylesheet id = identifier+ version =number xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" extension-element-prefixes=list-of-prefixes exclude-result-prefixes=list-of-prefixes>top-level-element* </xsl:stylesheet>

  • The attributes in the above syntax are:

    • id: Represents the name to a stylesheet and identify a stylesheet. This attribute also helps in importing a stylesheet into another XSLT document. This attribute is optional.

    • version: Specifies the version number of the XSLT.

    • xmlns:xsl: Enables you to identify the XSLT stylesheet. Within a stylesheet element the default namespace uri http://www.w3.org/1999/XSL/Transform is used.

    • extension-element-prefixes: This enables you to identify an extension element. You should separate the value of the elements by white spaces.


No comments:

Post a Comment

SOA Overview Part-1

  Middleware It provides a mechanism for the process to interact with other processes running on multiple network machines. Advantages...