469,623 Members | 1,850 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

xslt problems

5
Hi,

I'm very new to xslt and this is my first time posting to a Forum so please forgive me if I transgress any protocols.

I have to do a tally report. This report is divided up into sections. Each section has a list of questions. Each question has responses.

I need to display a list of responses to the questions (i.e. set:distinct), once and only once, each section.

My second problem is that these questions can also have corrective actions associated with them (CAR's in the report). The first section has 5 CAR's but when I do a sum I get the result 2-1-2 instead of 5.


This is a cut down version of the xml I'm working with (I have to use XSLT 1.0 transforming using FOP):


Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <report>
  3.   <audit>
  4.     <auditid>159</auditid> 
  5.     <auditlevel_id>21</auditlevel_id> 
  6.     <levelstring>AUDIT LIBRARY</levelstring> 
  7.     <auditname>Cory</auditname> 
  8.     <cust_id>2</cust_id> 
  9.     <ld>1</ld> 
  10.     <li>8</li> 
  11.     <questions>
  12.       <question>
  13.         <id>13759</id> 
  14.         <sec_id>1</sec_id> 
  15.         <sub_sec_id>1</sub_sec_id> 
  16.         <item_id>1</item_id> 
  17.         <questiontype>SelectResponse</questiontype> 
  18.         <resp_num>3</resp_num> 
  19.         <itemdescription>Standard Action Test</itemdescription> 
  20.         <possible_score>10.00</possible_score> 
  21.         <score_criteria /> 
  22.         <s_rs>
  23.           <s_r>
  24.             <seq>3</seq> 
  25.             <narr>Standard Action Test Narrative</narr> 
  26.             <followup>efdqde</followup> 
  27.           </s_r>
  28.         </s_rs>
  29.         <udf_info /> 
  30.         <audit_response>
  31.           <id>11602</id> 
  32.           <question_id>13759</question_id> 
  33.           <resp>N/a</resp> 
  34.           <narr>Standard Action Test Narrative</narr> 
  35.           <score>0.0</score> 
  36.           <p_o>0</p_o> 
  37.           <n_o>0</n_o> 
  38.           <p_f>0</p_f> 
  39.           <n_f>0</n_f> 
  40.           <question_type>SelectResponse</question_type> 
  41.           <answered>1</answered> 
  42.           <recordChanged>Y</recordChanged> 
  43.           <corr_acts>
  44.             <corr_act>
  45.               <id>1</id> 
  46.             </corr_act>
  47.             <corr_act>
  48.               <id>3</id> 
  49.             </corr_act>
  50.           </corr_acts>
  51.         </audit_response>
  52. ...
This is my stylesheet:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <xsl:stylesheet version="2.0"
  3.  
  4. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  5. xmlns:date="http://exslt.org/dates-and-times"
  6. xmlns:fo="http://www.w3.org/1999/XSL/Format"
  7. xmlns:xalan="http://xml.apache.org/xalan" 
  8. xmlns:java="http://xml.apache.org/xslt/java"
  9. xmlns:exsl="http://exslt.org/common" 
  10. xmlns:set="http://exslt.org/sets"
  11. xmlns:dyn="http://exslt.org/dynamic"
  12.  
  13. extension-element-prefixes="exsl"
  14. exclude-result-prefixes="set"> 
  15. <xsl:output />
  16.  
  17. <xsl:key name="response" match="audit_response" use="resp"/>
  18. <xsl:key name="responseNum" match="report/audit/questions/question" use="resp_num"/>
  19. <xsl:key name="car" match="report/audit/questions/question" use="audit_response/corr_acts/corr_act"/>
  20. <xsl:key name="response2" match="report/audit/questions/question" use="audit_response/resp" />
  21. <xsl:key name="corracts" match="report/audit/questions/question" use="audit_response/corr_acts/corr_act/id" />
  22. <xsl:key name="keys_sec_id" match="report/audit/questions/question" use="sec_id" />
  23.  
  24. <xsl:template match="/">
  25. <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  26. <fo:layout-master-set>
  27. <fo:simple-page-master master-name="rptPages" page-height="10in"
  28. page-width="8in" margin-top="1cm" margin-bottom=".5cm" margin-left="1cm" margin-right="1cm" >
  29. <fo:region-body margin-bottom="1.5cm" margin-top="2cm" keep-together.within-page="always" >
  30. <fo:region-body> 
  31. <fo:region-after region-name="rptFooter" extent="1cm" />
  32. <fo:region-before region-name="rptHeader" extent="2cm" /> 
  33. </fo:simple-page-master> 
  34. </fo:layout-master-set>
  35. <fo:page-sequence master-reference="rptPages" force-page-count="no-force">
  36.      <fo:static-content flow-name="rptHeader" > 
  37.  
  38. <!-- beginning of region body and beginning of Page Header information-->
  39.  
  40. <fo:flow flow-name="xsl-region-body" >
  41.  
  42. <!-- beginning of test section -->
  43.  
  44. <fo:table table-layout="fixed" inline-progression-dimension="18cm" cell-spacing="0" cell-padding="0">
  45. <fo:table-column column-width="8cm" />
  46. <fo:table-column column-width="1cm" />
  47. <fo:table-column column-width="2cm" />
  48. <fo:table-column column-width="1.5cm" />
  49. <fo:table-column column-width=".1cm" />
  50. <fo:table-column column-width="1.5cm" />
  51.  
  52. <fo:table-body> 
  53. <fo:table-row>
  54. <fo:table-cell >
  55. <fo:block font="Arial" text-align="left" font-size="10pt" font-weight="bold" space-after="6px" > 
  56. Sections
  57. </fo:block>
  58. </fo:table-cell>
  59.  
  60. <fo:table-cell> 
  61. <fo:block font="Arial" text-align="center" font-size="10pt" font-weight="bold" space-after="6px" > 
  62. List #
  63. </fo:block>
  64. </fo:table-cell>
  65.  
  66. <fo:table-cell > 
  67. <fo:block font="Arial" text-align="center" font-size="10pt" font-weight="bold" space-after="6px" > 
  68. Response
  69. </fo:block>
  70. </fo:table-cell>
  71.  
  72. <fo:table-cell > 
  73. <fo:block font="Arial" text-align="center" font-size="10pt" font-weight="bold" space-after="6px" > 
  74. Tally
  75. </fo:block>
  76. </fo:table-cell>
  77. <fo:table-cell />
  78.  
  79. <fo:table-cell > 
  80. <fo:block font="Arial" text-align="center" font-size="10pt" font-weight="bold" space-after="6px" > 
  81. CAR 
  82. </fo:block>
  83. </fo:table-cell>
  84.  
  85. </fo:table-row>
  86. </fo:table-body>
  87. </fo:table>
  88. <fo:table table-layout="fixed" inline-progression-dimension="18cm" cell-spacing="0" cell-padding="0">
  89. <fo:table-column column-width="8cm" />
  90. <fo:table-column column-width="1cm" />
  91. <fo:table-column column-width="2cm" />
  92. <fo:table-column column-width="1.5cm" />
  93. <fo:table-column column-width=".1cm" />
  94. <fo:table-column column-width="1.5cm" /> 
  95. <fo:table-body> 
  96.  
  97. <xsl:for-each select="report/audit/questions/question">
  98. <xsl:sort data-type="number" select="sec_id"/>
  99. <xsl:sort data-type="number" select="sec_id" />
  100. <xsl:sort data-type="number" select="sub_sec_id" />
  101. <xsl:sort data-type="number" select="item_id" />
  102.  
  103. <fo:table-row>
  104.  
  105. <xsl:choose>
  106. <xsl:when test="sec_id">
  107. <xsl:variable name="test" select="sec_id"></xsl:variable>
  108. <xsl:if test="(sec_id &gt;=0) and sub_sec_id=0 and item_id=0 ">
  109. <fo:table-cell > 
  110. <fo:block font="Arial" text-align="left" font-size="8pt" font-weight="normal" space-after="2px"> 
  111. <xsl:value-of select ="sec_id" />-<xsl:value-of select ="sub_sec_id" />-
  112. <xsl:value-of select ="item_id" />
  113. <xsl:text disable-output-escaping="yes">&amp;#160;&amp;#160;&amp;#160;</xsl:text>
  114. <xsl:value-of select="itemdescription" />
  115. </fo:block>
  116. </fo:table-cell>
  117. </xsl:if> 
  118. <fo:table-cell />
  119.  
  120.  
  121. <fo:table-cell >
  122. <!-- brings back a unique response number -->
  123. <fo:block font="Arial" text-align="center" font-size="8pt" font-weight="normal" space-after="2px">
  124. <xsl:if test="not(sec_id &gt;=0 and sub_sec_id &gt;=0 and item_id=0)" >
  125. <xsl:if test="sec_id=$test">
  126. <xsl:for-each select="/report/audit/questions/question[generate-id() = generate-id(key('response2',audit_response/resp)[1])]" />
  127. <fo:block></fo:block>
  128. <xsl:if test="not(audit_response/resp='')">
  129. <xsl:value-of select ="resp_num" />
  130. </xsl:if>
  131. </xsl:if>
  132. </xsl:if>
  133. </fo:block>
  134. </fo:table-cell>
  135. <fo:table-cell >
  136. <fo:block font="Arial" text-align="center" font-size="8pt" font-weight="normal" space-after="2px">
  137. <xsl:choose> 
  138. <xsl:when test="sec_id">
  139. <xsl:if test="sec_id=$test">
  140. <xsl:if test="id=audit_response/question_id">
  141.  
  142.  
  143. <xsl:if test="not(audit_response/resp='')">
  144. <xsl:for-each select="/report/audit/questions/question[generate-id() = generate-id(key('response2',audit_response/resp)[1])]" />
  145. <xsl:variable name="groups"><xsl:value-of select="audit_response/resp" /></xsl:variable>
  146. <xsl:value-of select="exsl:node-set(set:distinct($groups))"/>
  147. <fo:block></fo:block>
  148. </xsl:if>
  149. </xsl:if>
  150. </xsl:if>
  151. </xsl:when>
  152. </xsl:choose>
  153. </fo:block>
  154. </fo:table-cell>
  155. <!-- brings back a tally of the responses -->
  156. <fo:table-cell>
  157. <fo:block font="Arial" text-align="center" font-size="8pt" font-weight="normal" space-after="2px">
  158. <xsl:if test="not(sec_id &gt;=0 and sub_sec_id &gt;=0 and item_id=0)" >
  159. <xsl:if test="sec_id=$test">
  160. <xsl:if test="not(audit_response/resp='')" >
  161. <xsl:for-each select="/report/audit/questions/question[generate-id() = generate-id(key('response2',audit_response/resp)[1])]" />
  162. <xsl:variable name="grouping"><xsl:value-of select="audit_response/resp" /></xsl:variable>
  163. <fo:block></fo:block> 
  164. <xsl:value-of select="count($grouping)"/>
  165. </xsl:if>
  166. </xsl:if>
  167. </xsl:if>
  168. </fo:block>
  169. </fo:table-cell>
  170. <!-- brings back a count of the cars per answered question --> 
  171. <xsl:choose>
  172. <xsl:when test="sec_id">
  173. <xsl:variable name="test" select="sec_id"></xsl:variable>
  174. <xsl:if test="(sec_id &gt;=0) and sub_sec_id=0 and item_id=0 ">
  175. <fo:table-cell > 
  176. <fo:block font="Arial" text-align="center" font-size="8pt" font-weight="bold" space-after="2px">
  177. <xsl:for-each select="/report/audit/questions/question">
  178. <xsl:if test="sec_id=$test">
  179. <xsl:variable name="trial"><number><xsl:value-of select="count(audit_response/corr_acts/corr_act[id])"/></number></xsl:variable>
  180. <xsl:if test="$trial &gt;0">
  181. <fo:block></fo:block>
  182. <xsl:value-of select ="sum(exsl:node-set($trial)/number)"/>
  183. </xsl:if>
  184. </xsl:if>
  185. </xsl:for-each>
  186. </fo:block>
  187. </fo:table-cell>
  188. </xsl:if>
  189. </xsl:when>
  190. </xsl:choose>
  191. </xsl:when>
  192. </xsl:choose><!--ends the outside choose -->
  193. </fo:table-row>
  194. </xsl:for-each>
  195. </fo:table-body>
  196. </fo:table<fo:block id="last-page"/>
  197. </fo:flow>
  198. </fo:page-sequence>
  199. </fo:root>
  200. </xsl:template>
  201. </xsl:stylesheet>
And this is how the report looks:

Sections List# Response Tally CAR
1-0-0 words 3 N/a 1 2
1 Yes 1 1
1 No 1 2
1 No 1
2-0-0 1 Yes 1 1
1 No 1
1 Yes 1

This report should print No only once for section 1-0-0 with a count of two. It should print Yes only once for section 2-0-0 with a count of 2. It should print a total of 5 for the CAR's in section 1-0-0 and only 1 (as it does) for section 2-0-0.


Sorry this is such a long post.
Thanks
Debbie
Jun 12 '06 #1
0 3094

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Sergio del Amo | last post: by
1 post views Thread by Doug Farrell | last post: by
5 posts views Thread by K. N. | last post: by
9 posts views Thread by Jeff Rubard | last post: by
4 posts views Thread by Ringo Langly | last post: by
4 posts views Thread by Asbjørn Ulsberg | last post: by
10 posts views Thread by daz_oldham | last post: by
10 posts views Thread by olivier.scalbert | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.