Using xsl:if doesn't include closing tag - html

Trying to use the following code to insert a google ad inside of a blog roll
<xsl:if test="position() = 3">
<object data="/frontpage_blogroll_center_top_728x90"
width="735"
height="95" ></object>
</xsl:if>
For some reason the closing tag </object> doesn't get rendered in the HTML and causes error. Is there anyway to resolve this?

There is no difference (except lexical) in XML between:
<object></object>
and
<object/>
These represent exactly the same XML element, and different XSLT processors are free whichever of the two representations above they choose.
If the long form of the element is really needed in HTML, this can be achieved by either:
Using <xsl:output method="xhtml"/> . The xhtml method is only available in XSLT 2.0.
Using <xsl:output method="html"/> . The result of the XSLT transformation will be an HTML document (not XML).
Using a trick, like:
<object data="/frontpage_blogroll_center_top_728x90" width="735" height="95" >
<xsl:value-of select="$vsomeVar"/>
</object>
where $vsomeVar has no value and will not cause anything to be output, but will trick the XSLT processor into thinking something was output and thus outputting the long form of the element.

Use html output method.
This stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:if test="true()">
<object data="/frontpage_blogroll_center_top_728x90"
width="735"
height="95" ></object>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output:
<object height="95"
width="735"
data="/frontpage_blogroll_center_top_728x90"></object>
Tested with MSXSL, Xalan, Oracle, Saxon, Altova, XQSharp.

Related

is it possible to disable-output-escaping twice in XSLT

