xml transformations

webmasterbeta

New Member
Hi fellars, havin some problems with xslt .. here goes

Pseudo Code : i) when Activity[route] is encountered, route is created.

All activities with routetag will be slotted into approapriate routes corresponding to routetag=Activity[Route] 's Id.

Thus activities without routetag will be outputted at the top level while activities with routetag will be nested inside the route in the output file.

Initially, I used Activities's attribute (routeattr) to perform the above task. And it werks as I have shown below.
But then my sup wanted me to change it to Activitie's child(routetag) to perform the same task.
I initially assumed it was a simple task and just modified my original Xslt_One.xsl to Xslt_Two.xsl. But the output never was the same. Anyone wif any ideas to modifiy to Xslt_Two.xsl to give the same output as Xslt_One.xsl is welcomed.

Thanks all

this is the input.xml
<?xml version="1.0" encoding="UTF-8"?>

<Activities>

<Activity Id="1Id">
<catType>Alarm</catType>
</Activity>

<Activity Id="2Id">
<Route/>
</Activity>
<!--ORINGINALLY I WAS USING routeattr BUT NOW I AM USING routetag-->
<Activity Id="3Id" routeattr="2Id">
<routetag>2ID</routetag>
<catType>Email</catType>
</Activity>

<Activity Id="4Id" routeattr="2Id">
<routetag>2ID</routetag>
<catType>Gate</catType>
</Activity>

<Activity Id="5Id">
<catType>Gate</catType>
</Activity>

<Activity Id="6Id">
<Route/>
</Activity>

<Activity Id="7Id" routeattr="6Id">
<routetag>6Id</routetag>
<catType>Gate</catType>
</Activity>

<Activity Id="8Id" routeattr="6Id">
<routetag>6ID</routetag>
<catType>Email</catType>
</Activity>

<Activity Id="9Id">
<Route/>
</Activity>

<Activity Id="10Id" routeattr="9Id">
<routetag>9ID</routetag>
<catType>Gate</catType>
</Activity>

<Activity Id="11Id">
<catType>Alarm</catType>
</Activity>

</Activities>

This is Xslt_One.xsl ( This uses the routeattr)1
2
3 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4
5 <xsl:key name="storeRoute" match="Activity" use="@routeattr"/><!--FORMER CODE-->
6 <!--xsl:key name="storeRoute" match="Activity" use="routetag"/><NEW CODE v1--><!--Have tried both ways to store keys-->
7 <!--xsl:key name="storeRoute" match="routetag" use="."/><NEW CODE v2-->
8
9 <xsl:template match="Activities">
10 <all>
11 <xsl:apply-templates select="Activity"/>
12 </all>
13 </xsl:template>
14
15
16 <!-- template for route activity -->
17 <xsl:template match="Activity[Route]">
18 <route>
19 <xsl:apply-templates select="key('storeRoute',@Id)" mode="in-route"/>
20 <!--The prob is that even after putting " /.. " , it fails to call the template match mode="in-route"
21 which were formerly not a issue" -->
22 </route>
23 </xsl:template>
24
25 <!-- template for activities that are in a route -->
26 <xsl:template match="Activity[@routeattr]" mode="in-route"><!--FORMER CODE-->
27 <!--xsl:template match="Activity[routetag]" mode="in-route"><NEW CODE-->
28 <xsl:call-template name="Activity-content"/>
29 </xsl:template>
30
31 <!-- Empty: template : do not output at top level activities in route -->
32 <xsl:template match="Activity[@routeattr]"/><!--FORMER CODE-->
33 <!--xsl:template match="Activity[routetag]"/><NEW CODE-->
34
35 <!-- template for all other activities -->
36 <xsl:template match="Activity">
37 <xsl:call-template name="Activity-content"/>
38 </xsl:template>
39
40 <!-- named template handling the content of the activity -->
41 <!--This is specific code and not related to my question-->
42 <xsl:template name="Activity-content">
43 <!--LINE 41 TO LINE 59...JUST INCLUDED FOR THE SAKE OF THE COMPLETENESS-->
44 <xsl:choose>
45 <xsl:when test="catType='Alarm'">
46 <alarm>
47 <xsl:attribute name="id"><xsl:value-of select="'ding dong'"/></xsl:attribute>
48 </alarm>
49 </xsl:when>
50
51 <xsl:when test="catType='Email'">
52 <email>
53 <xsl:attribute name="id"><xsl:value-of select="'got Mail!'"/></xsl:attribute>
54 </email>
55 </xsl:when>
56
57 <xsl:when test="catType='Gate'">
58 <gate>
59 <xsl:attribute name="id"><xsl:value-of select="'close Gate'"/></xsl:attribute>
60 </gate>
61 </xsl:when>
62 </xsl:choose>
63 </xsl:template>
64
65 </xsl:stylesheet>

