0% found this document useful (0 votes)
10 views

XML Library

The document provides a simple example of using XML, XSLT, and Java to transform an XML document into HTML. It includes the XML and XSLT code to define a book catalog, and Java code to apply the XSLT transformation to generate an HTML file from the XML.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

XML Library

The document provides a simple example of using XML, XSLT, and Java to transform an XML document into HTML. It includes the XML and XSLT code to define a book catalog, and Java code to apply the XSLT transformation to generate an HTML file from the XML.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Sure, here's a simple example of XML with the structure you specified and a basic XSLT transformation:

XML (library.xml):

```xml

<?xml version="1.0" encoding="UTF-8"?>

<Library>

<Book>

<Title>Book Title</Title>

<Author>Author Name</Author>

<Year>2023</Year>

<Publishers>Publisher Name</Publishers>

</Book>

</Library>

```

XSLT (transform.xslt):

```xml

<?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>Library Catalog</h2>

<table border="1">

<tr>

<th>Title</th>

<th>Author</th>
<th>Year</th>

<th>Publisher</th>

</tr>

<xsl:for-each select="Library/Book">

<tr>

<td><xsl:value-of select="Title"/></td>

<td><xsl:value-of select="Author"/></td>

<td><xsl:value-of select="Year"/></td>

<td><xsl:value-of select="Publishers"/></td>

</tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

```

A simple Java program that uses the javax.xml.transform library to apply the XSLT transformation:

```java

Import javax.xml.transform.Transformer;

Import javax.xml.transform.TransformerFactory;

Import javax.xml.transform.stream.StreamResult;

Import javax.xml.transform.stream.StreamSource;

Public class XmlToHtmlTransformer {

Public static void main(String[] args) {

Try {
// Input XML file

String xmlInput = “path/to/library.xml”;

// XSLT file

String xsltFile = “path/to/transform.xslt”;

// Output HTML file

String htmlOutput = “path/to/output.html”;

// Create TransformerFactory

TransformerFactory transformerFactory = TransformerFactory.newInstance();

// Create Transformer

Transformer transformer = transformerFactory.newTransformer(new StreamSource(xsltFile));

// Apply transformation

Transformer.transform(new StreamSource(xmlInput), new StreamResult(htmlOutput));

System.out.println(“Transformation successful. Output written to: “ + htmlOutput);

} catch (Exception e) {

e.printStackTrace();

```
Replace “path/to/library.xml”, “path/to/transform.xslt”, and “path/to/output.html” with the actual paths
for your files. Run this Java program, and it will generate an HTML file based on the specified XSLT
transformation.

You might also like