Display different xsl:attribute depending on the ending of a string - xslt

I have the following xsl code in an xsl document
<A target="_blank" style="text-decoration=none">
<xsl:attribute name="href">viewdoc.aspx?doc=<xsl:value-of select="URLFilePath"/>&mode=inline</xsl:attribute>
<xsl:attribute name="prefix"><xsl:value-of select="FileName"/>: </xsl:attribute>
<IMG src="images/word_small.gif" border="0"/>
</A>
and in the code-behind I am doing this
newItemNode = xmlDocument.CreateElement("URLFilePath")
newItemNode.InnerText = correctedPath
xmlItemNode.ParentNode.AppendChild(newItemNode)
Now that works fine for word documents. However I need a way in code to check the extension of the file, and display the correct Image and xsl:attribute depending on the If statement.
So the If statement will be like this:-
If correctedPath.ToLower.Contains(".doc") Then
//display the word icon and attributes
Else
//display the excel icon and attributes
End If
Can you please give me some tips and help on how I can achieve this?
Thanks

Just using contains() may generally produce the wrong results (see the test XML document).
What is necessary is a ends-with() function, which is standard in XPath 2.0 and can be implemented in XSLT 1.0 as in the following transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="URLFilePath">
<xsl:variable name="visDoc">
<xsl:call-template name="ends-with">
<xsl:with-param name="pEnding" select="'.doc'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="visXls">
<xsl:call-template name="ends-with">
<xsl:with-param name="pEnding" select="'.xls'"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$visDoc=1">word_small.gif</xsl:when>
<xsl:when test="$visXls=1">xls_small.gif</xsl:when>
<xsl:otherwise>unknown_small.gif</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ends-with">
<xsl:param name="pEnding"/>
<xsl:value-of select=
"number(substring(.,
string-length() -string-length($pEnding) +1
)
=
$pEnding
)
"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the following test XML document:
<files>
<URLFilePath>myFile.doc</URLFilePath>
<URLFilePath>myFile.xls</URLFilePath>
<URLFilePath>myFile.xls.doc</URLFilePath>
<URLFilePath>myFile.doc.xls</URLFilePath>
</files>
the correct result is produced:
word_small.gif
xls_small.gif
word_small.gif
xls_small.gif
Do note that just using contains() produces incorrect results.

I managed to come up with a solution! Sorry for the late reply but had to work on something else
Here is the code:-
<A target="_blank" style="text-decoration=none">
<xsl:choose>
<xsl:when test="contains(., '.doc')">
<xsl:attribute name="href">viewdoc.aspx?doc=<xsl:value-of select="URLFilePath"/>&mode=inline
</xsl:attribute>
<xsl:attribute name="prefix">
<xsl:value-of select="FileName"/>:
</xsl:attribute>
<IMG src="images/word_small.gif" border="0"/>
</xsl:when>
<xsl:when test="contains(., '.xls')">
<xsl:attribute name="href">viewxls.aspx?doc=<xsl:value-of select="URLFilePath"/>&mode=inline
</xsl:attribute>
<xsl:attribute name="prefix">
<xsl:value-of select="FileName"/>:
</xsl:attribute>
<IMG src="images/excel_small.gif" border="0"/>
</xsl:when>
</xsl:choose>
</A>
Thanks for all your help guys, really very much appreciated!

A late reply but I hit to two answers that deal with matching end of string with XSLT 1.0 and are very elegant:
https://stackoverflow.com/a/3081205/520567
https://stackoverflow.com/a/3081866/520567
go give them +1

