5️⃣ 🎨 XSLT Tutorial
Estimated reading: 4 minutes 33 views

🧾 XSLT <xsl:value-of> – Extract and Display XML Content

🧲 Introduction – Why Learn <xsl:value-of>?

One of the most common tasks in XSLT is displaying the content of XML elements or attributes. The <xsl:value-of> element is the primary tool for retrieving and outputting node values during an XML transformation. Whether you’re generating HTML, plain text, or structured data, this element ensures you pull the right content from your XML source.

🎯 In this guide, you’ll learn:

  • What <xsl:value-of> does and how it works
  • How to extract values from elements and attributes
  • Use XPath inside select="" to navigate the XML
  • Examples with text output, concatenation, and formatting

📄 Syntax of <xsl:value-of>

<xsl:value-of select="XPathExpression"/>
  • select – An XPath expression targeting an element, attribute, or function
  • Returns the string value of the node
  • Does not output nested elements—only plain text

📄 Sample XML

<book id="101">
  <title>Learning XSLT</title>
  <author>Jane Doe</author>
  <price currency="USD">499</price>
</book>

🧪 Basic Example – Output Element Text

<p><xsl:value-of select="title"/></p>

✅ Output:

<p>Learning XSLT</p>

🏷️ Example – Output Attribute Value

<p>ID: <xsl:value-of select="@id"/></p>

✅ Output:

<p>ID: 101</p>

💲 Example – Accessing Nested Nodes or Attributes

<p>Price: <xsl:value-of select="price"/> <xsl:value-of select="price/@currency"/></p>

✅ Output:

<p>Price: 499 USD</p>

🧠 Example – Concatenating Values (Using XPath concat())

<p>
  <xsl:value-of select="concat(title, ' by ', author)"/>
</p>

✅ Output:

<p>Learning XSLT by Jane Doe</p>

🔁 Example in a Template Loop

<xsl:for-each select="catalog/book">
  <h3><xsl:value-of select="title"/></h3>
  <p><xsl:value-of select="author"/></p>
</xsl:for-each>

✅ Outputs titles and authors for every book.


⚠️ Difference: <xsl:value-of> vs <xsl:apply-templates>

Feature<xsl:value-of><xsl:apply-templates>
OutputText onlyCan include markup via template rules
Use caseDirect value extractionComplex/recursive processing
Supports recursion❌ No✅ Yes

✅ Best Practices for <xsl:value-of>

  • ✔️ Use for simple text/attribute extraction
  • ✔️ Combine with functions like concat(), substring(), format-number()
  • ✔️ Use select="." to get the current node’s value
  • ❌ Don’t expect it to render XML or HTML elements—use templates for that

📌 Summary – Recap & Next Steps

<xsl:value-of> is the workhorse for extracting text from XML. It’s perfect for outputting element content, attribute values, or combined strings. When you need to inject text into your final output, <xsl:value-of> is your go-to tag.

🔍 Key Takeaways:

  • Use select="XPath" to grab element or attribute text
  • Can be used inside <template>, <for-each>, or conditionals
  • Supports XPath functions like concat() and substring()

⚙️ Real-world relevance: Used in reports, HTML generation, plain-text export, and content assembly.


❓ FAQs – XSLT <xsl:value-of>

❓ What does <xsl:value-of> do?
✅ It extracts and outputs the text value of the node specified in the select attribute.

❓ Can I get an attribute value with <xsl:value-of>?
✅ Yes. Use @attributeName in the select.

❓ Can I use multiple <xsl:value-of> in one line?
✅ Yes. It will concatenate their output unless separated by elements or whitespace.

❓ Can it output HTML?
❌ No. It outputs only text. Use <xsl:apply-templates> to handle elements/markup.

❓ What if the XPath doesn’t match any node?
✅ It returns an empty string.


Share Now :

Leave a Reply

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

Share

XSLT <value-of>

Or Copy Link

CONTENTS
Scroll to Top