XSL where statement - xslt

I'm looking for some help with an XSL selector:
What I need is a selector that will show the Title of a different list where the Document of the same row matches the Name field. If there is no entry, I will show a link to create a new one. Here is what I have:
<xsl:choose>
<xsl:when test="/dsQueryResponse
/Change_Types
/Rows
/Row
/#Document = #Name"/>
<xsl:value-of select="/dsQueryResponse
/Change_Types
/Rows
/Row
/#Document[
/dsQueryResponse
/Change_Types
/Rows
/Row
/#Document = #Name
]"/>
</xsl:when>
<xsl:otherwise>
<!-- Code to show link -->
</xsl:otherwise>
</xsl:choose>
If anyone could point out where I'm going wrong, it would be greatly appreciated!

In the absence of your source XML it's a complete guess, but I suspect that
#Document = #Name
should be
#Document = current()/#Name
on both occasions. Unless you really do want the Document and Name attributes of the same element to have the same value.

Here's how I did it:
A variable needed to be created for #FileLeafRef so it could be preserved to test against inside the for-each.
<xsl:variable name="document" select="#FileLeafRef"/>
<xsl:choose>
<!-- If there is an entry in the 'Tickets' list for this #FileLeafRef -->
<xsl:when test="/dsQueryResponse/Tickets/Rows/Row/#Document = $document">
<!-- Show it here -->
<xsl:for-each select="/dsQueryResponse/Tickets/Rows/Row">
<xsl:if test="#Document = $document">
<xsl:value-of select="#Title"/>
</xsl:if>
</xsl:for-each>
</xsl:when>
<!-- Else, show a link to add a new Ticket with the document auto-populated -->
<xsl:otherwise>
<xsl:call-template name="addNewItemLink">
<xsl:with-param name="list" select="'Tickets'"/>
<xsl:with-param name="document" select="#FileLeafRef"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>

Related

How to conditionally output tags?

Tags can be outputted by either directly typing
<div>
<span>complex...</span>
</div>
or using <xsl:element>,
<xsl:element name="div">
<span>complex...</span>
</xsl:element>
My question is how to do this: when x, output <div>, when y, output <a>, when z, output no tag?
One of course can make three templates, or even write ugly code as
<xsl:when ...x >
<![CDATA[ <div> ]]>
</xsl:when>
<span>complex...</span>
<xsl:when ...x >
<![CDATA[ </div> ]]>
</xsl:when>
but is there a way to conditionally provide the value of the name attribute of xsl:element?
I tried this, failed:
<xsl:variable name="a" select="'div'"/>
<xsl:element name="$a">
...
[edited] Forgot to say, XSLT1.0 only
Here's another way to look at it:
<xsl:variable name="content">
<span>complex...</span>
</xsl:variable>
<xsl:choose>
<xsl:when ... x>
<div>
<xsl:copy-of select="$content"/>
</div>
</xsl:when>
<xsl:when ... y>
<a>
<xsl:copy-of select="$content"/>
</a>
</xsl:when>
<xsl:when ... z>
<xsl:copy-of select="$content"/>
</xsl:when>
</xsl:choose>
The name attribute is not expecting a full-fledge XPath expression but simply a string. So, instead of using name="$a" you only have to evaluate the Xpath expression into a string by bracing it with curly braces:
<xsl:element name="{$a}">
As for the conditional creation of the surrounding tag you could do something like this:
<xsl:variable name="tag_name">
<xsl:choose>
<xsl:when test="x">
<xsl:text>div</xsl:text>
</xsl:when>
<xsl:when test="y">
<xsl:text>a</xsl:text>
</xsl:when>
</xsl:choose>
<!-- possibly other checks for different tag names -->
<xsl:variable>
<xsl:choose>
<xsl:when test="$tag_name != ''">
<xsl:element name="$tag_name">
<!-- whatever has to be put into a tagged block (A) -->
</xsl:element>
</xsl:when>
<xsl:otherwise>
<!-- whatever has to be put into a untagged block (B) -->
</xsl:otherwise>
</xsl:choose>
If A and B are equal you could put that into a template.
XSLT doesn't output tags: it outputs nodes to a result tree. Your suggestion of using constructs like <![CDATA[ </div> ]]> is therefore way off the mark: you can't add half a node to a tree.
However, there's no difficulty with conditional generation of elements nodes. If you want to create an element but compute its name conditionally, then in XSLT 2.0 you can do
<xsl:element name="{if (test) then 'a' else 'b'}">
or if you're stuck with 1.0, the much more verbose
<xsl:variable name="elname">
<xsl:choose>
<xsl:when test="test">a</xsl:when>
<xsl:otherwise>b</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$elname}"/>
If you want to output an element or nothing depending on a condition, just do
<xsl:if test="test2">
<e/>
</xsl:if>

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

