Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSL/XML: HOw to put html tags in an xml doc so they render

Tags:

html

xml

xslt

I try replacing the tags like this:

<node><br></node> -- >  <node>&lt;br&gt;</node>

unfortunately when the xsl parses the xml file i actually get

<br>

displayed on the page instead of having it displayed as markup.

like image 966
Bogs Avatar asked Dec 01 '25 03:12

Bogs


2 Answers

HTML isn't XML, although they do look very similar; there's four things that are valid in HTML that you can't do with XML, all of which can be modified to be XML compliant:

  • Unclosed tags, as you discovered. Just replace these with a closed version- <br> to <br/> etc.
  • Attributes without values, such as in <input type="checkbox" checked>. Just assign them a value with the same name as the attribute, i.e. <input type="checkbox" checked="checked" />.
  • Mismatched tags- these are a little trickier. For example, it's legal in HTML to do <b>A<i>B</b>C</i>, which would make A bold, C italic, and B both bold and italic. You can make this XML compliant by doing <b>A<i>B</i></b><i>C</i> or <b>A</b><i><b>B</b>C</i>.
  • Most entities. Only &lt;, &gt;, &amp;, &quot;, &apos; and unicode values (e.g. &#160;/&#xA0;) are valid entities in XML. You can't use &nbsp; or &oslash; or anything like that by default. To fix this, you need to include an entity declaration at the top of the sheet, such as <!ENTITY nbsp "&#160;">.

XSLT is incapable of processing an HTML file unless it's also valid XML.

As a rule, I always write HTML to be XML compliant simply because it makes the whole range of XML tools available, and there's really no reason not to.

Replacing <br> with &lt;br&gt; actually replaces the tag with TEXT that happens to resemble html, not an xml compliant tag.

like image 109
Flynn1179 Avatar answered Dec 03 '25 19:12

Flynn1179


If you want to insert a non well-formed html, this is a possible work-around. Put your not well-formed html in a comment inside the xml, then extract it from xsl.

example of XML:

<Data>
  <!--
  <div>
    not well-formed xml<br>
  </div>
 -->
</Data>

example of XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:template match="Data">
    <html>
      <body>
        <xsl:value-of disable-output-escaping="yes" select="comment()"/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="text() | @*">
  </xsl:template>
</xsl:stylesheet>

output

<html>
  <body>
    <div>
     not well-formed xml<br>
    </div>
  </body>
</html>
like image 44
Renzo Ciot Avatar answered Dec 03 '25 18:12

Renzo Ciot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!