This can be done purely in your XSLT document if you require. For displaying the image, you could use a xsl:choose statement, that tests the URLFilePath element
<xsl:choose>
<xsl:when test="contains(., '.doc')">
<IMG src="images/word_small.gif" border="0"/>
</xsl:when>
<xsl:when test="contains(., '.xls')">
<IMG src="images/excel_small.gif" border="0"/>
</xsl:when>
</xsl:choose>
If you wanted to do this check in the code behind, you could always add extra attributes to your URLFilePath element.
imageAttr = xmlDocument.CreateAttr("image")
If correctedPath.ToLower.Contains(".doc") Then
imageAttr.value = "images/word_small.gif"
Else
imageAttr.value = "images/excel_small.gif"
End If
newItemNode.AppendChild(imageAttr)
And then, in your xls, you can simply use this attribute to set the source attribute of the image
<IMG border="0">
<xsl:attribute name="src"><xsl:value-of select='#image' /></xsl:attribute>
</IMG>

Related

confusing error thown while trying to match templates

I've the below XML line of code.
<entry colname="col3" align="left" valign="top"><para>grandchild, cousin, <content-style font-style="italic">etc</content-style>., shall be described as “lawful” and “one of the next-of-kin” or “only next-of-kin”.</para></entry>
and below XSL
<xsl:template match="entry" name="entry">
<xsl:choose>
<xsl:when test="./#namest">
<xsl:variable name="namest" select="#namest" />
<xsl:variable name="nameend" select="#nameend" />
<xsl:variable name="namestPos" select="count(ancestor::tgroup/colspec[#colname=$namest]/preceding-sibling::colspec)" />
<xsl:variable name="nameendPos" select="count(ancestor::tgroup/colspec[#colname=$nameend]/preceding-sibling::colspec)" />
<td colspan="{$nameendPos - $namestPos + 1}" align="{#align}">
<xsl:apply-templates select="child::node()[not(self::page)]" />
</td>
</xsl:when>
<xsl:otherwise>
<td>
<xsl:if test="./#morerows">
<xsl:attribute name="rowspan">
<xsl:value-of select="number(./#morerows)+1" />
</xsl:attribute>
</xsl:if>
<xsl:if test="./#align">
<xsl:attribute name="align">
<xsl:value-of select="#align" />
</xsl:attribute>
</xsl:if>
<xsl:if test="./#valign">
<xsl:attribute name="valign">
<xsl:value-of select="#valign" />
</xsl:attribute>
</xsl:if>
<xsl:for-each select="para">
<div class="para">
<xsl:choose>
<xsl:when test="../#colname='col3' and contains(./text(),'.')">
<xsl:variable name="strl">
<xsl:value-of select="fn:string-length(fn:substring-before(.,'.'))" />
</xsl:variable>
<xsl:choose>
<xsl:when test="$strl < '6'">
<a href="{concat('er:#SCP_ORD_',//chapter/#num,'/','P',translate(./text(),'.','-'))}">
<xsl:value-of select="./text()" />
</a>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates />
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates />
</xsl:otherwise>
</xsl:choose>
</div>
</xsl:for-each>
</td>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
and when i run this on my XML in the above mentioned line(the XML given in the sample), it is throwing me an error. and the error is as below.
Wrong occurrence to match required sequence type - Details: - XPTY0004: The supplied sequence ('2' item(s)) has the wrong occurrence to match the sequence type xs:string ('zero or one')
here what i actually was trying to achieve is, i have another table(which is basically a TOC), where in there is some linking needed, and below is such sample entries.
<entry colname="col3" align="right" valign="top"><para>A.1</para></entry>
<entry colname="col3" align="right" valign="top"><para>A.2</para></entry>
here i'm searching if the colname is col3 and if this has a . in it, and the above two cases mentioned are passing and are getting linked successfully, where in the case mentioned in the top is throwing the error, can anyone please suggest some better method to differentiate these two cases, and i use XSLT 2.0.
Thanks
The problem is
contains(./text(),'.')
./text() is not "the text of the current element" but rather the sequence of all text node children of the current element. In the case of
<para>grandchild, cousin, <content-style font-style="italic">etc</content-style>., shall be described as “lawful” and “one of the next-of-kin” or “only next-of-kin”.</para>
there are two such nodes, one containing everything between the <para> and <content-style> tags ("grandchild, cousin, " including the trailing space) and the other containing everything between the </content-style> and the </para>. But the contains function expects its first argument to be a single string, not a sequence of two nodes.
Instead of testing ./text() you probably just need to test .:
contains(., '.')
which is interpreted as the string value of the whole para element, i.e. a single string consisting of the concatenation of all the descendant text nodes (so the whole text "grandchild, cousin, etc., shall be described...").

