How can I apply an xslt template to a string? - string

Given a template used for building some html around a value, I want to pass in a string, rather than a node set. As an example I want to concat some values and pass that to the template. How can I achieve that sort of thing?
<xsl:template match="text()" mode="kvp-print-single">
<tr>
<td colspan="3"><xsl:value-of select="."/></td>
</tr>
</xsl:template>
...
<xsl:apply-templates select="concat=(haba/hiba:text(), ' - ', huba/baba:text())" mode="kvp-print-single"/>
ErrorMsg: xml or stylesheet file is invalid!
Exception: System.Xml.Xsl.XsltException: Expression must evaluate to a node-set.

If the aim is code re-use, to use the template in multiple places, then what you could do is give your template a name (in addition to the template match), and give it a default parameter
<xsl:template match="text()" name="kvp-print-single" mode="kvp-print-single">
<xsl:param name="text" select="." />
<tr>
<td colspan="3"><xsl:value-of select="$text"/></td>
</tr>
</xsl:template>
Then just use xsl:call-template to call it with you concatenated string as a parameter
<xsl:call-template name="kvp-print-single">
<xsl:with-param name="text" select="concat(haba/hiba:text(), ' - ', huba/baba:text())" />
</xsl:call-template>
Note, the template will still match "text()" nodes in the normal way when matched using xsl:apply-templates.

You could use call-template and a named template, rather than apply-templates, thus:
<xsl:template name="kvp-print-single">
<xsl:param name="theValue"/>
<tr>
<td colspan="3"><xsl:value-of select="$theValue"/></td>
</tr>
</xsl:template>
<xsl:call-template name="kvp-print-single">
<xsl:with-param name="theValue" select="concat(haba/hiba:text(), ' - ', huba/baba:text())"/>
</xsl:call-template>
The point of apply-templates is to take a nodeset, and apply the most appropriate template to each node in turn. call-template and named templates allows you to break up your XSLT into more manageable chunks, without changing the context.

You can't "pass in a string, rather than a node set" because templates are not called like functions. With XSLT, not the code controls the execution order but the data does by matching templates. It is possible to use named templates that can be called instead matched, but to pass values, you can use parameters for each templates.
In your case, you don't even have to as you can adress the text parts (given haba/hiba is the address) like so:
<xsl:template match="some_element" mode="kvp-print-single">
<tr>
<td colspan="3">
<xsl:value-of select="concat=(/root/haba/hiba/text(), ' - ', /root/huba/baba/text())"/>
</td>
</tr>
</xsl:template>
The adress needs to be correct XPath, of course (absolute or even relative to matching element).
#Mithon: The other answers require parameters. Use as many modularized templates as you want, but why would you want to add parameters if you don't need them?

Related

how to concatenate (part of) variable to String in XSLT

I would like to see the first couple of characters of the variable 'vari' to be concatenated to String abc=' here:
href="{concat('abc=', substring-before('vari', '='))}"
Here is the whole snippet:
<xsl:template match="report:subelement">
<tr>
<td>
<message>
<xsl:variable name="vari" select="."></xsl:variable>
<xsl:copy-of select="." />
</message>
</td>
<td>
<button type="button" onclick="window.location.href=this.getAttribute('href')" href="{concat('abc=', substring-before('vari', '='))}" >Kill thread</button>
</td>
</tr>
</xsl:template>
that is probably a trivial question but I am just learning xslt.
You are quite close, but:
To access the value of a variable you have to use an dollar ($) as prefix. Do not put variable name in apostrophe.
Therefore try:
href="{concat('abc=', substring-before($vari, '='))}
Than this will throw an error because your variable declaration is not in the same context as the usage.
The variable declaration has to be in the same element or in an ancestor. Put the declaration at the top of the subelement template or the in the <tr element.
Updated working template:
<xsl:template match=""report:subelement">
<xsl:variable name="vari" select="."></xsl:variable>
<tr>
<td>
<message>
<xsl:copy-of select="." />
</message>
</td>
<td>
<button type="button" onclick="window.location.href=this.getAttribute('href')"
href="{concat('abc=', substring-before($vari, '='))}" >Kill thread</button>
</td>
</tr>
</xsl:template>

