XSLT Transformation of Hyperlinks - html

I have an XSLT web page that transforms a table I have extracted from MS Access. In the xml document I have some hyperlinks such as C:\My Work\My HTML test.htm and as far as I know the white space is preserved. My problem is that when I transform this to a Hyperlink the link changes to file:///C:\My%20Work\My%20HTML%20test.htm which does not work. I have other links that are formed in the normal way (without spaces) that work fine so I can pinpoint the issue to the addition of the %20.
I have the command:
<xsl:preserve-space elements="clmAttach1Link clmAttach2Link"/>
in the XSL document. The code to transform the XML is:
<a>
<xsl:attribute name="href">
<xsl:value-of select="clmAttach1Link"/>
</xsl:attribute>
<xsl:value-of select="clmAttach2Name"/>
</a>
This code displays all of the information correctly just does not link to the local files.
Can anyone help me transform the hyperlinks to retain the spaces so I can link to local files?
Thanks

What is the correct link suppose to be? Should it be with spaces? else you could try to use:
<xsl:strip-space>
And have a path without spaces.

Spaces in URLs is not considered safe, but you can try using <xsl:text>:
<xsl:text disable-output-escaping="yes"><a href="</xsl:text>
<xsl:value-of disable-output-escaping="yes" select="clmAttach1Link"/>
<xsl:text disable-output-escaping="yes">"></xsl:text>
<xsl:value-of disable-output-escaping="yes" select="clmAttach2Name"/>
<xsl:text disable-output-escaping="yes"></a></xsl:text>
or only value-of and concat:
<xsl:value-of disable-output-escaping="yes" select="concat(
'<a href="', clmAttach1Link, '">', clmAttach2Name ,'</a>')"/>

Related

Create hyperlink with concat in XSLT --> HTML

Hi and happy year to everyone !
Once again, I need your help with a transformation in XSLT.
I have an XML-TEI file with a lot of pb elements. For instance :
<pb ed="bnf" id="f77.image.r"/>
I have also in the same file one link element :
<link id="bnf" target="http://test.com:/12148/btv1b90621925/"/>
My transformation in xslt should not only transform the <pb> element to look like this in HTML : [77r] but the [77r] should also be a hyperlink.
Now, it only does the first stuff :
<xsl:template match="pb[#ed='bnf']"><span class="pb">
<xsl:text> [</xsl:text>
<xsl:value-of select="substring-after(substring-before(#id, '.image.'), 'f')"/><xsl:value-of select="substring-after(#id, '.image.')">
</xsl:value-of>
<xsl:text></xsl:text>
<xsl:apply-templates/>
<xsl:text>] </xsl:text>
</span>
</xsl:template>
How can I reach the link element and do so the [f77r] takes the value of the link and concat it with the id of the pb element ? Every pb should be a hyperlink like this (with the end changing for each pb) :
http://test.com:/12148/btv1b90621925/f77.image.r
Thank you so much for your help :)
Best wishes,
Micha
It looks like you could use a key here, to look up the link elements by there id attribute.
<xsl:key name="links" match="link" use="#id" />
Then you can use the key to get the target of the link when you construct the href for the hyperlink
<a href="{key('links', #ed)/#target}{#id}">
Note the double-use of Attribute Value Templates to create the attribute. You could also write this if you wanted
<a href="{concat(key('links', #ed)/#target, #id)}">
Try this XSLT template
<xsl:template match="pb[#ed='bnf']">
<span class="pb">
<a href="{concat(key('links', #ed)/#target, #id)}">
<xsl:text> [</xsl:text>
<xsl:value-of select="substring-after(substring-before(#id, '.image.'), 'f')"/><xsl:value-of select="substring-after(#id, '.image.')">
</xsl:value-of>
<xsl:apply-templates/>
<xsl:text>] </xsl:text>
</a>
</span>
</xsl:template>
This should output the following:
<span class="pb">
[77r]
</span>

Import HTML Drupal module with custom XSL template

