root / trunk / buildscripts / cldrCurrency.xsl

Revision 7576, 7.4 kB (checked in by peller, 21 months ago)

XSLT to generate currency.js tables for dojo.i18n, from Evan Huang (IBM, CCLA). References #2326

Line 
1<?xml version="1.0" encoding="UTF-8"?>
2<xsl:stylesheet xmlns:saxon="http://saxon.sf.net/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" extension-element-prefixes="saxon" version="2.0">
3<xsl:output method="text" indent="yes"/>
4<!-- list the data elements whose spaces should be preserved
5       it seems listing only the parent node doesn't work -->
6<xsl:preserve-space elements="displayName symbol"/>
7<xsl:strip-space elements="*"/>
8
9<!--  currencyList is an external string property like "AUD|BEF|CAD|CHF|CNY|DEM|...|USD",
10        if it is provided, only those currencies in this list will be extracted,
11        otherwise all the currencies will be extracted by default-->
12<xsl:param name="currencyList"></xsl:param>
13<xsl:variable name="first" select="true()" saxon:assignable="yes"/>
14
15<xsl:template match="/">
16     <xsl:apply-templates/>
17</xsl:template>
18
19<!-- process ldml, numbers and currencies -->
20<xsl:template name="top" match="/ldml">
21    <xsl:choose>
22        <xsl:when test="count(./alias)>0">
23            <!-- Handle Alias -->
24            <xsl:for-each select="./alias">
25                <xsl:call-template name="alias_template">
26                    <xsl:with-param name="templateToCall">top</xsl:with-param>
27                    <xsl:with-param name="source" select="@source"></xsl:with-param>
28                    <xsl:with-param name="xpath" select="@path"></xsl:with-param>
29                </xsl:call-template>     
30                </xsl:for-each>
31        </xsl:when>
32        <xsl:otherwise>
33            <xsl:choose>
34                <xsl:when test="name()='currencies'">
35                    <xsl:result-document href="currency.js" encoding="UTF-8">// generated from cldr/ldml/main/*.xml, xpath: ldml/numbers/currencies
36({<xsl:choose><xsl:when test="string-length(string($currencyList))>0">                           
37                                 <xsl:for-each select="currency">
38                                     <xsl:if test="contains($currencyList,@type)">
39                                         <xsl:call-template name="currency"></xsl:call-template>
40                                     </xsl:if>
41                                 </xsl:for-each>
42                         </xsl:when>
43                         <xsl:otherwise>
44                             <xsl:for-each select="currency">
45                                 <xsl:call-template name="currency"></xsl:call-template>                       
46                             </xsl:for-each>
47                         </xsl:otherwise>
48                     </xsl:choose>
49})
50                 </xsl:result-document>
51                </xsl:when>
52                <xsl:otherwise>
53                    <xsl:if test="name()='ldml'">
54                        <!-- ldml -->
55                        <xsl:for-each select="numbers">   
56                            <xsl:call-template name="top"></xsl:call-template>
57                        </xsl:for-each>
58                    </xsl:if>
59                    <xsl:if test="name()='numbers'">
60                        <!-- numbers -->
61                        <xsl:for-each select="currencies">
62                            <xsl:call-template name="top"></xsl:call-template>
63                        </xsl:for-each>
64                    </xsl:if>                 
65                </xsl:otherwise>
66            </xsl:choose>
67         </xsl:otherwise>
68    </xsl:choose>       
69</xsl:template>
70
71    <!-- currency-->
72    <xsl:template name="currency" match="currency">
73    <xsl:param name="width" select="@type"></xsl:param>
74    <xsl:choose>
75        <xsl:when test="count(./alias)>0">
76            <!-- Handle Alias -->
77            <xsl:for-each select="./alias">
78                <xsl:call-template name="alias_template">
79                    <xsl:with-param name="templateToCall">currency</xsl:with-param>
80                    <xsl:with-param name="source" select="@source"></xsl:with-param>
81                    <xsl:with-param name="xpath" select="@path"></xsl:with-param>
82                </xsl:call-template>
83            </xsl:for-each>
84        </xsl:when>
85        <xsl:otherwise>
86        <xsl:if test="count(./* [(not(@draft) or @draft!='provisional' and @draft!='unconfirmed')]) > 0">
87            <xsl:call-template name="insert_comma"/>
88            <xsl:text>
89        </xsl:text>
90            <xsl:value-of select="@type"></xsl:value-of>
91            <xsl:text>:{</xsl:text>
92             <xsl:for-each select="*[not(@draft)] | *[@draft!='provisional' and @draft!='unconfirmed']">
93                    <xsl:value-of select="name()"></xsl:value-of>
94                    <xsl:text>:"</xsl:text>
95                    <xsl:value-of select="replace(.,'&quot;', '\\&quot;')"></xsl:value-of>               
96                    <xsl:text>"</xsl:text>
97                    <xsl:if test="count((following-sibling::node())[not(@draft)]
98                               | *[@draft!='provisional' and @draft!='unconfirmed']) > 0 ">
99                    <xsl:text>, </xsl:text>
100                    </xsl:if>
101             </xsl:for-each>
102            <xsl:text>}</xsl:text>
103            </xsl:if>
104         </xsl:otherwise>   
105        </xsl:choose>
106    </xsl:template>
107   
108    <!-- recursive process for alias -->
109    <xsl:template name="alias_template">
110        <xsl:param name="templateToCall"></xsl:param>
111        <xsl:param name="source"></xsl:param>
112        <xsl:param name="xpath"></xsl:param>
113        <xsl:variable name="cur_width" select="../@type"></xsl:variable>
114       
115        <xsl:choose>           
116            <xsl:when test="compare($source,'locale')=0">
117                <!-- source="locale" -->
118                <xsl:for-each select="saxon:evaluate(concat('../',$xpath))">   
119                    <xsl:call-template name="invoke_template_by_name">
120                        <xsl:with-param name="templateName" select="$templateToCall"></xsl:with-param>
121                    </xsl:call-template>
122                </xsl:for-each>
123            </xsl:when>
124            <xsl:otherwise>
125                <!-- source is an external xml file -->
126                <xsl:if test="string-length($xpath)>0">
127                    <xsl:for-each select="doc(concat($source,'.xml'))">
128                        <xsl:for-each select="saxon:evaluate($xpath)">
129                            <xsl:call-template name="invoke_template_by_name">
130                                <xsl:with-param name="templateName" select="$templateToCall"></xsl:with-param>
131                            </xsl:call-template>
132                        </xsl:for-each>
133                    </xsl:for-each>
134                </xsl:if>           
135            </xsl:otherwise>
136        </xsl:choose>
137    </xsl:template>   
138   
139    <!-- too bad that can only use standard xsl:call-template(name can not be variable)
140         error occurs if use <saxson:call-templates($templateToCall)  /> -->
141    <xsl:template name="invoke_template_by_name">
142        <xsl:param name="templateName"></xsl:param>
143        <xsl:if test="$templateName='top'">
144            <xsl:call-template name="top"></xsl:call-template>
145        </xsl:if>
146        <xsl:if test="$templateName='currency'">
147            <xsl:call-template name="currency"></xsl:call-template>
148        </xsl:if>
149    </xsl:template>   
150
151    <xsl:template name="insert_comma">
152        <xsl:choose>
153            <xsl:when test="$first">
154                <saxon:assign name="first" select="false()"/>
155            </xsl:when>
156            <xsl:otherwise>
157                <xsl:text>,</xsl:text>
158            </xsl:otherwise>
159        </xsl:choose>
160    </xsl:template>
161   
162</xsl:stylesheet>
Note: See TracBrowser for help on using the browser.