Sunday, May 18, 2025

XSLT Parameters

 


The <xsl:param> element in XSLT (Extensible Stylesheet Language Transformations) is used to define parameters that can be passed into templates. Parameters allow you to pass values into templates, making your XSLT transformations more flexible and reusable. Here's how <xsl:param> works in detail:

Basic Syntax:

<xsl:param name="parameterName" select="expression"/>

name Attribute: Specifies the name of the parameter. This is how you refer to the parameter within the template.

select Attribute (Optional): Specifies an XPath expression whose result will be the initial value of the parameter. If not provided, the parameter is initially empty.

Usage:

Defining Parameters:

Parameters are typically defined at the beginning of a template or an XSLT stylesheet.

<xsl:param name="paramName" select="'default value'"/>

In this example, a parameter named paramName is defined with an initial value of 'default value'.

Passing Parameters:

Parameters can be passed into templates using <xsl:with-param> within <xsl:call-template> or <xsl:apply-templates>.

<xsl:call-template name="TemplateName">

    <xsl:with-param name="paramName" select="someValue"/>

</xsl:call-template>

In this example, the paramName parameter is passed with the value of someValue to the template named TemplateName.

Accessing Parameters:

Inside the template, parameters are accessed using the $ symbol followed by the parameter name.

<xsl:template name="TemplateName">

    <xsl:param name="paramName"/>

    <!-- Use $paramName in the template -->

</xsl:template>

In this template, the value of the paramName parameter can be used within the template logic.

Use Cases:

Reusability:

Parameters allow you to reuse templates with different values. For instance, a generic template can be customized by passing different parameters.

Dynamic Logic:

Parameters enable the dynamic adjustment of template logic based on values passed into the template. Different transformations can be applied to the same template based on parameter values.

Configuration:

Parameters can be used for configuration settings, allowing you to adjust the behavior of your transformations without modifying the XSLT code.

<xsl:param name="dateFormat" select="'MM/dd/yyyy'"/>

In this example, the dateFormat parameter can be used throughout the XSLT stylesheet to format dates, and its value can be easily changed without modifying the rest of the code.

Note:

Parameters are local to the template where they are defined. They do not retain values between different template calls or apply-templates calls.

Parameters provide a way to make your XSLT transformations more dynamic and adaptable, allowing you to create versatile stylesheets capable of handling various scenarios.

Parameters

Applying Parameters

Parameters enable you to customize the transformation of XML documents. 
You can use parameters to selectively retrieve data from an XML document.

The <xsl:param> and <xsl:with-param> elements enable you to create and use parameters in an XSLT document.

There are two types of parameters, global and local. 

 If a parameter is declared or created within the <xsl:stylesheet> or <xsl:transform> elements, the scope of the parameter is global.

If the parameter created is within the template body, it is local to that particular template.

The syntax for the <xsl:param> element is:

<xsl:param name=Qname select=Expression as = sequence-type required="yes"> <!-- Content: sequence-constructor </xsl:param>
  • name: Defines the name of the parameter. The name of the parameter needs to be a qualified name, and can have optional namespace prefixes. If the prefix exists, you need to provide the namespace URI.

  • select: Specifies a value for the parameter, which is the default value. You can also assign an expression to this attribute. The result of the expression is the value of the parameter. Most of the expressions return Boolean, number, string, or node-set datatypes.

  • required: Specifies whether the parameter is compulsory or optional. You cannot use this attribute for function parameters. The default value is no.

  • as: Specifies the datatype of the parameter. The value assigned to the parameter is converted to this type using the argument conversion rules.

  • creates a parameter, empname, of the string datatype, and assigns a value, John, to it:<xsl:param name="empname" as="xs:string" > John</xsl:param>

Assigning Parameter Values

You can use the <xsl:with-param> element to assign values to parameters that are declared using the <xsl:param name> element. You can use the <xsl:with-param> element within the <xsl:template>, <xsl:call-template>, <xsl:apply-templates>, <xsl:apply-imports> elements, as required by the document.

The syntax of the <xsl:with-param> element is:

<xsl:with-param name =Qname select=Expression> template body </xsl:with-param>

In the above syntax, the name attribute specifies the parameter name, and the select attribute assigns a value to the parameter.

The name and select attributes of this element are similar to that of the <xsl:param> element. 

 For example, you can use the <xsl:param> and the <xsl:with-param> elements to retrieve and display information in an XML document.



xsl:call-template

 The <xsl:call-template> element in XSLT (Extensible Stylesheet Language Transformations) is used to invoke a named template defined within the XSLT stylesheet. It allows you to reuse a specific piece of XSLT code (a template) at multiple places within your stylesheet. Here's a detailed explanation of how <xsl:call-template> works:


Basic Syntax:

<xsl:call-template name="TemplateName"/>

name Attribute: Specifies the name of the template to be called. The XSLT processor will search for a template with this name in the current stylesheet and execute its content.

Behavior:

Template Invocation:

When <xsl:call-template> is encountered during the XSLT transformation, the processor searches for a template in the stylesheet with the specified name (TemplateName in the example above).

Parameter Passing (Optional):

You can pass parameters to the called template by using <xsl:with-param> elements inside <xsl:call-template>.

<xsl:call-template name="TemplateName">

    <xsl:with-param name="paramName" select="paramValue"/>

</xsl:call-template>

In the called template, you can access the passed parameters using <xsl:param> elements.

<xsl:template name="TemplateName">

    <xsl:param name="paramName"/>

    <!-- Template content using $paramName -->

</xsl:template>


Use Cases:

Reusable Code:

<xsl:call-template> allows you to create reusable code snippets in the form of templates. These templates can then be called wherever needed in the stylesheet.

<xsl:template name="formatDate">

    <xsl:param name="date"/>

    <xsl:value-of select="substring($date, 1, 10)"/>

</xsl:template>

<!-- Usage -->

<xsl:call-template name="formatDate">

    <xsl:with-param name="date" select="someDateNode"/>

</xsl:call-template>

In this example, the formatDate template can be called multiple times to format different date values.

Modularization:

<xsl:call-template> allows you to modularize your XSLT stylesheets. You can divide complex transformations into smaller, manageable templates and call them when needed.


Conditional Logic:

It enables the application of different templates based on conditions.

<xsl:choose>

    <xsl:when test="someCondition">

        <xsl:call-template name="templateForCondition"/>

    </xsl:when>

    <xsl:otherwise>

        <xsl:call-template name="defaultTemplate"/>

    </xsl:otherwise>

</xsl:choose>

Here, the appropriate template is called based on the value of someCondition.

Note:

<xsl:call-template> is often used in conjunction with <xsl:template> and <xsl:param> elements to create flexible and modular stylesheets.

It's essential to ensure that the named template exists in the same XSLT stylesheet where <xsl:call-template> is used; otherwise, an error will occur during the transformation process.

In summary, <xsl:call-template> provides a way to create modular, reusable, and conditionally applicable transformations in XSLT stylesheets, enhancing the maintainability and readability of XSLT code.




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.


SOA Overview Part-1

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