Chapter 61: XSLT

1. What does <xsl:value-of> really do?

<xsl:value-of> is the most frequently used instruction in XSLT.

Its only job is:

Take the result of an XPath expression Convert it to a string Output that string into the result document

In other words:

text

It is the XSLT equivalent of:

  • print() in Python
  • echo or <?= in PHP
  • console.log() (but writing to output instead of console)

2. The simplest possible usage

XML

What happens inside the processor?

  1. It looks at the current context node (the node the current template is processing)
  2. It evaluates the XPath expression title
  3. It finds the child element named <title>
  4. It takes all text content inside that element (including descendants)
  5. It writes that text to the output

So if the current node is:

XML

then

XML

outputs exactly:

text

3. The most important attribute: select

Almost every <xsl:value-of> has exactly one important attribute:

XML

The select attribute contains an XPath expression that is evaluated against the current context node.

Very common examples

Current context node is XPath expression What is outputted Explanation
<book> title Atomic Habits child element text
<book> author James Clear child element text
<book> price 499.00 child element text
<price> @currency INR attribute value
<book> @id b1 attribute value
<book> price/@currency INR attribute of child
<book> concat(title, ” by “, author) Atomic Habits by James Clear calculated string
<book> price * 1.18 588.82 (if price is 499) arithmetic result

4. Very common real-life patterns

Pattern 1 – Output element text + attribute

XML

→ Price: 499.00 INR

Pattern 2 – Conditional value

XML

Pattern 3 – Format numbers nicely

XML

→ 1,499.00 (if price was 1499)

Pattern 4 – Output current date/time

XML

→ Generated on: 2025-07-28T14:35:22.123+05:30

Pattern 5 – Safe output (avoid missing elements)

XML

5. Important attributes of <xsl:value-of>

Attribute Purpose Default value Common values / examples
select XPath expression to evaluate — (required) title, price/@currency, count(product)
disable-output-escaping Allow < > & to be output literally no yes (rare, dangerous – XSS risk in HTML)
separator String to put between multiple items (space) , or `

Example with separator

XML

If there are multiple authors:

XML

→ James Clear, Co-author

6. Common beginner mistakes & how to avoid them

Mistake 1 Using select with wrong context

XML

Fix

XML

Mistake 2 Forgetting that textContent includes all descendants

XML

→ outputs all text inside the book (titles + authors + prices concatenated)

Fix Use more specific XPath: book/title

Mistake 3 Trying to output markup with value-of

XML

→ outputs literal <strong>Important</strong> (text, not HTML)

Fix Use literal result elements:

XML

or <xsl:element name=”strong”>

7. Try yourself exercises (do these!)

  1. Output product name followed by price in parentheses
  2. Show price with currency symbol before the number (₹1499.00)
  3. If stock = 0, output “Out of stock” instead of the number
  4. Output “Low stock!” in red if stock < 20
  5. Create a list item <li> for each product with name and price
  6. Show “Free shipping” message if price > 2000

Lesson 8 – Real-world context (where <xsl:value-of> is used every day)

  • e-Invoice / GST → output invoice number, GSTIN, total amount
  • Bank statements → format dates, amounts with currency symbol
  • Product feeds → output name, price, availability
  • Technical documentation → insert section titles, version numbers
  • SOAP response → extract fault codes, error messages
  • Report generation → insert page numbers, current date/time

Would you like to continue with one of these next?

  • Difference between textContent, nodeValue, innerText
  • How to output formatted numbers / dates (format-number, format-date)
  • Safe handling of missing elements / attributes
  • Combining multiple <xsl:value-of> with text and attributes
  • Real-world patterns — invoice number + date, price + currency pair, status messages
  • Debugging when <xsl:value-of> outputs nothing or wrong text

Just tell me which direction you want to go next! 😊

You may also like...

Leave a Reply

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