Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • MartinB 411 posts 512 karma points
    Jun 29, 2011 @ 21:27
    MartinB
    0

    Thousand seperator in property value

    Hi there

    I have a property where my client enters the number of kilometers that a car have driven. He would like the field to auto seperate numbers above 999 with a . (dot) as is tradition in Denmark.

    Ie: 1.000 km instead of 1000 km

    How can i do that in XSLT on a property value? Searching the forum didn't give me much to go by :/

    <xsl:value-of select="$currentPage/kilometer"/>
  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jun 29, 2011 @ 23:54
    Lee Kelleher
    4

    Hi Martin,

    Because the Danish way differs from the "norm", you will need to do 2 things in your XSLT.

    1. Declare the "decimal-format" towards the top of your XSLT, like so:

    <xsl:decimal-format name="danish" decimal-separator="," grouping-separator="." />

    This effectively swaps over the decimal and thousands group separators.

    2. Use a function called "format-number()", like so...

    <xsl:value-of select="format-number($currentPage/kilometer, '###.###', 'danish')"/>

    The second param defines the formatting, the third param references the decimal-format you declared earlier.

    Cheers, Lee.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jun 30, 2011 @ 17:54
    Chriztian Steinmeier
    0

    @Lee: So who died and made english the "norm" :-)

    /Chriztian

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jun 30, 2011 @ 18:01
    Lee Kelleher
    0

    Wow, hand on heart, I stand corrected... seems that there are more countries that use thousand-separator point and decimal comma, than my "norm"!

    http://en.wikipedia.org/wiki/Decimal_mark#Hindu-Arabic_numeral_system

    LOL!

    Cheers, Lee.

  • MartinB 411 posts 512 karma points
    Jul 06, 2011 @ 11:31
    MartinB
    0

    Hi Lee

    Thanks a bunch. My client went on holiday so i totally forgot this :)

    You're a star

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jul 25, 2011 @ 14:34
    Bjarne Fyrstenborg
    0

    Hi Lee..

    Nice tip :)
    Do you have a suggestion, when having a multilingual site?

    <xsl:decimal-format name="danish" decimal-separator="," grouping-separator="." />
    <xsl:variable name="size" select="./umbracoBytes" />
    <xsl:variable name="sizeAndSuffix">
            <xsl:choose>
                    <xsl:when test="$size &gt;= 1073741824">
                            <xsl:value-of select="format-number($size div 1073741824,'###.###,##', 'danish')"/>
                            <xsl:textGB</xsl:text>
                    </xsl:when>
                    <xsl:when test="$size &gt;= 1048576">
                            <xsl:value-of select="format-number($size div 1048576,'###.###,##', 'danish')"/>
                            <xsl:textMB</xsl:text>
                    </xsl:when>
                    <xsl:when test="$size &gt;= 1024">
                            <xsl:value-of select="format-number($size div 1024,'###.###,##', 'danish')"/>
                            <xsl:textKB</xsl:text>
                    </xsl:when>
                    <xsl:when test="$size &gt; 0 and $size &lt; 1024">
                            <xsl:value-of select="format-number($size div 0,'###.###,##', 'danish')"/>
                            <xsl:textBytes</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                            <xsl:text>0 Bytes</xsl:text>
                    </xsl:otherwise>
            </xsl:choose>
    </xsl:variable>

    I would like to use the Danish format, when you have selected Danish as language and English format for the English version of site.

    Bjarne

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 25, 2011 @ 15:15
    Lee Kelleher
    1

    Hi Bjarne,

    Here is one way to do it (there are other ways too).

    Have both references to the decimal format:

    <xsl:decimal-format name="danish" decimal-separator="," grouping-separator="." />
    <xsl:decimal-format name="english" decimal-separator="." grouping-separator="," />

    Figure out which language is being used and assign that to a variable:

    <xsl:variable name="language" select=" ... however you select your language - make sure the output is 'danish' or 'english' ... " />

    Then you can use that variable in the "format-number" function:

    <xsl:value-of select="format-number(umbracoBytes, '###.###', $language)"/>

    Alternatively, if you are using uComponents, then there is an XsltExtension in the IO library called FormatFileSize - it might be useful?

    Cheers, Lee

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jul 25, 2011 @ 15:44
    Bjarne Fyrstenborg
    0

    Okay, thanks.. I see the idea..

    I'm not sure how the select the language.. do you have an example on this? Should I use the umbraco library method or?

    I really haven't worked with uComponents yet, but yes it might be useful and it has many features I will need in the future.
    But I would like if it possible to get the language just with xslt ...

    Bjarne

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 25, 2011 @ 15:50
    Lee Kelleher
    0

    Hi Bjarne,

    Difficult to offer a solution with knowing more about your set-up, but here's a suggestion...

    If the Danish URLs are on a ".dk" domain, then you could check for that? Otherwise it fallback on English?

    <xsl:variable name="language">
        <xsl:choose>
            <xsl:when test="contains(umbraco.library:RequestServerVariables('HTTP_HOST'), '.dk')">danish</xsl:when>
            <xsl:otherwise>english</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    Cheers, Lee.

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jul 25, 2011 @ 16:27
    Bjarne Fyrstenborg
    0

    Well, both languages are on fonnesberg.com, but under /en and /da ...

    So I have press photos here: http://fonnesberg.com/en/press/download/photos.aspx which are available for customers and others to use as press material.

    But perhaps I can use the same way to check if the url contains a .com/da as well?

    Bjarne 

     

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 25, 2011 @ 16:39
    Lee Kelleher
    0

    Hi Bjarne,

    Swap the test condition with this...

    starts-with(umbraco.library:RequestServerVariables('URL'), '/da/')

    Cheers, Lee.

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jul 25, 2011 @ 17:03
    Bjarne Fyrstenborg
    0

    Thanks, it  almost works now.. :)

    But there is a small problem with the decimals.. when I use ###.### I get two decimal in English.. and with ###.###,## I get two decimals in Danish..

    If you look at this page in Danish: http://fonnesberg.com/da/presse/download/billeder/efteraar-2011.aspx I get e.g. 11,36 MB ...  on the English version: http://fonnesberg.com/en/press/download/photos/autumn-2011.aspx 11.35964 MB ... if I use ###.### I get 11 MB and 11.36 MB ...

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 25, 2011 @ 17:11
    Lee Kelleher
    1

    hmmmm... try "###,###.##"?

    If you always wanted to show the decimal places, then use "###,###.00".

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jul 25, 2011 @ 17:21
    Bjarne Fyrstenborg
    0

    hm.. still not same numbers of decimals :/

    Even not with ###,###

    Thanks for the tip with ###,###.00 ...so you can keep zero's at the end.. when I use ###,###.00 it works on the English version.. but it the xslt crash on the Danish version of site..

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 25, 2011 @ 17:26
    Lee Kelleher
    0

    Hmmm... not sure what the answer is. (but I didn't want to leave you hanging here.)  If I think of anything, I'll let you know.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jul 25, 2011 @ 23:11
    Chriztian Steinmeier
    0

    Hi Bjarne,

    The "crashing" on the danish version is because you need to specify the pattern string in "danish" (i.e., using the characters you've specified for the named decimal-format you're using) - so you'll need to use ###.###,00 in the danish version.

    /Chriztian

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jul 25, 2011 @ 23:21
    Bjarne Fyrstenborg
    0

    Hi Chriztian

    Yes I have tried with ###.###,00 but it will crash xslt on the English version of site then...
    I think the way Lee suggests should use the different pattern for the file size with comma and dot.

    I also get e.g. 11.36 MB in English, but somehow I get more decimals in Danish, 11,35964 MB ... it seems to give me to right output, but not same number of decimals..

    Bjarne

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jul 25, 2011 @ 23:30
    Chriztian Steinmeier
    2

    OK - what I actually mean is that you need to use BOTH patterns - but the right one for each language - does that make sense?

    I've done something like this before:

    <!-- :: Decimal formats :: -->
    <xsl:decimal-format name="prices-da"
        decimal-separator=","
        grouping-separator="."
        NaN="(Ikke tilgængelig)"
    />
    <xsl:decimal-format name="prices-en"
        decimal-separator="."
        grouping-separator=","
        NaN="(Not available)"
    />
    
    <!-- Build a single variable $formats to hold info for use e.g. when formatting prices -->
    <xsl:variable name="format-mapper">
        <formats>
            <prices name="english" currency="GBP" xml:lang="en">
                <pattern>#,###.00</pattern>
            </prices>
            <!-- Must be last as this is the one we want to fallback to! -->
            <prices name="danish" currency="DKK" xml:lang="da">
                <pattern>#.###,00</pattern>
            </prices>
        </formats>
    </xsl:variable>
    <xsl:variable name="formats" select="msxml:node-set($format-mapper)/formats" />
    
    <!-- Formats the contents of any element as a price using the currently selected format -->
    <xsl:template match="*" mode="formatted-price">
        <xsl:variable name="currentFormat" select="($formats/prices[lang($lang)] | $formats/prices[lang('da')])[1]" />
        <xsl:value-of select="substring(concat($currentFormat/@currency, ' '), (string(number(.)) = 'NaN') * 5 + 1, 4)" />
        <xsl:value-of select="format-number(., $currentFormat/pattern, concat('prices-', $currentFormat/@xml:lang))" />           
    </xsl:template>
    

    And then to format a price:

    <xsl:apply-templates select="priceProperty" mode="formatted-price" />

    Let me know if it's total gibberish to you :-)

    /Chriztian

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jul 25, 2011 @ 23:49
    Bjarne Fyrstenborg
    0

    Yes, it makes more sense now :)

    Well, I have this right now:

    <xsl:decimal-format name="danish" decimal-separator="," grouping-separator="." />
    <xsl:decimal-format name="english" decimal-separator="." grouping-separator="," />
    <xsl:variable name="language">
            <xsl:choose>
              <xsl:when test="starts-with(umbraco.library:RequestServerVariables('URL'), '/da')">danish</xsl:when>
              <xsl:otherwise>english</xsl:otherwise>
            </xsl:choose>
    </xsl:variable>

    and this inside my loop..

    <xsl:variable name="size" select="./umbracoBytes" />
    <xsl:variable name="sizeAndSuffix">
            <xsl:choose>
                    <xsl:when test="$size &gt;= 1073741824">
                            <xsl:value-of select="format-number($size div 1073741824,'###,###.##', $language)"/>
                            <xsl:textGB</xsl:text>
                    </xsl:when>
                    <xsl:when test="$size &gt;= 1048576">
                            <xsl:value-of select="format-number($size div 1048576,'###,###.##', $language)"/>
                            <xsl:textMB</xsl:text>
                    </xsl:when>
                    <xsl:when test="$size &gt;= 1024">
                            <xsl:value-of select="format-number($size div 1024,'###,###.##', $language)"/>
                            <xsl:textKB</xsl:text>
                    </xsl:when>
                    <xsl:when test="$size &gt; 0 and $size &lt; 1024">
                            <xsl:value-of select="format-number($size div 0,'###,###.##', $language)"/>
                            <xsl:textBytes</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                            <xsl:text>0 Bytes</xsl:text>
                    </xsl:otherwise>
            </xsl:choose>
    </xsl:variable>

    how much of your code above would I have to integrate then?

    Bjarne

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Sep 06, 2011 @ 19:48
    Bjarne Fyrstenborg
    0

    I guess I need to use the first part you wrote Chriztian:

     

    <!-- :: Decimal formats :: -->
    <xsl:decimal-formatname="prices-da"
           
    decimal-separator=","
           
    grouping-separator="."
            N
    aN="(Ikke tilgængelig)"
    />
    <xsl:decimal-formatname="prices-en"
           
    decimal-separator="."
           
    grouping-separator=","
            N
    aN="(Not available)"
    />

    <!-- Build a single variable $formats to hold info for use e.g. when formatting prices -->
    <xsl:variablename="format-mapper">
           
    <formats>
                   
    <pricesname="english"currency="GBP"xml:lang="en">
                           
    <pattern>#,###.00</pattern>
                   
    </prices>
                   
    <!-- Must be last as this is the one we want to fallback to! -->
                   
    <pricesname="danish"currency="DKK"xml:lang="da">
                           
    <pattern>#.###,00</pattern>
                   
    </prices>
           
    </formats>
    </xsl:variable>
    <xsl:variablename="formats"select="msxml:node-set($format-mapper)/formats"/>

     

    Can you see how I shall modify the code so it fits?

    You can see how it's working here: http://fonnesberg.com/en/press/download/photos.aspx  and here: http://fonnesberg.com/da/presse/download/billeder.aspx when you hover the mouse over the thumbnails, you'll see the name and filesize of image.. but you can also see that on the english page I got two decimals and on the danish site version about 4-5 decimals..

    Bjarne

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Sep 07, 2011 @ 00:51
    Chriztian Steinmeier
    0

    Hi Bjarne,

    Here's something along the lines of how I'd do it:

    <!-- :: Decimal formats :: -->
        <xsl:decimal-format name="da" decimal-separator="," grouping-separator="." />
        <xsl:decimal-format name="en" decimal-separator="." grouping-separator="," />
    
        <!-- Build a single variable $formats to hold info for use e.g. when formatting numbers -->
        <xsl:variable name="format-mapper">
            <formats>
                <pattern xml:lang="en">#,###.00</pattern>
                <!-- Fallback must be last -->
                <pattern xml:lang="da">#.###,00</pattern>
            </formats>
        </xsl:variable>
        <xsl:variable name="formats" select="msxml:node-set($format-mapper)/formats" />
    
    <!-- Lookup table for size thresholds -->
        <xsl:variable name="sizes-mapper">
            <size id="giga" abbr=" GB">1073741824</size>
            <size id="mega" abbr=" MB">1048576</size>
            <size id="kilo" abbr=" KB">1024</size>
            <size id="byte" abbr=" Bytes">1</size>
        </xsl:variable>
        <xsl:variable name="sizes" select="msxml:node-set($sizes-mapper)" />
    
        <!-- Formats the contents of any element as a storage value using the currently selected format -->
        <xsl:template match="*" mode="storage">
            <xsl:variable name="currentFormat" select="($formats/pattern[lang($lang)] | $formats/pattern[lang('da')])[1]" />
            <xsl:variable name="divisor" select="$sizes/size[. &lt;= current()][1]" />
    
            <xsl:value-of select="format-number((. div $divisor), $currentFormat, $currentFormat/@xml:lang)" />
            <xsl:value-of select="$divisor/@abbr" />
        </xsl:template>
    

    So when you need to output a storage value, just apply-templates to it in the "storage" mode, e.g.:

    <xsl:apply-templates select="umbracoBytes" mode="storage" />

    /Chriztian

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Sep 07, 2011 @ 16:15
    Bjarne Fyrstenborg
    0

    Okay.. is it possible to still use the sizeAndSuffix variable:

    <xsl:variable name="size" select="./umbracoBytes" />
    <xsl:variable name="sizeAndSuffix">
            <xsl:choose>
                    <xsl:when test="$size &gt;= 1073741824">
                            <xsl:value-of select="format-number($size div 1073741824,'###,###.##', $language)"/>
                            <xsl:text> GB</xsl:text>
                    </xsl:when>
                    <xsl:when test="$size &gt;= 1048576">
                            <xsl:value-of select="format-number($size div 1048576,'###,###.##', $language)"/>
                            <xsl:text> MB</xsl:text>
                    </xsl:when>
                    <xsl:when test="$size &gt;= 1024">
                            <xsl:value-of select="format-number($size div 1024,'###,###.##', $language)"/>
                            <xsl:text> KB</xsl:text>
                    </xsl:when>
                    <xsl:when test="$size &gt; 0 and $size &lt; 1024">
                            <xsl:value-of select="format-number($size div 0,'###,###.##', $language)"/>
                            <xsl:text> Bytes</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                            <xsl:text>0 Bytes</xsl:text>
                    </xsl:otherwise>
            </xsl:choose>
    </xsl:variable>

    But I don't know if the I then still should have the $language variable there...

    How much should be placed inside the loop?

    Bjarne

Please Sign in or register to post replies

Write your reply to:

Draft