XSLT Current Function Tutorial 🎯

beginner
14 min

XSLT Current Function Tutorial 🎯

Welcome to CodeYourCraft's XSLT Current Function Tutorial! In this comprehensive guide, we'll delve into the world of XSLT Current Functions, helping you understand, apply, and master these powerful tools. By the end of this tutorial, you'll be equipped to manipulate XML data like a pro! 📝

What are XSLT Current Functions?

XSLT (eXtensible Stylesheet Language Transformations) is a language for transforming XML documents into other formats such as HTML, PDF, and more. XSLT Current Functions provide context-sensitive information about the current node being processed, enabling powerful navigation and manipulation of the XML data. 💡

XSLT Current Functions Types

XSLT offers several current functions. Here are the most commonly used ones:

  1. current(): Returns the current node being processed.
  2. self::node(): Returns the current node's siblings and descendants, excluding the current node itself.
  3. parent::node(): Returns the current node's parent.
  4. ancestor::node(): Returns the current node's ancestors.
  5. ancestor-or-self::node(): Returns the current node and its ancestors.
  6. descendant::node(): Returns the current node's descendants.
  7. descendant-or-self::node(): Returns the current node and all its descendants.
  8. following-sibling::node(): Returns the current node's following siblings.
  9. preceding-sibling::node(): Returns the current node's preceding siblings.
  10. preceding::node(): Returns the current node's predecessors.

XSLT Current Function Example

Let's see an example to understand how XSLT Current Functions work.

xml
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h1>Books</h1> <ul> <xsl:apply-templates select="bookstore/book"/> </ul> </body> </html> </xsl:template> <xsl:template match="book"> <li><xsl:value-of select="title"/> (<xsl:value-of select="@price"/>) </li> </xsl:template> <xsl:template match="bookstore"> <xsl:value-of select="count(book)"/> books found. </xsl:template> </xsl:stylesheet>

In this example, we have an XML document representing a bookstore with multiple books. We use XSLT Current Functions to transform this XML into an HTML list.

Quick Quiz
Question 1 of 1

What does the XSLT template with the `match="/"` do in the example above?

XSLT Current Function Quiz

Let's test your understanding of XSLT Current Functions with a quick quiz!

  1. Given the following XML:
xml
<root> <parent> <child>1</child> <child>2</child> <child>3</child> </parent> <child>4</child> <child>5</child> </root>

What does self::node() return when applied to the <parent> element?

A: <root>, <child>4, <child>5 B: <parent>, <child>1, <child>2, <child>3 C: <root>, <parent> Correct: B Explanation: self::node() returns the current node and its descendants, excluding the current node itself. In this case, it returns the <parent> element and its children.

Conclusion

Congratulations! You've now mastered the basics of XSLT Current Functions. These powerful tools enable you to navigate and manipulate XML data with ease, making your XSLT transformations more efficient and practical.

Keep practicing, and soon you'll be able to tackle real-world projects with confidence! 💡

Remember, learning is a continuous process. Stay curious, keep exploring, and CodeYourCraft will always be here to help you along the way! 🎯