This document discusses XML transformation using XSLT. It provides an overview of XSLT, explaining that XSLT is an XML-based language used to specify rules for transforming one XML document into another document in a different format like HTML or text. It describes the XSLT transformation principle, including how an XSL stylesheet is used to transform the abstract tree representation of an XML document based on a set of transformation rules. Examples are given of transforming an XML document into HTML using an XSL stylesheet and a Java program for performing XSL transformations.
Why Teams call analytics are critical to your entire business
Media IT - XML and XML Transformation (XSLT)
1. Media IT :: Dr Serge Linckels :: http://www.linckels.lu/ :: serge@linckels.lu ::
Faculty of Science, Technology and Communication (FSTC)
Bachelor en informatique (professionnel)
-- Media IT -–
¯_(ツ)_/¯
Unit 9
XML
Transformation
2. Media IT :: Dr Serge Linckels :: http://www.linckels.lu/ :: serge@linckels.lu ::
9. XML Transformation
2
9.1 XSLT overview
9.2 XSLT transformation principle
9.3 XML + XSLT References
9.4 Exercise
3. Media IT :: Dr Serge Linckels :: http://www.linckels.lu/ :: serge@linckels.lu ::
9. XML Transformation
9.1 XSLT overview
3
Picture created by Harald Sack
4. Media IT :: Dr Serge Linckels :: http://www.linckels.lu/ :: serge@linckels.lu ::
9. XML Transformation
9.1 XSLT overview
4
XSLT overview
Extensible Stylesheet Language Transformations (XSLT) is an XML-based language used to
specify rules by which one XML document is transformed into another (XML) document
The resulting document may be XML syntax or another format, such as HTML or plain text
Examples of applications of XSLT:
- convert data between different XML schemas
- convert XML data into HTML or XHTML documents for web pages (e.g., with CSS)
- creating a dynamic web page
- convert into an intermediate XML format that can be converted to PDF documents
Such a transformation is based on the following languages:
- XSLT: specifies the transformation rules
- XSL-FO: describes how to transform layout
- XPath: access to specific parts of an XML document
5. Media IT :: Dr Serge Linckels :: http://www.linckels.lu/ :: serge@linckels.lu ::
9. XML Transformation
9.2 XSLT transformation principle
5
XML
documents
DTD/XML-S
XSL
document 1
XSL
document 2
PDF document
HTML
WML
XSLT
processor
6. Media IT :: Dr Serge Linckels :: http://www.linckels.lu/ :: serge@linckels.lu ::
9. XML Transformation
9.2 XSLT transformation principle
6
XSL stylesheet
tree representation of XML
document
XSL works on the abstract tree representation of the XML document
A set of transformation rules are required in form a an XSLT document, e.g., template (XSL
stylesheet)
output
document
+
The structure tree is browsed and for each node the appropriate template from the XSL
stylesheet is applied
7. Media IT :: Dr Serge Linckels :: http://www.linckels.lu/ :: serge@linckels.lu ::
9. XML Transformation
9.2 XSLT transformation principle
7
<data>
<row num="1">toto 1</row>
<row num="2">toto 2</row>
<row num="3">toto 3</row>
</data>
XML document
<?xml version="1.0" encoding="ISO-8859-1"?>
<html xsl:version="1.0"
xmlns:xsl="http://www.w3.org/
1999/XSL/Transform">
<body>
<h1>Demo</h1>
<xsl:for-each select="row">
<br/>Row:
<xsl:value-of select="@num"/>
- Data:
<xsl:value-of select="."/>
</xsl:for-each>
</body>
</html>
XSL stylesheet
<html><body>
<h1>Demo</h1>
<br/>Row: 1 - Data: toto 1
<br/>Row: 2 - Data: toto 2
<br/>Row: 3 - Data: toto 3
</body></html>
HTML document
XSLT
processor
8. Media IT :: Dr Serge Linckels :: http://www.linckels.lu/ :: serge@linckels.lu ::
9. XML Transformation
9.2 XSLT transformation principle
8
Perform XSL transformation using a stylesheet
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl" ?>
<data>
<row num="1">toto 1</row>
<row num="2">toto 2</row>
<row num="3">toto 3</row>
</data>
9. Media IT :: Dr Serge Linckels :: http://www.linckels.lu/ :: serge@linckels.lu ::
9. XML Transformation
9.2 XSLT transformation principle
9
import java.io.FileReader;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class TestTransformation {
public static void main(String[] args)
throws Exception {
Source sourceXSL = new StreamSource(new FileReader("stylesheet.xsl"));
Source sourceXML = new StreamSource(new FileReader("data.xml"));
TransformerFactory trFac = TransformerFactory.newInstance();
Transformer tf;
Result resultOnScreen = new StreamResult(System.out);
tf = trFac.newTransformer(sourceXSL);
tf.transform(sourceXML, resultOnScreen);
}
}
Perform XSL transformation using a Java programme
10. Media IT :: Dr Serge Linckels :: http://www.linckels.lu/ :: serge@linckels.lu ::
9. XML Transformation
9.3 XML + XSLT References
10
XML in a Nutshell
Elliotte R. Harold, W. Scott Means, W. Scott Means
E-Librarian Service
User-Friendly Semantic Search in Digital Libraries
Serge Linckels, Christoph Meinel
11. Media IT :: Dr Serge Linckels :: http://www.linckels.lu/ :: serge@linckels.lu ::
9. XML Transformation
9.4 Exercise