I have a requirement to import a html website to Drupal and I have decided to using Import HTML module to do it.
I have to be able to grab just the text from html page (inside tag) without the html tags.
For this, I'm trying to create a custom xsl template based on the default template: html2simplehtml.xsl.
Currently my import is working fine with html2simplehtml.xsl template.
here is example of the result node body from the import:
<div class="container-narrow">
<div class="masthead">
<ul class="nav nav-pills pull-right">
<li class="active">
Home
</li>
<li>
Applications
</li>
<li>
Middleware
</li>
now, the requirement is to only get:
Home
Applications
Middleware
I have found this to remove html tags:
<!-- This will remove the tag -->
<xsl:template name="remove-html">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '<')">
<xsl:value-of select="substring-before($text, '<')"/>
<xsl:call-template name="remove-html">
<xsl:with-param name="text" select="substring-after($text, '>')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
but I am not sure where to put and how to call it using this:
<!-- Calling the template that removes tag -->
<xsl:call-template name="remove-html">
<xsl:with-param name="text" select="{HtmlBody}"/>
</xsl:call-template>
How can I do this?
I'm not quite familiar with the way that Drupal calls your XSLT but let's assume it's a simple XSLT 1.0 processor using some HTML page as input and generating the output that you showed above. Let's further assume that the original HTML is well formed with all required closing tags, so that it's in fact XHTML which can be processed by the XSLT processor. (This is not true for the HTML you included in your question, by the way.)
So what you want to do is basically prevent all tags in the XML/XHTML input from showing up in the output. I think the easiest way to achieve this to use the <xsl:value-of select> tag. Assuming that you copy all the child tags of the <body></body> section of your XHTML like this:
<xsl:template match="body">
<xsl:copy-of select="*">
</xsl:template>
instead you could do this:
<xsl:template match="body">
<xsl:value-of select=".">
</xsl:template>
<xsl:value-of> forces the evaluation of the XML sub tree into a string which is done (simply put) by concatenating all contained text elements. This does not, however, take care of white space yet. If you want to the eliminate disturbing white space you could brace the call like this:
<xsl:template match="body">
<xsl:value-of select="normalize-space(.)">
</xsl:template>
Now for the template you originally wanted to use: This does in fact remove tags from the input, too. But if I interpret the code right the input is NOT an XML node set but it must already be a STRING. So this works for other context in which you have a literal XML representation in a string. If you tried to use it here you would have to explicitly convert your XML representation into a string beforehand by using e.g. <xsl:value-of>. In this case the template would already be stripped off the tags (as described above) and would effectively not do anything at all but return the same string that it was passed as parameter. So IMHO, you will not need this template at all.

In HTML code use another data tag in href=Variable

<!-- Section: /report/detail -->
<xsl:template match="/report/detail">
<div href="./lf_web" style="left: 0.0ex; position: absolute"><xsl:value-of select="./lf_po"/></div>
<br/>
</xsl:template>
In this code I am trying to use the content of the taq element "./lf_web".
Can any one help me please.
It is in and .xsl file and linked to .xml data source
You should tag your question with XSL instead of div and href which are not important to your question.
One way is to first retrieve the value in a variable. I like that way because you can use functions:
<xsl:variable name="website_uri" as="xs:string">
<xsl:value-of select="info/uri"/>
</xsl:variable>
<xsl:variable name="protocol" as="xs:string">
<xsl:value-of select="substring-before($website_uri, '://')"/>
</xsl:variable>
And I show how you use a variable: with a $ sign before the name of the value. However, to do so inside a different attribute, you need to put it between curly brackets;
...
Now I think you can do the query you're trying to achieve inline too, but I don't see a good example for it.

Show escaped html tags from XML as well as non-escaped tags in XSLT

I have these escaped tags in my XML file: <strong> --> The whole xml is like this:
<test>
<TEST2>
<li><strong>blablablabla</li>
</TEST2>
<test>
I want to show the text in strong and bold letters (like this) and I want to show the list-item bullet. Now I only manage to show the text in strong and bold letters OR I can show the list-item bullet, but I never managed to show them both correctly. So my question is: how can I show them both in the right manner?
First, I tried <xsl:value-of select="/test/TEST2" disable-output-escaping="yes" />, which showed me the text in italic and bold version, but then, it did not output the list item bullets, because it just outputted the raw <li> tag...
Then, I tried <xsl:copy-of-select="/test/TEST">, which showed the list bullets, but this time, it also outputted the raw <strong> and <em> tags.
Besides, copy-of select, combined with disable-output-escaping did not work.
What should I do to show them both correctly?
Sample outputs:
*<em><strong>blalblblbal</strong></em> if I use the second one.
If I use the first one:
blalblblbal
Well use
<xsl:template match="li">
<xsl:copy>
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:copy>
</xsl:template>
of course with other templates for ancestors of li generating a ol or ul as needed and processing child elements e.g.
<xsl:template match="test">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
But disable-output-escaping is not supported for instance for client-side XSLT in Mozilla browsers. And generally XSLT processors do not need to support it.

XSLT document for-each and image syntax

I am trying to use the code below to select all values of notableWork, so for each notableWork get its value and get the next one etc. It is sort of working as two "Notable Work:" are appearing just not the values of notable work.
<xsl:for-each select="notableWork">
<p>
Notable Work: <xsl:value-of select="notableWork" />
</p>
</xsl:for-each>
The other issue I am having is with images. I have been messing around with the img tag trying variations of it, below is just what I have ended up with. I have an element called imaged which contains a value of image2.jpg, image4.jpg etc. I cannot seem to get it to work :P Just wondering what the correct syntax is, using google I found variations of so really not sure.
<img src="<xsl:value-of select="image" />" />
Thanks :)
<xsl:for-each select="notableWork">
<p>
Notable Work: <xsl:value-of select="notableWork" />
</p>
</xsl:for-each>
can never work unless you have a nesting like notableWork/notableWork. You already selected notableWork so you cant select it again. So you should work with the node, the current node. Which is XSLT: ., current() or XPath: self::node(). All 3 can be used for the same effect.
Notable Work: <xsl:value-of select="." />
or
Notable Work: <xsl:value-of select="current()"/>
To set the attribute of a image tag you can use
<img>
<xsl:attribute name="src">
<xsl:value-of select="images" />
</xsl:attribute>
</img>
or
<img src="{image}"/>