change the class of a link with xslt

I would like to change the class of a link but something must be wrong, however I can't see where.
I have checked that the if test is OK.
Could you tell me where I was wrong ?
<xsl:template match="*" mode="title-link-proced">
<a target="BodyPart" class="little_link">
<xsl:if test="#change = 'true'">
<xsl:attribute name="class">little_link_evolution</xsl:attribute>
</xsl:if>
</a>
</xsl>
I also tried
<xsl:template match="*" mode="title-link-proced">
<a target="BodyPart" class="little_link">
<xsl:if test="#change = 'true'">
<xsl:attribute name="class" select="little_link_evolution"></xsl:attribute>
</xsl:if>
</a>
</xsl>
but that dit not work either
EDIT:
I checked that there is a change attribute with the value true (if I display:
<xsl:value-of select="#change"/>
I get true)
EDIT 2
XML iput
<level_4 change="true" module="2X3E-ZE_RTY_OAD" title="presentation">
</level_4>
OK, it might help some one day
rather than attemting to modify the existing class, I decide not to attribute a class in the beginning and to do this:
<a target="BodyPart">
<xsl:choose>
<xsl:when test="#change = 'true'">
<xsl:attribute name="class">little_link_evolution</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="class">little_link</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
And it worked !

disable the current link in a breadcrumb trail

Does anyone know how to disable the current link in a breadcrumb trail.
I am working on a bspoke CMS and i have been asked to remove the current link in the breadcrumb trail.
All that i know in XSLT has failed and i have quite some time on, XSLT is no my strength and i need some helpful input.
Please help
here is the XSLT code:
[code]
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" omit-xml-declaration="yes" />
<!--
Variable to determine if the current open page is displayed as a link or plain text
select = 1 - displays plain text link
select = 0 - displays link (default value)
-->
<xsl:variable name="DisableCurrentPageLink" select="0" />
<xsl:template match="BreadCrumbTrail">
<xsl:apply-templates select="Page" />
</xsl:template>
<xsl:template match="Page">
<span class="BCTDelimiter">»</span>
<xsl:choose>
<xsl:when test="$DisableCurrentPageLink = 1">
<xsl:choose>
<xsl:when test="#IsOpenPage = 1">
<span class="breadcrumb-link">
<xsl:value-of select="#Title"/>
</span>
</xsl:when>
<xsl:otherwise>
<a href="{#URL}" class="breadcrumb-link" title="{#Title}">
<xsl:value-of select="#Title" />
</a>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<a href="{#URL}" class="breadcrumb-link" title="{#Title}">
<xsl:value-of select="#Title" />
</a>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
[/code]
It's hard without seeing the input form. That code will make an unlinked span for pages that have <page IsOpenPage="1"> as the markup, but does you CMS add that attribute for the relevant pages?
If the currently open page is always last in the trail you could change
<xsl:when test="#IsOpenPage = 1">
to
<xsl:when test="not(following-sibling::Page)">

XSL Sorting With Sitecore

