source: main/trunk/greenstone3/web/interfaces/core/transform/preProcess.xsl@ 25880

Last change on this file since 25880 was 25880, checked in by kjdon, 12 years ago

invented new gsvar: namespace, to get variables out of gslib

File size: 5.0 KB
Line 
1<?xml version="1.0" encoding="UTF-8"?>
2
3<xsl:stylesheet version="1.0"
4 xmlns:gsf="http://www.greenstone.org/greenstone3/schema/ConfigFormat"
5 xmlns:gslib="http://www.greenstone.org/skinning"
6 xmlns:gsvar="http://www.greenstone.org/skinning-var"
7 xmlns:java="http://xml.apache.org/xslt/java"
8 xmlns:util="xalan://org.greenstone.gsdl3.util.XSLTUtil"
9 xmlns:xalan="http://xml.apache.org/xalan"
10 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
11 xmlns:xslt="output.xsl"
12 xmlns:gs3="http://www.greenstone.org/gs3"
13 >
14
15 <xsl:output method="xml"/>
16 <xsl:namespace-alias
17 stylesheet-prefix="xslt" result-prefix="xsl"/>
18
19 <xsl:template match="/">
20
21 <!-- explicitly output the stylesheet element here so we can include the
22 namespace declarations. They were going missing before with the new xalan/xerces-->
23 <xslt:stylesheet>
24
25 <xsl:for-each select="/skinAndLibraryXsl/skinXsl/xsl:stylesheet">
26
27 <!-- produce an exact copy of skin stylesheet, with gslib nodes expanded. -->
28 <xsl:for-each select="@*">
29 <xsl:variable name="attribute-name" select="name()"/>
30 <xsl:attribute name="{$attribute-name}">
31 <xsl:value-of select="."/>
32 </xsl:attribute>
33 </xsl:for-each>
34 <!-- merge the attributes of the library stylesheet -->
35 <xsl:for-each select="/skinAndLibraryXsl/libraryXsl/xsl:stylesheet/@*">
36 <xsl:variable name="attribute-name" select="name()"/>
37 <xsl:attribute name="{$attribute-name}">
38 <xsl:value-of select="."/>
39 </xsl:attribute>
40 </xsl:for-each>
41
42 <xsl:call-template name="expand_gslib_elements" />
43
44 <!-- add content of library to the skin stylesheet -->
45 <xsl:for-each select="/skinAndLibraryXsl/libraryXsl/xsl:stylesheet">
46 <xsl:call-template name="expand_gslib_elements" />
47 </xsl:for-each>
48
49 </xsl:for-each>
50 </xslt:stylesheet>
51
52
53 </xsl:template>
54
55
56 <!-- produce an exact copy of the current node, but expand and replace all elements belonging to the gslib namespace. -->
57 <xsl:template name="expand_gslib_elements">
58
59
60 <xsl:for-each select="*|text()">
61 <xsl:choose>
62
63 <xsl:when test="namespace-uri(.)=namespace::gsvar">
64 <xsl:element name="xsl:value-of">
65 <xsl:attribute name="select">$<xsl:value-of select="local-name()" /></xsl:attribute>
66 </xsl:element>
67</xsl:when>
68 <!-- if node has gslib prefix, expand it into appropriate copy-of or call-template element -->
69 <xsl:when test="namespace-uri(.)=namespace::gslib">
70
71 <xsl:variable name="name" select="local-name()"/>
72 <xsl:choose>
73
74 <!-- if library contains a variable of this name, expand to it's value -->
75 <xsl:when test="/skinAndLibraryXsl/libraryXsl//xsl:variable[@name = $name]">
76 <xsl:element name="xsl:copy-of">
77 <xsl:attribute name="select">$<xsl:value-of select="local-name()" /></xsl:attribute>
78 </xsl:element>
79 </xsl:when>
80
81
82 <!-- if library contains a template of this name, expand to a call-template and pass attributes as parameters -->
83 <xsl:otherwise>
84 <xsl:element name="xsl:call-template">
85 <xsl:attribute name="name"> <xsl:value-of select="local-name()" /></xsl:attribute>
86
87 <xsl:for-each select="@*">
88 <xsl:element name="xsl:with-param">
89 <xsl:attribute name="name"><xsl:value-of select="name()"/></xsl:attribute>
90 <xsl:call-template name="convert_attVal_to_valueOf" />
91 </xsl:element>
92 </xsl:for-each>
93 </xsl:element>
94 </xsl:otherwise>
95
96 </xsl:choose>
97 </xsl:when>
98
99 <xsl:when test="self::text()">
100 <xsl:value-of select="."/>
101 </xsl:when>
102
103 <!-- if a regular node v2 -->
104 <xsl:otherwise>
105 <xsl:variable name="element-name" select="name()"/>
106 <xsl:variable name="element-namespace" select="namespace-uri()"/>
107 <xsl:element name="{$element-name}" namespace="{$element-namespace}">
108 <xsl:for-each select="@*">
109 <xsl:variable name="attribute-name" select="name()"/>
110 <xsl:attribute name="{$attribute-name}">
111 <xsl:value-of select="."/>
112 </xsl:attribute>
113 </xsl:for-each>
114 <xsl:call-template name="expand_gslib_elements" />
115 </xsl:element>
116 </xsl:otherwise>
117
118
119 </xsl:choose>
120 </xsl:for-each>
121
122 </xsl:template>
123
124 <!-- converts an attribute value (the current node) into either a literal value or an <xsl:value-of> element -->
125 <xsl:template name="convert_attVal_to_valueOf">
126
127 <xsl:choose>
128 <!-- if attribute is not literal (starts with a "{") -->
129 <xsl:when test="starts-with(string(), '&#123;')">
130 <xsl:element name="xsl:value-of">
131 <xsl:attribute name="disable-output-escaping">yes</xsl:attribute>
132 <xsl:attribute name="select"><xsl:value-of select="substring(string(),2,string-length()-2)"/></xsl:attribute>
133 </xsl:element>
134 </xsl:when>
135 <xsl:otherwise>
136 <xsl:value-of select="."/>
137 </xsl:otherwise>
138 </xsl:choose>
139 </xsl:template>
140
141 <xsl:template name="create_dummy_variables">
142 <xsl:for-each select="//*[namespace-uri()=namespace::gslib]">
143 <xsl:element name="xsl:variable">
144 <xsl:attribute name="name">
145 <xsl:value-of select="local-name()"/>
146 </xsl:attribute>
147 </xsl:element>
148 </xsl:for-each>
149 </xsl:template>
150
151
152</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.