I have a comma separated list coming from C#, which I am parsing in XSLT and loading it as drop down. After the user selects the option from the drop down and submits the page, If other fields are not filled in the page, I try to reload the page with the selected option for this drop down.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="parseString">
<xsl:param name="list"/>
<xsl:if test="contains($list, ',')">
<xsl:element name="option">
<xsl:attribute name="value">
<xsl:value-of select="substring-before($list, ',')"/>
</xsl:attribute>
<xsl:value-of select="substring-before($list, ',')"/>
<xsl:if test="substring-before($list, ',')=$carrier">
: sel value
<xsl:attribute name="SELECTED"></xsl:attribute>
</xsl:if>
</xsl:element>
<xsl:call-template name="parseString">
<xsl:with-param name="list" select="substring-after($list, ',')"/>
</xsl:call-template>
</xsl:if>
But upon reload, the selected value in the drop down is not maintained.
But I can see the text - 'sel value' meeting the condition and displayed. For example in the Image you can see the text for carrier - Metro PCS.
Any Help would be appreciated.
Thanks.
EDIT: I have tried multiple ways for selected attribute like
<xsl:attribute name="SELECTED"></xsl:attribute>
<xsl:attribute name="SELECTED">True</xsl:attribute>
<xsl:attribute name="SELECTED">selected</xsl:attribute>
None of them seem to work.
Try swapping these two lines:
: sel value
<xsl:attribute name="SELECTED"></xsl:attribute>
to be
<xsl:attribute name="SELECTED"></xsl:attribute>
: sel value
I think you are trying to add an attribute to the ": sel value" text node which obviously won't work.
EDIT
Taking a closer look at your template I think it is an issue like suggested above (adding an attribute to a text node).
Try this:
<xsl:element name="option">
<xsl:attribute name="value">
<xsl:value-of select="substring-before($list, ',')"/>
</xsl:attribute>
<xsl:if test="substring-before($list, ',')=$carrier">
<xsl:attribute name="SELECTED"></xsl:attribute>
: sel value
</xsl:if>
<xsl:value-of select="substring-before($list, ',')"/>
</xsl:element>
When your if is true it is trying to add an attribute to the text node added by the value-of. All of your attribute additions need to come before adding any child nodes, be they text or otherwise.
Related
I have a variable #expectedLength. I need to assign it to a style attribute.
<xsl:if test="#expectedLength">
<xsl:attribute name="style">
<xsl:value-of select="'width:200px'"/>
</xsl:attribute>
</xsl:if>
I need to replace 200 with the value of #expectedLength. How can I use the variable?
You could change your snippet to
<xsl:if test="#expectedLength">
<xsl:attribute name="style">width: <xsl:value-of select="#expectedLength"/>;</xsl:attribute>
</xsl:if>
That should work with any version of XSLT.
In XSLT 2 and later you can also use the select expression
<xsl:if test="#expectedLength">
<xsl:attribute name="style" select="concat('width: ', #expectedLength, ';')"/>
</xsl:if>
I would prefer to and suggest to set up a template
<xsl:template match="#expectedLength">
<xsl:attribute name="style" select="concat('width: ', #expectedLength, ';')"/>
</xsl:template>
and then to make sure higher up that any attribute nodes are processed.
I am trying to use this code to select all of the pages for this website and if the url has contact in it. It would pull that specific url. I neeed to make it create a link with the url value that the for-each would pull.
<xsl:for-each select="$currentPage/ancestor-or-self::* [#level=$level]/* [#isDoc and string(umbracoNaviHide) != '0'] [contains(#nodeName,'contact')]">
<xsl:value-of select="#nodeName"/>
</xsl:for-each>
In Umbraco the url is stored as url or niceurl (http://our.umbraco.org/wiki/reference/umbracolibrary/niceurl) - the node name is usually used to generate it - but its not the same thing.
Something like the following should work:
<xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[#level = 1]" />
<xsl:variable name="urlString" select="contact" />
<xsl:for-each select="$siteRoot//*[#isDoc][not(umbracoNaviHide = 1)][contains(umbraco.library:NiceUrl($currentPage/../#id), $urlString)]">
<xsl:value-of select="#nodeName"/>
</xsl:for-each>
<xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[#level = 1]" />
<xsl:for-each select="$siteRoot">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="translate(#nodeName,'.','')"/>_contact.aspx
</xsl:attribute>
Contact Us
</xsl:element>
</xsl:for-each>
I just took the main pages name and added the _contact to it instead.
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
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.
Can i use a Select within a concat in xslt?
eg
<xsl:for-each select="root/OrderItems/lineitem">
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of select="concat('http://www.site.com/r&h=11', '&q=',<xsl:value-of select="Quantity" />, )" />
</xsl:attribute>
</xsl:element>
</xsl:for-each>
Try this:
<xsl:for-each select="root/OrderItems/lineitem">
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of
select="concat('http://www.site.com/r&h=11', '&q=', Quantity)" />
</xsl:attribute>
</xsl:element>
</xsl:for-each>
No, because it is not well formed XML, you cannot put a self closing XML element within a self closing XML element, or I suppose in this case you cannot use an XML element in the value of an XML attribute