I'm not very familiar with xsl, so I'm sort of stumbling my way though this.
My xsl file is building a menu. I am trying to sort the menu items by the value in menu title field in Sitecore. When I run the code, it does not sort. It just writes out each menu item four times.
Can anyone shed some light on what I am missing?
<xsl:template name="show-title">
<xsl:param name="root" />
<xsl:for-each select="$sc_currentitem/item">
<xsl:sort select="sc:fld('menu title',.)" order="ascending"/>
<xsl:choose>
<xsl:when test="sc:fld('menu title',$root)!=''">
<sc:text field="menu title" select="$root" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$root/#name" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
EDIT: Below is the data that the code above is generating
Example Output:
03/05/201203/05/201203/05/201203/05/2012
03/01/201203/01/201203/01/201203/01/2012
03/08/201203/08/201203/08/201203/08/2012
03/02/201203/02/201203/02/201203/02/2012
03/07/201203/07/201203/07/201203/07/2012
I am trying to get it to generate the following:
03/01/2012
03/02/2012
03/05/2012
03/07/2012
03/08/2012
Thanks!
It looks like you are trying to read menu title field from the wrong node. You should be reading it from context node --> . <--
Try this
<xsl:template name="show-title">
<xsl:param name="root" />
<xsl:for-each select="$sc_currentitem/item">
<xsl:sort select="sc:fld('menu title',.)" order="ascending"/>
<xsl:choose>
<xsl:when test="sc:fld('menu title',$root)!=''">
<sc:text field="menu title" select="." />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="./#name" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
This is just a guess as you don't really supply enough information for anyone to do more than guess but....
Within your for-each you are referring to $root e.g. <xsl:value-of select="$root/#name" />
I am guessing that the $root parameter contains a list of some kind and that you should be selecting just part of this list based on some value from the current for-each context

XSLT xsl:apply-templates Conditional Syntax

I've got the following XSLT code which lists out the folders and their file items from a specified node.
This all works fine but I'd like to parameterise the page and optionally filter its output by a tag value.
Being an XLST numpty I'm stumped with the syntax for the conditional I should be putting in under the <xsl:when test="$tag"> clause - can someone please help ?
<xsl:variable name="tag" select="umbraco.library:Request('tag')" />
<xsl:template match="/">
<!-- Root folder in Media that holds the folders to output -->
<xsl:variable name="mediaRootFolderId" select="5948" />
<!-- Pass in true() to get XML for all nodes below -->
<xsl:variable name="mediaRootNode" select="umbraco.library:GetMedia($mediaRootFolderId, true())" />
<xsl:choose>
<xsl:when test="$tag">
</xsl:when>
<xsl:otherwise>
<!-- If we didn't get an error, output Folder elements that contain Image elements -->
<xsl:apply-templates select="$mediaRootNode[not(error)]/Folder[File]" >
<xsl:sort select="#nodeName"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Template for folders -->
<xsl:template match="Folder">
<div class="folder">
<h2>Folder: <xsl:value-of select="#nodeName" /></h2>
<div class="images">
<xsl:apply-templates select="File">
<xsl:sort select="#nodeName"/>
</xsl:apply-templates>
</div>
</div>
</xsl:template>
<!-- Template for files -->
<xsl:template match="File">
File: <a href="{umbracoFile}" alt="{#nodeName}" ><xsl:value-of select="#nodeName" /></a> <br/>
</xsl:template>
Instead of the long <xsl:choose> instruction, use:
<xsl:apply-templates select=
"$mediaRootNode[not($tag)][not(error)]
/Folder[File]" >
Explanation: For the XPath expression in the select attribute above to select a non-empty set of nodes it is necessary that boolean($tag) is true(). Thus the above single <xsl:apply-templates> instruction is equivalent to the long <xsl:choose> in the question.
you can test if $tag is set like this.
<xsl:param name="tag">
<xsl:message terminate="yes">
$tag has not been set
</xsl:message>
</xsl:param>
This isn't standard though, it'll work on most XSLT processors though.
If you wanted to be absolutely save, you could set the value to an illegal value (such as 1 div 0) and test for it in the body of the template:
<xsl:param name="tag" select="1 div 0" />
<xsl:if test="$tag = 1 div 0">
<xsl:message terminate="yes">
$tag has not been set, or has been set to Infinity, which is invalid.
</xsl:message>
</xsl:if>
Source: O'Reilly XSLT Cookbook