Ruby XPATH XSLT
Syntax
XPATH
//element
XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Example
XPATH
//div[@class="example"]
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
</catalog>
Output
XPATH
<div class="example">
<p>Example output.</p>
</div>
XSLT
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<tr>
<td>Empire Burlesque</td>
<td>Bob Dylan</td>
</tr>
<tr>
<td>Hide your heart</td>
<td>Bonnie Tyler</td>
</tr>
<tr>
<td>Greatest Hits</td>
<td>Dolly Parton</td>
</tr>
</table>
</body>
</html>
Explanation
XPATH
The above XPATH expression selects all div elements that have a class attribute with a value of "example".
XSLT
The above XSLT code transforms an XML document into an XHTML table that displays the XML data in a tabular format. The XSLT code uses various elements such as xsl:template, xsl:for-each and xsl:value-of to select and display data from the XML document.
Use
XPATH
XPATH can be used to select specific elements or attributes from an XML document. It is commonly used in web scraping, data mining and data extraction applications.
XSLT
XSLT is used to transform XML documents into various formats such as HTML, XHTML and PDF. It is commonly used in web applications that require the presentation of XML data in a user-friendly format.
Important Points
XPATH
- XPATH is a query language used to select elements and attributes from an XML document.
- XPATH expressions can be used to navigate the hierarchy of an XML document's nodes.
- XPATH is commonly used in web scraping, data mining and data extraction applications.
XSLT
- XSLT is used to transform XML documents into various formats such as HTML, XHTML and PDF.
- XSLT uses a template-based approach to transform an XML document into another format.
- XSLT is commonly used in web applications that require the presentation of XML data in a user-friendly format.
Summary
XPATH and XSLT are two commonly used languages in XML processing. XPATH is used to select specific elements or attributes from an XML document, while XSLT is used to transform XML documents into various formats such as HTML, XHTML and PDF. These two languages are essential tools for web scraping, data mining and data extraction applications, and for web applications that require the presentation of XML data in a user-friendly format.