Using XSL, Part 2
I've now designed three totally separate websites using XSL and XML to generate XSL. This journal entry shares some more tips and tricks for using XSL.
How do I create an XML file that has HTML mark up in its fields, (i.e. <block><This is content</b></content>) and force that mark up to pass through?
Say you have an XML file that contains something like this:
<?xml version='1.0'?>
<block>
<b>this is content</b>
</block>
And you want the <b></b> tags to make the output be bold, and not be defined as an XML tag called <b>. In your XSL file make something like this at the top:<xsl:template match="a|i|b|u"><xsl:copy-of select="."/></xsl:template>Put any HTML tag you want in that list, with each tag separated by a vertical pipe. If it's not in the list, it won't be passed through. Attributes will also go through having to be specified. This technique works with tags that have opening and closing tags, such as <B></B>, and tags that only have an opening part, such as <BR/>, <HR/>, and <IMG/>.
Then, in your XSL file, instead of using <xsl:value-of select="/block"/>, use <xsl:apply-templates select="/block"/>. Be careful using this technique, though, because any HTML tags that match defined XSL templates will end up calling those templates. For example, if you have this defined at some point in your document:
<xsl:template match="somename">
some data or something
</xsl:template>
And then your XML file contains this:
<?xml version='1.0'?>
<block>
<somename>this is content</somename>
</block>
Then the phrase "this is content" will be replaced with the phrase "some data or something", which is probably not what you wanted. However, this can be very powerful to build your own tags. You can make a new tag like this:
<xsl:template match="newtag">
<b><i><xsl:copy-of select="."/><i></b>
</xsl:template>
And then wrap something in <newtag> and it will be transformed to be wrapped with <b> and <i>.How do I insert HTML entities?
You can't do things like or ©, but you can insert & and <. There are five defined HTML entities in XSL/XML. They are &, <, >, ', and ". But you probably want to use the rest of them. The trick is to use the numerical definition of the entity. The World Wide Web consortium has a nice list of HTML entities and their numerical values available.
How do I insert Javascript?
Putting { and } is not really allowed inside attributes, so you cannot put javascript into an A tag at all. But you can call functions, but even there you end up with errors when you put in your curly braces. Surround your javascript like this:
<![CDATA[
function goJS() {
return false;
}
]]>