1 |
<?xml version="1.0" encoding="ISO-8859-1"?> |
2 |
|
3 |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
4 |
|
5 |
<!-- |
6 |
|
7 |
Purpose of this XML Stylesheet is (was) to implement a set of templates |
8 |
to do simple xml node manipulation. (kinda XML API via XSLT) |
9 |
Main target is "flexible insertion of new xml child nodes". |
10 |
|
11 |
If this attempt works out well, it will be continued by a follow-up |
12 |
stylesheet trying to implement XUpdate in XSLT. |
13 |
|
14 |
Note: The attempt indeed seems to work out well, so this stylesheet |
15 |
became deprecated now. Work continues inside "xupdate2xsl.xml". |
16 |
|
17 |
--> |
18 |
|
19 |
<xsl:output method="raw"/> |
20 |
|
21 |
<!-- 1. This is the passthru logic (copy all untouched nodes). --> |
22 |
<xsl:template name="passthru"><xsl:copy><xsl:apply-templates /></xsl:copy></xsl:template> |
23 |
<!-- activate this --> |
24 |
<xsl:template match="*"><xsl:call-template name="passthru" /></xsl:template> |
25 |
<!-- override some builtin rules: see http://www.w3.org/TR/xslt#built-in-rule --> |
26 |
<xsl:template match="comment()"><xsl:call-template name="passthru" /></xsl:template> |
27 |
|
28 |
<!-- 2. This is the crud API. --> |
29 |
|
30 |
<!-- 2.a. The context finder. --> |
31 |
|
32 |
<!-- query part one: the node name --> |
33 |
<xsl:template match="source"> |
34 |
<xsl:choose> |
35 |
<!-- query part two: the attrib key and value --> |
36 |
<xsl:when test="@key=expekt"> |
37 |
<xsl:call-template name="crud" > |
38 |
<xsl:with-param name="action">CREATE</xsl:with-param> |
39 |
<xsl:with-param name="mode">callback</xsl:with-param> |
40 |
<xsl:with-param name="cbname">abc</xsl:with-param> |
41 |
</xsl:call-template> |
42 |
</xsl:when> |
43 |
<xsl:otherwise> |
44 |
<xsl:call-template name="passthru_kay" /> |
45 |
</xsl:otherwise> |
46 |
</xsl:choose> |
47 |
</xsl:template> |
48 |
|
49 |
|
50 |
<!-- 2.b. The node manipulators: They translate the current context. --> |
51 |
|
52 |
<!-- by Mike Kay, Software AG [http://p2p.wrox.com/archive/xslt/2001-06/98.asp] --> |
53 |
<!-- enhancements & comments: Andreas Motl, rotamente.de --> |
54 |
<!-- <xsl:template match="source"> --> |
55 |
<xsl:template name="passthru_kay"> |
56 |
<xsl:copy> |
57 |
<xsl:copy-of select="@*" /> |
58 |
<xsl:apply-templates /> |
59 |
</xsl:copy> |
60 |
</xsl:template> |
61 |
|
62 |
<xsl:template name="inject_kay_motl"> |
63 |
<xsl:param name="payload" /> |
64 |
<xsl:value-of select="$payload" /> |
65 |
<xsl:copy> |
66 |
<xsl:copy-of select="@*" /> |
67 |
<xsl:apply-templates /> |
68 |
<!-- <xsl:apply-templates select="*[3]" /> --> |
69 |
<!-- <xsl:value-of select="text()" /> --> |
70 |
<!-- Multiple handlers here now: a) create fressshhhh element, b) clone s.th. --> |
71 |
<!-- TODO: Do switch/case or couple of if/then statements here to be able to handle that "at runtime". --> |
72 |
<xsl:call-template name="create_fresh_element" /> |
73 |
<xsl:call-template name="clone_last_element" /> |
74 |
<xsl:call-template name="clone_element_nr" /> |
75 |
</xsl:copy> |
76 |
</xsl:template> |
77 |
|
78 |
<xsl:template name="crud"> |
79 |
|
80 |
<xsl:param name="action" /> |
81 |
<xsl:param name="payload" /> |
82 |
<xsl:param name="mode" /> |
83 |
<xsl:param name="cbname" /> |
84 |
|
85 |
<!-- |
86 |
action: <xsl:value-of select="$action" /> |
87 |
payload: <xsl:value-of select="$payload" /> |
88 |
--> |
89 |
|
90 |
<!-- |
91 |
<xsl:if test="$action=5"> |
92 |
CRUD: CREATE - if |
93 |
</xsl:if> |
94 |
--> |
95 |
|
96 |
<xsl:copy> |
97 |
|
98 |
<xsl:copy-of select="@*" /> |
99 |
<xsl:apply-templates /> |
100 |
|
101 |
<!-- crud action dispatcher --> |
102 |
<xsl:choose> |
103 |
|
104 |
<!-- CREATE --> |
105 |
<xsl:when test="{$action}=CREATE"> |
106 |
|
107 |
<!-- thats a (compact) switch case in xsl lingo! --> |
108 |
<xsl:choose> |
109 |
|
110 |
<xsl:when test="$payload!="> |
111 |
<xsl:comment> Payload injected on {timestamp} </xsl:comment> |
112 |
<xsl:value-of select="$payload" /> |
113 |
</xsl:when> |
114 |
|
115 |
<xsl:when test="$mode=callback"> |
116 |
<xsl:comment> Payload injected via callback <xsl:value-of select="{$cbname}" /> on {$timestamp} </xsl:comment> |
117 |
<xsl:call-template name="xupdate_element_create_embedded" /> |
118 |
</xsl:when> |
119 |
|
120 |
<xsl:otherwise> |
121 |
<xsl:comment> Cloned last element as template on {timestamp} </xsl:comment> |
122 |
<xsl:call-template name="clone_last_element" /> |
123 |
</xsl:otherwise> |
124 |
|
125 |
</xsl:choose> |
126 |
|
127 |
</xsl:when> |
128 |
|
129 |
<xsl:otherwise> |
130 |
<xsl:comment> |
131 |
UNKOWN CRUD ACTION: "<xsl:value-of select="$action" />" on <xsl:value-of select="$now" />. |
132 |
</xsl:comment> |
133 |
</xsl:otherwise> |
134 |
|
135 |
</xsl:choose> |
136 |
|
137 |
</xsl:copy> |
138 |
|
139 |
</xsl:template> |
140 |
|
141 |
|
142 |
|
143 |
<!-- 2.c. The element manipulators: They mungle a specific element inside the current context. --> |
144 |
|
145 |
<!-- copy over node 1:1 (use last element as a template) --> |
146 |
<xsl:template name="clone_last_element"> |
147 |
<xsl:copy-of select="*[last()]" /> |
148 |
</xsl:template> |
149 |
|
150 |
<xsl:template name="clone_element_nr"> |
151 |
<!-- TODO: let idx be passed in via "with-param" or s.th.l.th. --> |
152 |
<xsl:copy-of select="*[2]" /> |
153 |
</xsl:template> |
154 |
|
155 |
<!-- creates instance of new element --> |
156 |
<xsl:template name="create_fresh_element"> |
157 |
<xsl:element name="new">content</xsl:element> |
158 |
</xsl:template> |
159 |
|
160 |
<xsl:template name="xupdate_element_create_embedded"> |
161 |
<xsl:variable name="huhu" select="*[0]/@key">haha</xsl:variable> |
162 |
<!-- <xsl:transform version="1.1" xmlns:xupdate="http://www.xmldb.org/xupdate"> --> |
163 |
<!-- <xupdate:modifications version="1.0" xmlns:xupdate="http://www.xmldb.org/xupdate"> --> |
164 |
<!-- <xsl:append select="/addresses" child="last()"> --> |
165 |
<xsl:element name="address"> |
166 |
<town>San Francisco</town> |
167 |
</xsl:element> |
168 |
<xsl:value-of select="*[0]/@key" /> |
169 |
<xsl:value-of select="$huhu" /> |
170 |
<!-- </xupdate:modifications> --> |
171 |
<!-- </xsl:append> --> |
172 |
<!-- </xsl:transform> --> |
173 |
</xsl:template> |
174 |
|
175 |
|
176 |
<!-- inject other stuff --> |
177 |
<xsl:template name="inject_template"> |
178 |
<xsl:comment> Hello World! </xsl:comment> |
179 |
|
180 |
</xsl:template> |
181 |
|
182 |
|
183 |
|
184 |
|
185 |
<!-- 0. The root node dispatcher. --> |
186 |
<!-- <xsl:template match="source"><xsl:call-template name="insertChildNode"/></xsl:template> --> |
187 |
|
188 |
<!-- 2. This is the manipulation logic: insertChildNode --> |
189 |
|
190 |
<!-- 2.a. The "API" method. - now deprecated --> |
191 |
<xsl:template name="insertChildNode"> |
192 |
|
193 |
<!-- manipulate node --> |
194 |
<xsl:comment> node - begin </xsl:comment> |
195 |
|
196 |
<!-- <xsl:copy-of select="source" /> --> |
197 |
<xsl:copy> |
198 |
|
199 |
<!-- Pass through children 1:1. --> |
200 |
<xsl:apply-templates /> |
201 |
|
202 |
<!-- Call one of the manipulators - this implements injection behavior. --> |
203 |
<xsl:call-template name="clone_last_element" /> |
204 |
<xsl:call-template name="inject_template" /> |
205 |
|
206 |
</xsl:copy> |
207 |
<xsl:comment> node - end </xsl:comment> |
208 |
|
209 |
</xsl:template> |
210 |
|
211 |
|
212 |
</xsl:stylesheet> |