Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>However, during XLST, the <code>&lt;i&gt;</code> tag gets ignored, so "Star Wars" is not italicized in the HTML output. Is there a relatively simple way to fix this?</p> </blockquote> <p><strong>Your problem is here</strong>:</p> <blockquote> <pre><code>&lt;ul&gt; &lt;xsl:for-each select="favoriteMovies/favoriteMovie"&gt; &lt;li&gt;&lt;xsl:value-of select="."/&gt;&lt;/li&gt; &lt;/xsl:for-each&gt; &lt;/ul&gt; </code></pre> </blockquote> <p>The <strong><a href="http://www.w3.org/TR/1999/REC-xslt-19991116#value-of" rel="noreferrer"><code>&lt;xsl:value-of&gt;</code></a></strong> 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 <code>select</code> attribute of this XSLT instruction. The string value of an element is the concatenation of all its text-node descendents.</p> <p>So this is how you get the reported output.</p> <p><strong>Solution</strong>:</p> <p><strong>Use the <a href="http://www.w3.org/TR/1999/REC-xslt-19991116#copy-of" rel="noreferrer"><code>&lt;xsl:copy-of&gt;</code></a> instruction</strong>, which copies all the nodes that are specified in its <code>select</code> attribute:</p> <pre><code>&lt;ul&gt; &lt;xsl:for-each select="favoriteMovies/favoriteMovie"&gt; &lt;li&gt;&lt;xsl:copy-of select="node()"/&gt;&lt;/li&gt; &lt;/xsl:for-each&gt; &lt;/ul&gt; </code></pre> <p><strong>Another solution, more alligned with the principles of XSLT</strong> avoids using <code>&lt;xsl:for-each&gt;</code> at all:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:template match="/"&gt; &lt;html&gt; &lt;head /&gt; &lt;body&gt; &lt;xsl:apply-templates/&gt; &lt;/body&gt; &lt;/html&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/*"&gt; &lt;ul&gt; &lt;xsl:apply-templates/&gt; &lt;/ul&gt; &lt;/xsl:template&gt; &lt;xsl:template match="favoriteMovie"&gt; &lt;li&gt;&lt;xsl:copy-of select="node()"/&gt;&lt;/li&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When any of the two solutions defined above are applied to the provided XML document</strong>:</p> <pre><code>&lt;favoriteMovies&gt; &lt;favoriteMovie&gt;the &lt;i&gt;Star Wars&lt;/i&gt; saga &lt;/favoriteMovie&gt; &lt;/favoriteMovies&gt; </code></pre> <p><strong>the wanted, correct result is produced</strong>:</p> <pre><code>&lt;html&gt; &lt;head/&gt; &lt;body&gt; &lt;ul&gt; &lt;li&gt;the &lt;i&gt;Star Wars&lt;/i&gt; saga &lt;/li&gt; &lt;/ul&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload