Chapter 64: XSLT

1. What does <xsl:if> really do? (the clearest explanation)

<xsl:if> is the simplest conditional statement in XSLT.

It lets you say:

“If this condition is true, then output this content. If not — output nothing (there is no else branch).”

It is not like a full if-else statement in JavaScript or Python.

There is no <xsl:else> tag in XSLT 1.0 (the most common version today).

If you need “else” behavior → you use <xsl:choose> (we’ll see that later).

Everyday analogy

Imagine you are packing a lunchbox for your child:

  • If it’s Monday → put an apple
  • If it’s Tuesday → put a banana
  • If it’s any other day → put nothing extra

<xsl:if> is the Monday rule: “If today is Monday → add apple” (If not Monday → do nothing)

2. Basic syntax – the pattern you will see 99% of the time

XML
  • test=”…” → must contain a boolean XPath expression (something that evaluates to true or false)
  • If the test expression returns true → the content inside is processed and output
  • If false → the entire <xsl:if> block is skipped (nothing is output)

3. Realistic examples – from simple to common real patterns

Example 1 – Very basic: show warning only if stock is low

Input XML fragment

XML

XSLT

XML

Result

HTML

Example 2 – Using <xsl:if> with attribute condition

XML

→ only electronics products get the blue label

Example 3 – Combining multiple conditions

XML

Common mistake — forgetting parentheses when combining with or / and

Wrong:

XML

Correct:

XML

Lesson 4 – Important operators & functions used inside test=”…”

Expression type Example inside test=”…” Meaning / when it is true
Equals @inStock = “true” attribute equals string “true”
Not equals @inStock != “true” not equal
Less than stock &lt; 10 note: use &lt; in XML
Greater than price > 1000 note: > is okay in attribute, but &gt; is safer
Numeric comparison number(price) < 500 force string → number conversion
Contains contains(title, “Dad”) title contains substring “Dad”
Starts-with starts-with(title, “The”) title starts with “The”
And stock &lt; 10 and price > 1000 both conditions true
Or category = “electronics” or category = “gadgets” at least one true
Not not(inStock = “true”) inStock is not “true”

Lesson 5 – Full realistic example: Product list with smart conditional messages

XML

Result for each product

  • Wireless Mouse → normal display + “Low stock!” if stock drops
  • Expensive item → gets “Free shipping eligible!”
  • Out of stock item → gets red warning box

Lesson 6 – Common beginner mistakes & how to avoid them

Mistake 1 Writing <xsl:if test=”stock < 10″> instead of &lt;

XML

Fix Use XML entity:

XML

Mistake 2 Forgetting to use number() when comparing numeric values

XML

Fix

XML

Mistake 3 Trying to use <xsl:else> (there is no such element in XSLT 1.0)

Fix Use <xsl:choose> when you need else/elseif behavior

Lesson 7 – Try yourself exercises (do these!)

  1. Show “Free shipping” only if price > 2000
  2. Show “Low stock!” in orange if stock between 10 and 20
  3. Show “Out of stock” in red box if stock = 0
  4. Add “New arrival” badge if year > 2020 (add <year> to products if needed)
  5. Output “Special offer” if price < 500 and stock > 30
  6. Make “VIP item” text appear only for products in category “electronics”

Summary – <xsl:if> Quick Reference

Question / Goal Pattern / Best practice code Important note / trap
Basic syntax <xsl:if test=”condition”> … </xsl:if> No <xsl:else> — use <xsl:choose> for else
Compare numbers number(price) < 500 Without number(), string comparison is used
Compare attributes @inStock = “true” Case-sensitive!
Check if element exists title or count(title) > 0 title evaluates to true if at least one exists
Check if element does not exist not(title) or count(title) = 0 Very common pattern
Combine conditions stock &lt; 10 and number(price) > 1000 Use parentheses when mixing and/or

Would you like to continue with one of these next?

  • <xsl:if> vs <xsl:choose> – when to use which
  • Complex conditions – combining and / or / not with parentheses
  • Checking existence of elements / attributes
  • Real-world patterns — low stock warnings, free shipping messages, VIP badges, status indicators
  • Debugging when <xsl:if> does not trigger or shows wrong content
  • Using <xsl:if> inside <xsl:for-each> vs inside templates

Just tell me which direction feels most useful or interesting for you right now! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *