I am using XSLT to convert XML to HTML. I am having trouble figuring out how to deal with embedded XML nodes for formatting. For example, let's say I have the XML element:
<favoriteMovie>the <i>Star Wars</i> saga</favoriteMovie>
However, during XLST, the <i>
tag gets ignored, so "Star Wars" is not italicized in the HTML output. Is there a relatively simple way to fix this?
test.xml:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.html.xsl"?>
<favoriteMovies>
<favoriteMovie>the <i>Star Wars</i> saga</favoriteMovie>
</favoriteMovies>
test.html.xsl:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes" />
<xsl:template match="/">
<html>
<head />
<body>
<ul>
<xsl:for-each select="favoriteMovies/favoriteMovie">
<li><xsl:value-of select="." /></li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XSLT is used to transform XML document from one form to another form. XSLT uses Xpath to perform matching of nodes to perform these transformation . The result of applying XSLT to XML document could be an another XML document, HTML, text or any another document from technology perspective.
XSLT <xsl:value-of> Element.
The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.
However, during XLST, the
<i>
tag gets ignored, so "Star Wars" is not italicized in the HTML output. Is there a relatively simple way to fix this?
Your problem is here:
<ul> <xsl:for-each select="favoriteMovies/favoriteMovie"> <li><xsl:value-of select="."/></li> </xsl:for-each> </ul>
The <xsl:value-of>
instruction is used to create a text node. In doing so it copies to the output the string value of the XPath expression specified in the select
attribute of this XSLT instruction. The string value of an element is the concatenation of all its text-node descendents.
So this is how you get the reported output.
Solution:
Use the <xsl:copy-of>
instruction, which copies all the nodes that are specified in its select
attribute:
<ul>
<xsl:for-each select="favoriteMovies/favoriteMovie">
<li><xsl:copy-of select="node()"/></li>
</xsl:for-each>
</ul>
Another solution, more alligned with the principles of XSLT avoids using <xsl:for-each>
at all:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<html>
<head />
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="/*">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="favoriteMovie">
<li><xsl:copy-of select="node()"/></li>
</xsl:template>
</xsl:stylesheet>
When any of the two solutions defined above are applied to the provided XML document:
<favoriteMovies>
<favoriteMovie>the
<i>Star Wars</i> saga
</favoriteMovie>
</favoriteMovies>
the wanted, correct result is produced:
<html>
<head/>
<body>
<ul>
<li>the
<i>Star Wars</i> saga
</li>
</ul>
</body>
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With