Output file generated.
1 <all>
2 <alarm id="ding dong"/>
3 <route>
4 <email id="got Mail!"/>
5 <gate id="close Gate"/>
6 </route>
7 <gate id="close Gate"/>
8 <route>
9 <gate id="close Gate"/>
10 <email id="got Mail!"/>
11 </route>
12 <route>
13 <gate id="close Gate"/>
14 </route>
15 <alarm id="ding dong"/>
16 </all>

Xslt_Two.xsl(this one uses the routetag)
1
2
3 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4
5 <!--xsl:key name="storeRoute" match="Activity" use="@routeattr"/><FORMER CODE-->
6 <!--xsl:key name="storeRoute" match="Activity" use="routetag"/><NEW CODE v1--><!--Have tried both ways to store keys-->
7 <xsl:key name="storeRoute" match="routetag" use="."/><!--NEW CODE v2-->
8
9 <xsl:template match="Activities">
10 <all>
11 <xsl:apply-templates select="Activity"/>
12 </all>
13 </xsl:template>
14
15
16 <!-- template for route activity -->
17 <xsl:template match="Activity[Route]">
18 <route>
19 <xsl:apply-templates select="key('storeRoute',@Id)/.." mode="in-route"/>
20 <!--The prob is that even after putting " /.. " , it fails to call the template match mode="in-route"
21 which were formerly not a issue" -->
22 </route>
23 </xsl:template>
24
25 <!-- template for activities that are in a route -->
26 <!--xsl:template match="Activity[@routeattr]" mode="in-route"><FORMER CODE-->
27 <xsl:template match="Activity[routetag]" mode="in-route"><!--NEW CODE-->
28 <xsl:call-template name="Activity-content"/>
29 </xsl:template>
30
31 <!-- Empty: template : do not output at top level activities in route -->
32 <!--xsl:template match="Activity[@routeattr]"/><FORMER CODE-->
33 <xsl:template match="Activity[routetag]"/><!--NEW CODE-->
34
35 <!-- template for all other activities -->
36 <xsl:template match="Activity">
37 <xsl:call-template name="Activity-content"/>
38 </xsl:template>
39
40 <!-- named template handling the content of the activity -->
41 <!--This is specific code and not related to my question-->
42 <xsl:template name="Activity-content">
43 <!--LINE 41 TO LINE 59...JUST INCLUDED FOR THE SAKE OF THE COMPLETENESS-->
44 <xsl:choose>
45 <xsl:when test="catType='Alarm'">
46 <alarm>
47 <xsl:attribute name="id"><xsl:value-of select="'ding dong'"/></xsl:attribute>
48 </alarm>
49 </xsl:when>
50
51 <xsl:when test="catType='Email'">
52 <email>
53 <xsl:attribute name="id"><xsl:value-of select="'got Mail!'"/></xsl:attribute>
54 </email>
55 </xsl:when>
56
57 <xsl:when test="catType='Gate'">
58 <gate>
59 <xsl:attribute name="id"><xsl:value-of select="'close Gate'"/></xsl:attribute>
60 </gate>
61 </xsl:when>
62 </xsl:choose>
63 </xsl:template>
64
65 </xsl:stylesheet>

But UnFortunetly,U can see the output generated is different.
1
2 <all>
3 <alarm id="ding dong"/>
4 <route/>
5 <gate id="close Gate"/>
6 <route>
7 <gate id="close Gate"/>
8 </route>
9 <route/>
10 <alarm id="ding dong"/>
11 </all>
 
Back
Top