XSL / XSLT - Adding a value-of as a style attribute for a DIV

I'm using SharePoint 2010 and the Content Query Web Part to output a list of dates from a SharePoint list. The display of this list is controlled by an XSL stylesheet called ItemStyle.xsl
I have made progress with the general appearance but would now like to add one of the fields it retrieves as a background/style attribute.
I believe the problem I am having relates to the xsl value-of select having a closing bracket at the end of the tag and therefore inadvertently closing my DIV. Can someone look at the code below and suggest alternative way of printing the CategoryColour within the opening DIV.
I also will occasional have "xmlns:ddwrt" inserted into the html where I would expect to at least see "style:background...."
Many Thanks
<xsl:stylesheet
version="1.0"
exclude-result-prefixes="x d xsl msxsl cmswrt"
xmlns:x="http://www.w3.org/2001/XMLSchema"
xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
xmlns:cmswrt="http://schemas.microsoft.com/WebParts/v3/Publishing/runtime"
xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<!-- PrettyCal Template -->
<xsl:template name="PrettyCal" match="Row[#Style='PrettyCal']" mode="itemstyle">
<xsl:variable name="Start"><xsl:value-of select="#EventDate" /></xsl:variable>
<xsl:variable name="End"><xsl:value-of select="#EndDate" /></xsl:variable>
<xsl:variable name="AllDay"><xsl:value-of select="#AllDayEvent" /></xsl:variable>
<xsl:variable name="Location"><xsl:value-of select="#EventLocation" /></xsl:variable>
<xsl:variable name="CategoryColour"><xsl:value-of select="#EventCategoryColour" /></xsl:variable>
<div class="upcoming-events" style="background: {CategoryColour}" ><xsl:value-of select="$CategoryColour"/>
<h2 class="event-title">
<a>
<xsl:attribute name="onClick">
javascript:SP.UI.ModalDialog.showModalDialog({ url: '/services/marketing/Lists/College%20Calendar/DispForm.aspx?ID=<xsl:value-of select="#ID" />', title: 'Event Details' }); return false;
</xsl:attribute>
<xsl:value-of select="#Title" /></a></h2>
</div>
</xsl:template>
</xsl:stylesheet>
You have:
<div class="upcoming-events" style="background: {CategoryColour}" ><xsl:value-of select="$CategoryColour"/>
You actually wanted:
<div class="upcoming-events" style="background: {$CategoryColour}" ><xsl:value-of select="$CategoryColour"/>
Your missing a $ in front of the variable CategoryColour.

DataView WebPart in sharepoint designer

I would like to display the list items using DataView WebPart and I am successful so far. But I would like to show the items in two columns for each row, instead of one columns per one row. How can i achieve this.
<tr>
<xsl:if test="position() mod 2 = 1">
<xsl:attribute name="class">ms-alternating</xsl:attribute>
</xsl:if>
<xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
<td class="ms-vb" width="1%" nowrap="nowrap">
<span ddwrt:amkeyfield="ID" ddwrt:amkeyvalue="ddwrt:EscapeDelims(string(#ID))" ddwrt:ammode="view"></span>
</td>
</xsl:if>
<xsl:variable name="ImageURL">
<xsl:value-of select="#ImageURL" />
</xsl:variable>
<td class="ms-vb">
<img alt="" src="{$ImageURL}" />
</td>
</tr>
I would like to show the items from a list in two columns and the table should be increased dynamically based on the number of items. Can someone guide me on how to achieve this.
Actually you can achieve this more easily with Item Lister Web Part. Try to check that out.

Sharepoint XSL - break string into componant parts

