I have a large number of dealers created as subnodes to my "Dealers" page.
Each of my dealer have a countrycode property like DK, GB or SE, and in my dictionary i have a similar entries for each unique countrycode. They look like:
CountryName.DK CountryName.GB CountryName.SE
This allows my to ge the localized country name like in my XSLT:
My problem is that i need to create a unique list of countries sorted by the localized countryname. I've found some XSLT that gives me the unique countries, but i can see how i can implement sorting.
Hope someone has done this before and can help me out.
First off, I hope you're aware that the Dictionary is there ideally for you to *not* have to do those concatenation things? E.g. - you should be able to just have a single "CountryName" key, and then fill in the values for the Languages you've set up. Umbraco will give you the one that's applicable for the branch of the tree you're in. (If you are aware but your setup can't use it for some reason, please ignore :-).
Secondly, the two main branches in your code (xsl:when $cat = category and the corresponding xsl:otherwise) are identical (which could be because you left something out) - which makes it even harder to see what you're trying to do. (Incidentally, the category you're testing will be taken from the countryCode element being processed - is that what you're expecting?)
Anyway, here's a condensed version including the xsl:sort :
The select on a sort statement is actually an XPath expression like anywhere else - it's executed for every element in the enclosed for-each (or apply-templates), and the resulting value is used for sorting the complete set. You can even use multiple sort statements to achieve the final sorting...
XSLT - Sorting countries
Hi,
I have a large number of dealers created as subnodes to my "Dealers" page.
Each of my dealer have a countrycode property like DK, GB or SE, and in my dictionary i have a similar entries for each unique countrycode. They look like:
CountryName.DK
CountryName.GB
CountryName.SE
This allows my to ge the localized country name like in my XSLT:
<xsl:variable name="countryName" select="umbraco.library:GetDictionaryItem( concat('CountryName.', $currentNode/countryCode) )"/>
My problem is that i need to create a unique list of countries sorted by the localized countryname. I've found some XSLT that gives me the unique countries, but i can see how i can implement sorting.
Hope someone has done this before and can help me out.
Here's what i use for the unsorted unique list:
<select name="dealer-country">
<option>Select country</option>
<xsl:for-each select="umbraco.library:GetXmlNodeById($source)//countryCode[not(.=preceding::countryCode)]">
<xsl:choose>
<xsl:when test="$cat = category">
<option value="{.}">
<xsl:variable name="trans" select="umbraco.library:GetDictionaryItem( concat('CountryName.', .) )"/>
<xsl:choose>
<xsl:when test="string-length($trans) > 0">
<xsl:value-of select="$trans" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</option>
</xsl:when>
<xsl:otherwise>
<option value="{.}">
<xsl:variable name="trans" select="umbraco.library:GetDictionaryItem( concat('CountryName.', .) )"/>
<xsl:choose>
<xsl:when test="string-length($trans) > 0">
<xsl:value-of select="$trans" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</option>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</select>
Again, i hope someone has can give me som pointers, i'm pretty blank.
Regards
Martin
Hi Martin,
First off, I hope you're aware that the Dictionary is there ideally for you to *not* have to do those concatenation things? E.g. - you should be able to just have a single "CountryName" key, and then fill in the values for the Languages you've set up. Umbraco will give you the one that's applicable for the branch of the tree you're in. (If you are aware but your setup can't use it for some reason, please ignore :-).
Secondly, the two main branches in your code (xsl:when $cat = category and the corresponding xsl:otherwise) are identical (which could be because you left something out) - which makes it even harder to see what you're trying to do. (Incidentally, the category you're testing will be taken from the countryCode element being processed - is that what you're expecting?)
Anyway, here's a condensed version including the xsl:sort :
/Chriztian
Hi Martin,
I have 2 suggestions:
1. The easy way: Use jquery to sort your dropdown.
2. Or build up a temp XML file in your XSLT or do your sorting on this:
<xsl:variable name="sortabledealer">
<root xmlns="">
<xsl:for-each select="umbraco.library:GetXmlNodeById($source)//countryCode[not(.=preceding::countryCode)]">
<dealer name="{umbraco.library:GetDictionaryItem( concat('CountryName.', .) )}" value={trans}>
</dealer>
</xsl:for-each>
</root>
</xsl:variable>
Then in your template match:
<xsl:variable name="nodes" select="msxml:node-set($sortabledealer)" />
<xsl:for-each select="$nodes/root/dealer">
<xsl:sort select="@name" order="ascending"/>
Hope this brings you on the right track.
Regards,
Boudewijn
Hi Chriztian,
My initial XSLT was basicly a copy past from the internet, so i didn't quite understand why it worked it just did.
Your XSLT did exactly what i needed and i actually think i understand how it works.
But could you tell why this works?
I though it had to be the elemnt name like countryCode.
// Martin
Hi Martin,
The select on a sort statement is actually an XPath expression like anywhere else - it's executed for every element in the enclosed for-each (or apply-templates), and the resulting value is used for sorting the complete set. You can even use multiple sort statements to achieve the final sorting...
Hope that explains it :-)
/Chriztian
is working on a reply...