Worldscope

XSLT xsl:if

Palavras-chave:

Publicado em: 06/08/2025

XSLT xsl:if: Conditional Processing in Transformations

The xsl:if instruction in XSLT provides a fundamental mechanism for conditional processing of XML data. This article explores the usage of xsl:if, enabling you to create transformations that dynamically adapt to the content of your source XML.

Fundamental Concepts / Prerequisites

To understand xsl:if effectively, you should have a basic understanding of the following:

  • XSLT (Extensible Stylesheet Language Transformations): The core language for transforming XML documents.
  • XPath: A query language used for navigating and selecting nodes in an XML document. The test attribute in xsl:if uses XPath expressions.
  • XML (Extensible Markup Language): The data format that XSLT transforms.

Core Implementation

This example demonstrates how to use xsl:if to conditionally output elements based on the value of an attribute in the XML source.


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <products>
      <xsl:apply-templates select="products/product"/>
    </products>
  </xsl:template>

  <xsl:template match="product">
    <xsl:if test="@discount > 0">
      <discounted_product>
        <name><xsl:value-of select="name"/></name>
        <price><xsl:value-of select="price"/></price>
        <discount><xsl:value-of select="@discount"/></discount>
      </discounted_product>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

<!-- Example XML Input: -->
<products>
  <product discount="10">
    <name>Laptop</name>
    <price>1200</price>
  </product>
  <product discount="0">
    <name>Mouse</name>
    <price>25</price>
  </product>
  <product discount="5">
    <name>Keyboard</name>
    <price>75</price>
  </product>
</products>

<!-- Expected XML Output: -->
<products>
  <discounted_product>
    <name>Laptop</name>
    <price>1200</price>
    <discount>10</discount>
  </discounted_product>
  <discounted_product>
    <name>Keyboard</name>
    <price>75</price>
    <discount>5</discount>
  </discounted_product>
</products>

Code Explanation

The XSLT stylesheet processes an XML document containing a list of products. The <xsl:template match="/"> matches the root node of the XML document and initiates the transformation.

The <xsl:apply-templates select="products/product"/> instruction applies a template to each product element found within the products element.

The <xsl:template match="product"> matches each product element. The core logic lies within the <xsl:if test="@discount > 0"> instruction. This instruction evaluates the XPath expression @discount > 0. If the value of the discount attribute of the current product element is greater than 0, then the code block within the xsl:if instruction is executed. This block creates a discounted_product element containing the product's name, price, and discount.

If the discount attribute is not greater than 0, the discounted_product element is not created for that product.

Complexity Analysis

The time complexity of processing an xsl:if instruction is primarily determined by the complexity of the XPath expression in its test attribute. Assuming the XPath expression evaluates in constant time O(1), then each xsl:if takes O(1) time. If there are 'n' product elements, the `xsl:apply-templates` executes n times. Therefore, the overall time complexity is O(n), where n is the number of product elements.

The space complexity depends on the size of the generated output. In the worst-case scenario (when the condition in xsl:if is always true), the size of the output will be proportional to the size of the input, resulting in a space complexity of O(n), where n is the number of product elements.

Alternative Approaches

While xsl:if is suitable for simple conditional logic, xsl:choose provides a more structured way to handle multiple mutually exclusive conditions. xsl:choose, xsl:when, and xsl:otherwise work similarly to an "if-else if-else" statement in procedural languages. Using xsl:choose can improve readability and maintainability when dealing with complex conditional logic, but it might introduce slightly more overhead than a simple xsl:if in very basic scenarios.

Conclusion

The xsl:if instruction is a powerful and essential tool for implementing conditional logic in XSLT transformations. By understanding how to use xsl:if with XPath expressions, you can create dynamic and flexible transformations that effectively adapt to the content of your source XML documents.