Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

XSLT and XPath

Lesson 30 of 34 in the free Web Technologies notes on Siksha Sarovar, written by Rohit Jangra.

XSLT and XPath

XPath

XPath (XML Path Language) is a query language for selecting nodes from an XML document.

XPath Syntax

<!-- Sample XML -->
<bookstore>
  <book category="fiction">
    <title>The Great Gatsby</title>
    <author>Fitzgerald</author>
    <price>12.99</price>
  </book>
  <book category="tech">
    <title>Clean Code</title>
    <author>Martin</author>
    <price>35.00</price>
  </book>
</bookstore>

XPath Expressions

ExpressionSelects
/bookstoreRoot element
//bookAll book elements anywhere
/bookstore/bookDirect children
//book[@category='tech']Books with category="tech"
//title/text()Text content of all titles
/bookstore/book[1]First book
/bookstore/book[last()]Last book
//book[price>20]Books with price > 20
//@categoryAll category attributes
//book[contains(title,'Code')]Books with "Code" in title

XPath Axes

AxisSelects
child::Direct children
parent::Parent node
ancestor::All ancestors
descendant::All descendants
following-sibling::Following siblings
preceding-sibling::Preceding siblings
attribute::Attributes
self::Current node

XSLT

XSLT (Extensible Stylesheet Language Transformations) transforms XML into HTML, plain text, or other XML formats.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <html>
    <body>
      <h2>Book Catalog</h2>
      <table border="1">
        <tr>
          <th>Title</th>
          <th>Author</th>
          <th>Price</th>
        </tr>
        <xsl:for-each select="bookstore/book">
          <xsl:sort select="price" data-type="number"/>
          <tr>
            <td><xsl:value-of select="title"/></td>
            <td><xsl:value-of select="author"/></td>
            <td><xsl:value-of select="price"/></td>
          </tr>
        </xsl:for-each>
      </table>
    </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

Linking XSLT to XML

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>
<bookstore>
  ...
</bookstore>

Key XSLT Elements

ElementPurpose
<xsl:template>Define transformation rule
<xsl:for-each>Loop over nodes
<xsl:value-of>Output node value
<xsl:if>Conditional output
<xsl:choose>Switch/when/otherwise
<xsl:sort>Sort nodes
<xsl:apply-templates>Apply templates
Key Takeaway: XPath provides a powerful query syntax for navigating XML trees. XSLT uses XPath to transform XML into other formats (HTML, text), making it essential for XML-based reporting and data transformation.