I have XML that has encoded HTML data. I am trying to render the data but can't seem to figure out how. Best I can tell is I need to disable-output-escaping="yes" twice but not sure how to do that.
For example, this is a snippet of my XML:
<root>
<node value="&lt;b&gt;body&lt;/b&gt;" />
</root>
My XSLT is outputting HTML. Here is the rendered output (the HTML source) with various options
<xsl:value-of select="#value" /> outputs &lt;b&gt;hi&lt;/b&gt;
<xsl:value-of select="#value" disable-output-escaping="yes" /> outputs <b>hi</b>
I would like it to output <b>hi</b> to the HTML source so its actually rendered as a bolded hi. Does that make sense? Is that possible?
Escaping is the process of turning < into <. If you disable escaping, it will leave < as <. What you want to achieve is to turn < into <, which would normally be called "unescaping".
In the normal course of events, a parser performs unescaping, while a serializer performs escaping. So if you want to unescape characters, you need to put them through a parsing process, which means you need to take the content of the #value attribute and put it through an operation like fn:parse-xml-fragment() in XPath 3.0, or an equivalent extension function in your chosen processor.
Assuming Sharepoint as a Microsoft .NET product uses XslCompiledTransform you could try to implement the unescaping and parsing with extension "script" (C# or VB or JScript.NET code embedded in XSLT) as follows:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="msxsl mf">
<msxsl:script language="C#" implements-prefix="mf">
<msxsl:using namespace="System.IO"/>
public string Unescape(string input)
{
XmlDocument doc = new XmlDocument();
XmlDocumentFragment frag = doc.CreateDocumentFragment();
frag.InnerXml = input;
return frag.InnerText;
}
public XPathNavigator ParseXml(string xmlInput)
{
using (StringReader sr = new StringReader(xmlInput))
{
return new XPathDocument(sr).CreateNavigator();
}
}
</msxsl:script>
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<html>
<head>
<title>Test</title>
</head>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node">
<div>
<xsl:copy-of select="mf:ParseXml(mf:Unescape(#value))" />
</div>
</xsl:template>
</xsl:stylesheet>
If you have access to an XSLT processor (like any version of Saxon 9.7 or Exselt or the latest Altova or XmlPrime) supporting the XPath 3 functions parse-xml and parse-xml-fragment you can write that template without extension functions (in a version="3.0" stylesheet) as
<xsl:template match="node">
<div>
<xsl:copy-of select="parse-xml(string(parse-xml-fragment(#value)))"/>
</div>
</xsl:template>
Output your result with disable-output-escaping, then treat it again in another XSL with disable-output-escaping.

How to get a value from a JavaScript in XSLT?

I have the following XML and XSLT to transform to HTML.
XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<te>t1</te>
</root>
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" />
<xsl:template match="root">
<html>
<div>
<xsl:variable name="name1" select="te" />
**
<xsl:value-of select="CtrlList['$name1']" />
**
</div>
<script language="javascript">var List={
"t1":"test"
}</script>
</html>
</xsl:template>
</xsl:stylesheet>
So my objective is get the value of "te" from the XML and map it with the JavaScript object "List" and return the value test while transforming with the XSLT. So i should get the value test as output.
Can anyone figure out what wrong in the XSLT.
When you look at your XSLT, it may seem like there is JavaScript there, but all XSLT sees is that it is outputing an element named "script", with an attribute "language", which contains some text. It is also worth noting that xsl:value-of is used to get the value from the input document, but your script element is actually part of the result tree, and so not accessible to xsl:value-of.
However, it is possible to extend XSLT so it can use javascript functions, but this is very much processor dependant, and you should think of it the same way as embedding JavaScript in HTML. Have a look at this question, as an example
How to include javaScript file in xslt
So, in your case, your XSLT would be something like this (Note this particular example will only work in Mircorsofts MSXML processor)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="http://mycompany.com/mynamespace"
exclude-result-prefixes="msxsl user">
<xsl:output method="xml" indent="yes" />
<msxsl:script language="JScript" implements-prefix="user">
var List={
"t1":"test"
}
function lookup(key) {
return List[key];
}
</msxsl:script>
<xsl:template match="root">
<html>
<div>
<xsl:variable name="name1" select="te"/>
<xsl:value-of select="user:lookup(string($name1))"/>
</div>
</html>
</xsl:template>
</xsl:stylesheet>
Of course, it might be worth asking why you want to use javascript in your XSLT. It may be possible to achieve the same result using purely XSLT, which would certainly make you XSLT more portable.

XSLT: Transforming into non-xml content?

Is it possible to use XSLT to transform XML into something other than XML?
e.g. i want the final non-xml content:
<Content>
<image url="file1.png">
<image url="file2.png">
...
<image url="filen.png">
<EndContent>
You'll notice this document is not xml (or even html), but it does have what we would call <elements>.
Is it possible, using XSLT, to generate non-xml output?
Another example of non-xml output might be:
<HTML>
<BODY>
<IMG src="file1.png"><BR>
<IMG src="file2.png"><BR>
...
<IMG src="filen.png"><BR>
</BODY>
</HTML>
You'll notice this document is HTML, because in HTML IMG and BR tags are forbidden from having a closing tag. This contrasts with xhtml (the reformulation of HTML using xml) where all elements are required to have a closing tag (because in xml every tag must be closed).
Another example of non-xml output might be:
INSERT INTO Documents (Filename) VALUES ('file1.png')
INSERT INTO Documents (Filename) VALUES ('file2.png')
...
INSERT INTO Documents (Filename) VALUES ('file3.png')
i can make up any source xml i like, but one example might be:
Source xml:
<DocumentStore>
<Document type="image">file1.png</Document>
<Document type="image">file2.png</Document>
<Document type="image">filen.png</Document>
</DocumentStore>
Or perhaps:
<Profiles>
<User avatar="file1.png" />
<User avatar="file2.png" />
<User avatar="filen.png" />
</Profiles>
You can use <xsl:output> to specify the output format, which doesn't have to be xml, see this reference page.
However, if you are outputting html, no modern browser should complain even if you do put the closing tags, so using your example above, i believe all browser should be ok with :-
<HTML>
<BODY>
<IMG src="file1.png"></IMG><BR></BR>
<IMG src="file2.png"></IMG><BR></BR>
...
<IMG src="filen.png"></IMG><BR></BR>
</BODY>
</HTML>
So not too sure why you don't want to put the closing tag, unless i'm missing something.
Update: Added example of non xml output
Given this stylesheet:-
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/filenames">
<xsl:for-each select="filename">
INSERT INTO Documents (Filename) VALUES ('<xsl:value-of select="." />')
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
and this input xml:-
<?xml version="1.0" encoding="UTF-8"?>
<filenames>
<filename>file1.png</filename>
<filename>file2.png</filename>
<filename>file3.png</filename>
</filenames>
You get output like this:-
INSERT INTO Documents (Filename) VALUES ('file1.png')
INSERT INTO Documents (Filename) VALUES ('file2.png')
INSERT INTO Documents (Filename) VALUES ('file3.png')
No matter how you create your IMG tags,
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<HTML>
<BODY>
<xsl:element name="IMG">
<xsl:attribute name="src">file1.png</xsl:attribute>
</xsl:element>
<IMG src="file2.png"></IMG>
<IMG src="filen.png"/>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
The output method "html" will cause the IMG tags to not be closed.
<HTML>
<BODY><IMG src="file1.png"><IMG src="file2.png"><IMG src="filen.png"></BODY>
</HTML>
Yes, you can, by using the xsl:output element in your stylesheet.

XSLT parse escaped HTML stored in an attribute and convert that attribute's content into element's content

I'm stuck on what I think should be simple thing to do. I've been looking around, but didn't find the solution. Hope you will help me.
What I have is an XML element with an attribute that contains escaped HTML elements:
<Booking>
<BookingComments Type="RAM" comment="RAM name fred<br/>Tel 09876554<br/>Email fred#bla.com" />
</Booking>
What I need to get is parsed HTML elements and content from the #comment attribute to be a content of element as follows:
<p>
RAM name fred<br/>Tel 09876554<br/>Email fred#bla.com
<p>
Here is my XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="xs fn" version="1.0">
<xsl:output method="html" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
doctype-system="http://www.w3.org/TR/html4/loose.dtd" encoding="UTF-8" indent="yes" />
<xsl:template name="some-template">
<p>Some text</p>
<p>
<xsl:copy-of
select="/Booking/BookingComments[lower-case(#Type)='ram'][1]/#comment"/>
</p>
</xsl:template>
</xsl:stylesheet>
I've read that copy-of is a good way to restore escaped HTML elements back to proper elements. In this specific case, because it's initially an attribute the copy-of translates it into attribute as well. So I get:
<p comment="RAM name fred<br/>Tel 09876554<br/>Email fred#bla.com"></p>
Which isn't what I want.
If I use apply-templates instead of copy-of, as in:
<p>
<xsl:apply-templates select="/Booking/BookingComments[lower-case(#Type)='ram'[1]/#comment"/>
</p>
I get p's content simply as text, not restored HTML elements.
<p>RAM name fred<br/>Tel 09876554<br/>Email fred#bla.com</p>
I'm sure I'm missing something obvous. I would really appreciate any help and tips!
I would recommend using a dedicated template:
<!-- check if lower-casing #Type is really necessary -->
<xsl:template name="BookingComments[lower-case(#Type)='ram']/#comment">
<p>
<xsl:value-of select="." disable-output-escaping="yes" />
</p>
</xsl:template>
This way you could simply apply templates to the attribute. Note that disabling output escaping has the potential to generate ill-formed output.
You could bind an extension function parse() which parses a string into a nodeset. The exact mechanism will depend on your XSLT engine.
In Xalan, we can take the following static method:
public class MyExtension
{
public static NodeIterator Parse( string xml );
}
and use it like so:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:java="http://xml.apache.org/xalan/java"
exclude-result-prefixes="java"
version="1.0">
<xsl:template match="BookingComments">
<xsl:copy-of select="java:package.name.MyExtension.Parse(string(#comment))" />
</xsl:template>
</xsl:stylesheet>

Using XSLT as xhtml link extractor

I'm starting using XSLT and write this scipt:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8" />
<xsl:template match="span[#class='thumb']" >
Link: <xsl:value-of select="$base" /><xsl:value-of select="a/#href" />
</xsl:template>
<xsl:template match="/">
Base href: <xsl:value-of select="$base" />
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
And using this command:
xsltproc --html --param base "'http://example.com'" lista.xslt test.html
I need to get list of Links, but I get whole page on output. What's wrong? How can I get it works?
There are some default templates which are unseen here. The really easy way to resolve it is to just explicitly limit to the span elements you're matching as below. Otherwise, you can override the default templates.
<xsl:template match="/">
Base href: <xsl:value-of select="$base" />
<xsl:apply-templates select="//span[#class='thumb']" />
</xsl:template>
There's a default template that matches essentially everything if you let it. Your 4th last line calls that template.
That's part of the problem. The rest can probably be taking care of by matching just the stuff you're looking for, directly in the top-level template.