File: units.xsl

package info (click to toggle)
appstream 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,448 kB
  • sloc: ansic: 51,878; xml: 10,459; cpp: 4,721; python: 538; sh: 264; makefile: 24
file content (78 lines) | stat: -rw-r--r-- 2,631 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?xml version="1.0" encoding="UTF-8"?>
<!--
    Purpose:
     Different templates dealing with numbers and units

    Named Templates:
    * get.value.from.unit(string)
      Returns the number of a string without the unit; for example,
      if a string contains "40mm", using this template will return 40.

    * get.unit.from.unit(string)
      Returns the unit of the string; for example, if a string contains
      "40mm", using this template will return "mm".

    Author:     Thomas Schraitle <toms@opensuse.org>,
                Stefan Knorr <sknorr@suse.de>
    Copyright:  2013

-->
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exsl="http://exslt.org/common"
  exclude-result-prefixes="exsl">
  
  <xsl:template name="get.value.from.unit">
    <xsl:param name="string"/>
    <xsl:variable name="lasttwo" 
                  select="substring($string, string-length($string)-1, 2)"/>

    <!--
    <xsl:message>get.numbers.from.unit:
    string="<xsl:value-of select="$string"/>"
    lasttwo="<xsl:value-of select="$lasttwo"/>"
    </xsl:message>
    -->

    <xsl:choose>
      <!-- These are the possible XSL-FO units -->
      <xsl:when test="$lasttwo = 'cm' or 
                      $lasttwo = 'em' or
                      $lasttwo = 'in' or
                      $lasttwo = 'mm' or
                      $lasttwo = 'pc' or
                      $lasttwo = 'pt' or
                      $lasttwo = 'px'">
        <xsl:value-of select="substring-before($string, $lasttwo)"/>
      </xsl:when>
      <xsl:when test="contains($string, '%')">
        <xsl:value-of select="substring-before($string, '%')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message>Unknown unit in <xsl:value-of select="$string"/></xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="get.unit.from.unit">
    <xsl:param name="string"/>
    <xsl:variable name="lasttwo" 
                  select="substring($string, string-length($string)-1, 2)"/>
    <xsl:choose>
      <!-- These are the possible XSL-FO units -->
      <xsl:when test="$lasttwo = 'cm' or 
                      $lasttwo = 'em' or
                      $lasttwo = 'in' or
                      $lasttwo = 'mm' or
                      $lasttwo = 'pc' or
                      $lasttwo = 'pt' or
                      $lasttwo = 'px'">
        <xsl:value-of select="$lasttwo"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message>Unknown unit in <xsl:value-of select="$string"/></xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>