Using Multiple Nested Attributes in XSLT

What is the best way to construct nested attributes in XSL?
My issue is that onmouseover is an attribute and the src of img is an attribute. The current error given by the builder is:
An item of type 'Element' cannot be constructed within a node of type 'Attribute'.
I used to have an issue of multiple attributes which would have been my preferred route but throws an error:
Attribute and namespace nodes cannot be added to the parent element after a text, comment, pi, or sub-element node has already been added.
I have since attempted the following as a workaround but with no luck
<xsl:template name="Item3">
<xsl:param name="ItemID" />
<xsl:variable name="IMGSRC">
<xsl:choose>
<xsl:when test="$ItemID = 'ST-18/NM/NM/36'">
<xsl:value-of select="concat('imagesCategories/','ST-18-NM-NM-36','.jpg')"/>
</xsl:when>
<xsl:when test="$ItemID = 'ST-18/NM/NM/48'">
<xsl:value-of select="concat('imagesCategories/','ST-18-NM-NM-48','.jpg')"/>
</xsl:when>
<xsl:when test="$ItemID = 'ST-18/NM/NM/72'">
<xsl:value-of select="concat('imagesCategories/','ST-18-NM-NM-72','.jpg')"/>
</xsl:when>
<xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/12'">
<xsl:value-of select="concat('imagesCategories/','ST18-SMAM-SMAM-12','.jpg')"/>
</xsl:when>
<xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/24'">
<xsl:value-of select="concat('imagesCategories/','ST18-SMAM-SMAM-24','.jpg')"/>
</xsl:when>
<xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/36'">
<xsl:value-of select="concat('imagesCategories/','ST18-SMAM-SMAM-36','.jpg')"/>
</xsl:when>
<xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/48'">
<xsl:value-of select="concat('imagesCategories/','ST18-SMAM-SMAM-48','.jpg')"/>
</xsl:when>
<xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/60'">
<xsl:value-of select="concat('imagesCategories/','ST18-SMAM-SMAM-60','.jpg')"/>
</xsl:when>
<xsl:when test="$ItemID = 'ST-18/SMAM/SMAM/72'">
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('imagesCategories/',$ItemID,'.jpg')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="Items/Item[#ItemID=$ItemID]">
<xsl:attribute name="onmouseover">
<xsl:text>ddrivetip('</xsl:text>
<img src="{$IMGSRC}"/>
<br />
<b>
<xsl:value-of select="Items/Item[#ItemID=$ItemID]/#ItemID" />
</b>
<br />
<b>
<xsl:value-of select="Items/Item[#ItemID=$ItemID]/#ItemDescription" />
</b>
<br />
<br />
<xsl:text>Price (01-09): </xsl:text>
<xsl:value-of select="Items/Item[#ItemID=$ItemID]/#PriceLevel1" />
<br/>
<xsl:text>Price (10-24): </xsl:text>
<xsl:value-of select="Items/Item[#ItemID=$ItemID]/#PriceLevel2" />
<br/>
<xsl:text>Price (25-49): </xsl:text>
<xsl:value-of select="Items/Item[#ItemID=$ItemID]/#PriceLevel3" />
<br/>
<xsl:text>Qty In Stock: </xsl:text>
<xsl:value-of select="Items/Item[#ItemID=$ItemID]/#QtyOnHand" />
<br />
<br />
<xsl:text>Click </xsl:text>
<b>
<xsl:text>"BUY!"</xsl:text>
</b>
<xsl:text> to add this item to your shopping cart</xsl:text>
<xsl:text>', '', '300')</xsl:text>
</xsl:attribute>
There is some additional code and then the proper closing tags.
Thanks everyone!
It looks like you are trying to pass html as a string to your ddrivetip function. However, you are adding these as nodes instead of text, and nodes cannot be added added to attributes, so one solution is to make the nodes text (You'll have to escape the brackets and double quotes too).
However, you are putting a lot of information into the onmouseover event, which is not recommended. Instead of what you are currently doing, I would make a hidden element with an id that incorporates your itemId with the contents of your html and then show that as needed in your onmouseover event.
Use CDATA sections so that the XSLT Processor interpret your img tags as a part of text nodes and not as an attempt to insert element nodes into an attribute (it is forbidden by the XML specification)
<xsl:attribute name="onmouseover">
<xsl:text><![CDATA[ddrivetip('<img src="]]></xsl:text>
<xsl:value-of select="$IMGSRC" />
<xsl:text><![CDATA["/>
<br />
...

Pass string into template parameter which expects node?

So,
I have an XSLT template which expects a node set as a parameter and uses this as display text. However, sometimes this node is empty in the XML and I want to pass default display text instead of the display text not showing up instead:
Works:
<xsl:call-template name="myTemplate">
<xsl:with-param name="parm1" select="//element">
</xsl:call-template>
Doesn't work:
<xsl:variable name="dispText">
<xsl:choose>
<xsl:when test="string-length(//element) = 0">
<xsl:value-of select="'Default Text'" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="//element" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:call-template name="myTemplate">
<xsl:with-param name="parm1" select="$dispText">
</xsl:call-template>
Any ideas as to how I could accomplish this? I've tried all sorts of things with no luck :(
It seems like all I need to do is create a new node with the display text I want, but I don't know if that is even possible?
Thanks
Implement the default handling in the template, because that's where it belongs. The calling side should be consistent and not have side-effects on the template behavior (i.e. you should not be able to "forget" passing in the default value).
<xsl:template name="myTemplate">
<xsl:param name="parm1" /><!-- node set expected! -->
<!-- actual value or default -->
<xsl:variable name="value1">
<xsl:choose>
<xsl:when test="not($parm1 = '')">
<xsl:value-of select="$parm1" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$default1" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- work with $value1 from this point on -->
</xsl:template>
I'm guessing //element is a nodeset and using string-length() on it might not be valid. Try converting it to a string() first?

Different way of sorting a list, depending on a variable in XSLT

I am making a newslist, and have until now sorted the news after their date. Newest first.
But I would like to give the administrator of the site a more flexible solution. This means that in the backend, the admin can choose from a dropdown-list, in wich way he/she want's the list to be sorted. By date(newest first and oldest first), or by title (A-Z and Z-A). This means 4 possible ways right.
Right now I have the following XSLT:
<xsl:variable name="alleNyheder" select="$currentPage//node" />
<xsl:variable name="sort">
<news>
<xsl:for-each select="$alleNyheder[#template='1092']">
<news>
<id>
<xsl:value-of select="./#id"></xsl:value-of>
</id>
<date>
<xsl:choose>
<xsl:when test="./data[#alias='date'] != ''">
<xsl:value-of select="./data[#alias='date']"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="./#createDate"/>
</xsl:otherwise>
</xsl:choose>
</date>
</news>
</xsl:for-each>
</news>
</xsl:variable>
<xsl:for-each select="msxml:node-set($sort)/news/news">
<xsl:sort data-type="text" select="date" order="descending" />
---- My newsitems ----
</xsl:for-each>
So in the moment I sort my list after the "date"-value in my variable $sort.
If I change the "date"-field in $sort, to titles instead of date's I can actually sort my list after the titles of the news. But unfortunately it should be sorted in an ascending order, instead of a descending order. And I can't figure out how to change the order-value dynamically like I do in the select-value.
If it helps anyone, I am working on Umbraco CMS.
Thanks
-Kim
<xsl:choose>
<xsl:when test="$sortfield = 'date' and $sortorder = 'D'>
<xsl:for-each select="msxml:node-set($sort)/news/news">
<xsl:sort data-type="text" select="date" order="descending" />
<!-- ... -->
</xsl:for-each>
</xsl:when>
<xsl:when test="$sortfield = 'date' and $sortorder = 'A'>
<xsl:for-each select="msxml:node-set($sort)/news/news">
<xsl:sort data-type="text" select="date" order="ascending" />
<!-- ... -->
</xsl:for-each>
</xsl:when>
<!-- ... -->
</xsl:choose>
On a different note, you really should look into <xsl:apply-templates> and avoid <xsl:for-each>. Your code gets cleaner and a lot more idiomatic this way. I am also sure that the entire temporary node-set() business is completely avoidable.