I'm using a "choice" site column with the multiple-check option enabled so that users can tag a list item with several choices from the column.
This column then powers a design feature in a content query webpart - where the column choice is appended to create an image filename.
Choice1
Choice2
Choice3
Choice4
becomes
<img src="http://mysite/content-Choice1.jpg />
The problem I've got is that the XSL parser is fed a string which has semicolons (;) and hashes (#) separating the choice values. If all 4 options were ticked, the string fed into the XSLT parser would be:
;#Choice1;#Choice2;#Choice3;#Choice4
How can I work through the string and separate each choice into its own XSL variable?
I've tried various substring-before functions, but I can't get anything working.
Since XPath 1.0 does not support the tokenize() function, you'll have to do all the work yourself. For instance, you can generate the <img> elements recursively from the choices:
<xsl:template name="RecurseConvertChoicesToImages">
<xsl:param name="choices" />
<xsl:variable name="token"
select="substring-before($choices, ';#')" />
<xsl:variable name="nextToken"
select="substring-after($choices, ';#')" />
<xsl:if test="$token">
<img src="http://mysite/content-{$token}.jpg" />
</xsl:if>
<xsl:if test="$nextToken">
<xsl:call-template name="RecurseConvertChoicesToImages">
<xsl:with-param name="choices" select="$nextToken" />
</xsl:call-template>
</xsl:if>
</xsl:template>
I would recommend to use JavaScript to parse the string and accordingly display the image using JavaScript.

Outputting SharePoint Hyperlink Column as URL

I have some document URLs stored in a Sharepoint publishing column. When I output the info into a HTML page using:
<xsl:value-of select="#[ColumnName]" />
in ItemStyle.xml, I get [url], [document name] in the page. I would like to display this as a URL can anyone help with the XSL?
You could use:
<xsl:value-of select="substring-before(#[ColumnName],',')"/>
or whatever the separator is.
Thanks everyone, in the end I figured out the following based on a post at sguk
<xsl:variable name="Doc">
<xsl:call-template name="OuterTemplate.GetTitle">
<xsl:with-param name="Title" select="#DocumentLink1"/>
</xsl:call-template>
</xsl:variable>
with the following a tag code:
<a href="{substring-before($Doc,',')}">
<xsl:value-of select="substring-after($Doc,',')" />
</a>
or for an image:
<xsl:variable name="Image">
<xsl:call-template name="OuterTemplate.GetTitle">
<xsl:with-param name="Title" select="#img" />
</xsl:call-template>
</xsl:variable>
with the following img tag:
<img src="{substring-before($Image,',')}" alt="{substring-after($Image,',')}" />
I'm posting the solution back here as this proved ludicrously hard to figure out (probably my fault as I don't really 'get' XSL) but just in case anybody is looking for it, this code outputs images or links from the 'Hyperlink or Picture' column type in Sharepoint.
Another thing you can do is to take a list that shows URLs properly (like a Links list) and use SharePoint Designer to convert it to a DataView WebPart. In there will be the proper XSL for doing the conversion.
The easiest way to do this is with SharePoint designer:
click the field that shows "http://link, description"
a box with an > will appear, click it and you will get a "common xsl:value-of tasks" flyout.
It will have the field name and type, with the type set to "text".
Change the type to "hyperlink" and you will get a box allowing you to format the hyperlink. It will have all the necessary xsl already populated but you can input your own text or remove the link.
This hopefully helps. It shows "Project Site" when the hyperlink is entered and spaces when not.
<!--Project Site--><TD Class="{$IDAAO2UG}">
<xsl:variable name="Field" select="#Project_x0020_Site" />
<xsl:choose>
<xsl:when test="substring-before(#Project_x0020_Site, ', ')=''"><xsl:value-of select="substring-after(#Project_x0020_Site, ', ')" /></xsl:when>
<xsl:otherwise><A HREF="{substring-before(#Project_x0020_Site, ', ')}">
<xsl:choose>
<xsl:when test="substring-after(#Project_x0020_Site, ', ')=''"><xsl:value-of disable-output-escaping="no" select="substring-before(#Project_x0020_Site, ', ')" /></xsl:when>
<xsl:otherwise><xsl:value-of select="substring-after(#Project_x0020_Site, ', ')" /></xsl:otherwise>
</xsl:choose>
</A></xsl:otherwise>
</xsl:choose>
</TD>
In SharePoint 2013 you have to do things a bit differently because the #Url attribute is is no longer delimited with a comma. There is now a .desc sub property of #Url. Below is an example of how this works hopefully this saves someone else some time.
<xsl:template name="dvt_1.rowview">
<xsl:if test="string-length(#URL) > 0">
<div class="link-item item">
<a title="{#Comments}" target="_blank" href="{#URL}">
<xsl:value-of select="#URL.desc" />
</a>
</div>
</xsl:if>
</xsl:template>

Resources