XSLT (XSL Transformations) Question:
Download Job Interview Questions and Answers PDF
How to transform an XML into XHTML?
Answer:
Below, I write an example which show you how transform an XML into XHTML.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="html"/>
<xsl:template match="/persons">
<html>
<head>
<title>Test an XML Example</title>
</head>
<body>
<h1>Persons</h1>
<ul>
<xsl:apply-templates select="person">
<xsl:sort select="family-name" />
</xsl:apply-templates>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="person">
<li>
<xsl:value-of select="family-name"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="name"/>
</li>
</xsl:template>
</xsl:stylesheet>
To get output on the XHTML we write like that,
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head> <title>Test an XML Example</title> </head>
<body>
<h1>Persons</h1>
<ul>
<li>gupta, Abhi</li>
<li>jain, sudi</li>
</ul>
</body>
</html>
Example:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="html"/>
<xsl:template match="/persons">
<html>
<head>
<title>Test an XML Example</title>
</head>
<body>
<h1>Persons</h1>
<ul>
<xsl:apply-templates select="person">
<xsl:sort select="family-name" />
</xsl:apply-templates>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="person">
<li>
<xsl:value-of select="family-name"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="name"/>
</li>
</xsl:template>
</xsl:stylesheet>
To get output on the XHTML we write like that,
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head> <title>Test an XML Example</title> </head>
<body>
<h1>Persons</h1>
<ul>
<li>gupta, Abhi</li>
<li>jain, sudi</li>
</ul>
</body>
</html>
Download XSLT Interview Questions And Answers
PDF
Previous Question | Next Question |
How you define template in XSLT? | How to transform an XML document into another XML document? |