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
|
0 1 2 3 4 5 6 7 8 9 |
<xsl:if test="XPath-condition"> <!-- everything inside here is only output if the condition is true --> <span style="color:red">Low stock!</span> </xsl:if> |
- 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
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<product> <name>Wireless Mouse</name> <stock>45</stock> </product> <product> <name>USB-C Hub</name> <stock>8</stock> </product> |
XSLT
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<xsl:for-each select="product"> <div class="product"> <h3><xsl:value-of select="name"/></h3> <p>Stock: <xsl:value-of select="stock"/></p> <!-- Only show this if stock is low --> <xsl:if test="stock &lt; 10"> <span style="color:red; font-weight:bold;">Low stock – hurry!</span> </xsl:if> </div> </xsl:for-each> |
Result
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<div class="product"> <h3>Wireless Mouse</h3> <p>Stock: 45</p> <!-- nothing extra --> </div> <div class="product"> <h3>USB-C Hub</h3> <p>Stock: 8</p> <span style="color:red; font-weight:bold;">Low stock – hurry!</span> </div> |
Example 2 – Using <xsl:if> with attribute condition
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<xsl:for-each select="product"> <div class="product"> <h3><xsl:value-of select="name"/></h3> <xsl:if test="@category = 'electronics'"> <span style="background:#e0f2fe; padding:2px 8px; border-radius:4px;"> Electronics </span> </xsl:if> <p>₹<xsl:value-of select="price"/></p> </div> </xsl:for-each> |
→ only electronics products get the blue label
Example 3 – Combining multiple conditions
|
0 1 2 3 4 5 6 7 8 9 10 |
<xsl:if test="stock &lt; 10 and number(price) > 1000"> <div style="background:#fee2e2; padding:12px; border-radius:6px;"> <strong>Warning:</strong> Expensive item with very low stock! </div> </xsl:if> |
Common mistake — forgetting parentheses when combining with or / and
Wrong:
|
0 1 2 3 4 5 6 |
<xsl:if test="stock < 10 or price > 1000"> <!-- syntax error --> |
Correct:
|
0 1 2 3 4 5 6 |
<xsl:if test="stock &lt; 10 or number(price) > 1000"> |
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 < 10 | note: use < in XML |
| Greater than | price > 1000 | note: > is okay in attribute, but > 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 < 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
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<xsl:template match="catalog"> <html> <head> <title>Product List</title> <style> .product { margin:16px 0; padding:16px; border:1px solid #d1d5db; border-radius:8px; } .low-stock { color:#dc2626; font-weight:bold; } .out-of-stock { background:#fee2e2; color:#991b1b; padding:8px; border-radius:4px; } .free-shipping { color:#059669; font-weight:bold; } </style> </head> <body> <h1>Current Products</h1> <xsl:for-each select="product"> <div class="product"> <h3><xsl:value-of select="name"/></h3> <p> Price: <strong><xsl:value-of select="price"/> <xsl:value-of select="price/@currency"/></strong> </p> <p> Stock: <xsl:value-of select="stock"/> <xsl:if test="stock &lt; 20"> <span class="low-stock"> (Low stock!)</span> </xsl:if> </p> <xsl:if test="stock = 0"> <div class="out-of-stock">Out of stock – backorder available</div> </xsl:if> <xsl:if test="number(price) > 2000"> <div class="free-shipping">Free shipping eligible!</div> </xsl:if> </div> </xsl:for-each> </body> </html> </xsl:template> |
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 <
|
0 1 2 3 4 5 6 |
<xsl:if test="stock < 10"> <!-- WRONG – invalid XML --> |
Fix Use XML entity:
|
0 1 2 3 4 5 6 |
<xsl:if test="stock &lt; 10"> |
Mistake 2 Forgetting to use number() when comparing numeric values
|
0 1 2 3 4 5 6 |
<xsl:if test="price < 500"> <!-- compares strings: "1499" < "500" is FALSE! --> |
Fix
|
0 1 2 3 4 5 6 |
<xsl:if test="number(price) < 500"> |
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!)
- Show “Free shipping” only if price > 2000
- Show “Low stock!” in orange if stock between 10 and 20
- Show “Out of stock” in red box if stock = 0
- Add “New arrival” badge if year > 2020 (add <year> to products if needed)
- Output “Special offer” if price < 500 and stock > 30
- 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 < 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! 😊
