Can't get img to display from xml source using xsl attribute - html

My xsl file has this
<img>
<xsl:attribute name="src">
<xsl:value-of select="//movie[#num='1']/thumb_img1" width="100" height="111"/>
</xsl:attribute>
</img>
my xml file has
<questions>
<question>
<movie num="0">
<num>0</num>
<quote>Xslt</quote>
<title>movie</title>
<thumb_img1>pic.jpeg</thumb_img1>
<big_img>bigpic.jpeg</big_img>
</movie>
when I run this I just get an x in a picture box when I use value-of it gives me the filename pic.jpeg which is what I want to use as the source for the picture

First of all, what you wrote is not correct XSLT code. Using this code, you could never have transformed anything. The attributes width and height are not allowed on an xsl:value-of element.
Use an attribute value template instead of xsl:attribute.
<img src="{//movie[#num='1']/thumb_img1}" width="100" height="111"/>
Depending on the context of the img element in your code (the template match) you might not need to start the expression with //.

Related

Display image in HTML(base64) using XSLT

I am trying to display an JPG image in HTMl after conversion to base64. This operation I am trying in XSLT code. Couple of options I tried with concat and write-binary, but none of them seem to be working.
Is this supported using XSLT? Is there any built-in function available?
<img>
<xsl:attribute name="src">
<!--xsl:value-of select="concat('data:image/gif;base64,',xPath)"/-->
<xsl:value-of select="file:write-binary(C:\MyDesktop\Desktop\allImages\download.jpg, xs:base64Binary(string()))"/>
</xsl:attribute>
</img>
Assuming you have access to e.g. Saxon PE or EE with support for the EXPath file module http://expath.org/spec/file then I think it suffices to construct a data URI https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs using the file:read-binary method http://expath.org/spec/file#fn.read-binary:
In the XSLT you can use e.g.
<img src="data:image/jpeg;base64,{file:read-binary('C:\MyDesktop\Desktop\allImages\download.jpg')}"/>
to construct an HTML img element that has the image data inlined in the data URI set as the src attribute value.
The stylesheet obviously needs to declare the module's namespace with e.g.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:file="http://expath.org/ns/file"
exclude-result-prefixes="#all" version="3.0">

XSLT: What is the best way to apply a floating text that will display the full expression for acronyms from an external dictionary file?

I'm new to XSLT.
I need to apply floating text that displays a setting for acronyms, as soon as the mouse cursor hovers over a word that is abbreviated.
My input file is XML, and every word that should display acronyms is represented in the following format:
<abbreviation Id="E.G."/>
When the mouse cursor hovers over the word EG, the floating text will be displayed: "for example".
I thought to apply the floating text using the HTML abbr tag
And the code I wrote is:
<xsl:template match="abbreviation">
<abbr title="for example.">
<xsl:value-of select="#Id"/>
</abbr>
</xsl:template>
I want the "title" attribute to be given as a parameter the definition of the main boxes from an external file that will contain a dictionary of related abbreviations and explanations.
I would love to know how to apply my issue.
And also get ideas about the external dictionary file - what kind of file should you create? For example, I would like a good structure of the file.
Note:
I use OXYGEN EDITOR,
And I believe I can also get solutions in XSLT version 2 and 3
Suppose your external dictionary looks like:
dict.xml
<dictionary>
<entry abbr="C.V.">course of life</entry>
<entry abbr="E.G.">for example</entry>
<entry abbr="N.B.">note well</entry>
</dictionary>
You can then use a key to transform an input like:
XML
<root>
<abbreviation Id="E.G."/>
</root>
using:
XSLT 2.0 (untested)
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="path-to-dictionary" select="'dict.xml'"/>
<xsl:key name="abbr-lookup" match="entry" use="#abbr" />
<xsl:template match="/root">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="abbreviation">
<abbr title="{key('abbr-lookup', #Id, document($path-to-dictionary))}">
<xsl:value-of select="#Id"/>
</abbr>
</xsl:template>
</xsl:stylesheet>
to get:
Result
<html>
<body>
<abbr title="for example">E.G.</abbr>
</body>
</html>
To understand the syntax used, read about Attribute Value Templates.
Well, XSLT does not float any text, it is a programming language to transform XML (or with XSLT 2 or 3, other input formats) to XML, (X)HTML, plain text. You seem to want to transform your XML to HTML in the hope the HTML user agent or browser displays a tooltip of the title attribute.
As for using an XML file as a secondary input file, if you have abbrvs.xml with
<root><abbr key="E.G.">for example.</abbr>...</root>
then in XSLT you can use
<abbr title="{key('abbr', #Id, doc('abbrvs.xml'))}">
<xsl:value-of select="#id"/>
</abbr>
to pull that title attribute value from the secondary input file if the XSLT declares a key
<xsl:key name="abbr" match="abbr" use="#key"/>

XSL: How to use HTML tags inside stylesheet?

I got this code in my XSL stylesheet:
<xsl:for-each select="report:column-names/report:column">
<fo:table-cell display-align="center" font-size="9pt">
<fo:block font-family="{$font.family}" font-weight="bold">
<xsl:value-of select="." disable-output-escaping="yes" /> <--problematic value
</fo:block>
</fo:table-cell>
</xsl:for-each>
In the problematic value that I mentioned, I got values that I want to wrap with bdi html tag.
I tried to just put bdi and I didn't see my value, like this:
<bdi><xsl:value-of select="." disable-output-escaping="yes" /></bdi>
How can apply this tag for my values?
You cannot mix HTML and XSL-FO. XSL-FO is an XML vocabulary that is defined for formatting. The original purpose of XSLT was to transform arbitrary XML vocabularies (the 'X' in 'XML' comes from 'Extensible', after all) into the standard formatting vocabulary. That's what you've been doing with the XSLT in your question.
The description of Unicode BIDI Processing in XSL 1.1 is at https://www.w3.org/TR/xsl11/#d0e4879. The applicable FO is fo:bidi-override (https://www.w3.org/TR/xsl11/#fo_bidi-override), and the applicable properties are direction (https://www.w3.org/TR/xsl11/#direction) and unicode-bidi (https://www.w3.org/TR/xsl11/#unicode-bidi).
You so far haven't shown the content of a report:column element, but it looks like you want:
<fo:bidi-override unicode-bidi="embed" direction="rtl">
<xsl:value-of select="." disable-output-escaping="yes" />
</fo:bidi-override>
Without seeing a report:column element, the embed and rtl are just guesses.
(Using disable-output-escaping is seldom a good idea, but we can't see how bad an idea it is in this case without seeing a report:column element that needs it.)

How to find a specific substring of text anywhere in my XML and select this text using XSLT

I need to find and select a specific substring of text using XPATH/XSLT (if possible) - in this case the entity code for a non-breaking space "&#xa0" then highlight this in my HTML output using some CSS.
I am not searching within a specific element node, I need to search from the root node of my document and find this entity wherever it appears. I do not know where this will appear in my XML, I just know the entity code I am looking for.
Here is a section of my document where "&#xa0" appears within the text:
<row rowsep="1">
<entry align="left" colname="col1" colsep="1">
<para>N/A = not applicable/not generally used, A = applicable </para>
</entry>
</row>
I'm using XSLT 2.0.
Using XSLT 2.0 you could use analyze-string on all text() nodes e.g.
<xsl:template match="text()">
<xsl:analyze-string select="." regex=" ">
<xsl:matching-substring>
<span class="highlight"><xsl:value-of select="."/></span>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
You would then need to set up other templates to transform the remaining elements to HTML where you include the CSS or link to some CSS document defining something like
span.highlight { color: red; }
The span or similar wrapper element is necessary in my view as CSS does not allow you to style a single letter or word within a text node.

XSLT strange behavior with umlaut (mutation)

I have a very strange behavior when I got mutations in a node in xml when I try to parse it with an XSLT.
So we now assume that CustomerName has the Value 'MÖP'.
In this case, the resulting HTML 'a' tag would have a href to 'M%C3%B6P'
<a href="{CustomerName}">
<xsl:value-of disable-output-escaping="yes" select="CustomerName"/>
</a>
In this case, the resulting HTML 'div' tag would have an id 'MÖP'
<div style="display:none">
<xsl:attribute name="id"><xsl:value-of select="CustomerName"/></xsl:attribute>
</div>
It seems like the attribute href in the a tag has something to do with it.
My question is, why is it like that ?
An what can i do that in both cases i got the same output ?
Greetz
The XSLT processor is doing you a favour by escaping the URL value in the href attribute of the HTML link element. This is by design, see http://www.w3.org/TR/xslt-xquery-serialization/#HTML_ESCAPE-URI-ATTRIBUTES. If you really don't want that and you use an XSLT 2.0 processor then you can use <xsl:ouput method="html" escape-uri-attributes="no"/> in your stylesheet.