home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / faq-xslt.xml < prev    next >
Encoding:
Extensible Markup Language  |  2004-07-12  |  8.3 KB  |  220 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3.   Copyright 1999-2004 The Apache Software Foundation
  4.  
  5.   Licensed under the Apache License, Version 2.0 (the "License");
  6.   you may not use this file except in compliance with the License.
  7.   You may obtain a copy of the License at
  8.  
  9.       http://www.apache.org/licenses/LICENSE-2.0
  10.  
  11.   Unless required by applicable law or agreed to in writing, software
  12.   distributed under the License is distributed on an "AS IS" BASIS,
  13.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.   See the License for the specific language governing permissions and
  15.   limitations under the License.
  16. -->
  17. <!DOCTYPE faqs PUBLIC "-//APACHE//DTD FAQ V1.0//EN" "faq-v10.dtd">
  18.  
  19. <faqs title="XSLT FAQs">
  20.  
  21. <faq>
  22.   <question>
  23.    How do I tell Cocoon to stop adding carriage-returns during XSL
  24.    transformation?
  25.   </question>
  26.   <answer>
  27.    <p>The short answer is that this is not a Cocoon issue. You need to
  28.     read up on XSLT usage. Please see 
  29.     <link href="http://cocoon.apache.org/community/mail-lists.html">other resources for XSLT,</link>
  30.     specifically, the XSL FAQ and discussion lists.
  31.    </p>
  32.    <p>The long answer is that you need to use the XSLT function
  33.     <code>normalize-space()</code> whenever you want to rely on the content
  34.     of an xml element.
  35.    </p>
  36.    <p>For example, if your application is producing some Javascript code in
  37.     your HTML output, then you might mistakenly try to use the following
  38.     construct in your XSL stylesheet:
  39.    </p>
  40.  
  41. <source><![CDATA[
  42. alert('<xsl:value-of select="message"/>');
  43. ]]></source>
  44.  
  45.    <p>which will produce:</p>
  46.  
  47. <source><![CDATA[
  48. alert('
  49. messageValue
  50. ');
  51. ]]></source>
  52.  
  53.   <p>The line-endings in the content of your "<code>message</code>" element will cause javascript errors. Instead, do this:
  54.   </p>
  55.  
  56. <source><![CDATA[
  57. alert('<xsl:value-of select="normalize-space(message)"/>');
  58. ]]></source>
  59.  
  60.   <p>Note that there are many more issues about whitespace handling. Please
  61.    refer to the relevant XSLT resources rather than clutter up the Cocoon
  62.    discussion lists.
  63.   </p>
  64.   </answer>
  65. </faq>
  66.  
  67. <faq>
  68.   <question>
  69.    Is (your description here) kind of functionality appropriate for a stylesheet?
  70.   </question>
  71.   <answer>
  72.    <p>When this kind of question arises, ask yourself:</p>
  73.    <ul>
  74.      <li>Am I using my stylesheet to address style concerns, or am I programming with it?</li>
  75.      <li>Could I solve this problem once and for all during Generation?</li>
  76.      <li>Isn't my problem better solved with a Transformer, since it requires coding?</li>
  77.     </ul>
  78.   <p>
  79. There is not one, nor only one, answer. In Cocoon, you can accomplish the same thing in a number of different ways. Nonetheless, if your transformation depends on something specific happening during generation, it will be more difficult to reuse your code.
  80. </p>
  81.   <p>
  82. Here's a hint. Do all that you possibly can in a Generator. Add only what is absolutely
  83. necessary with Transformers. Use stylesheets to change format or style,
  84. not to code. This approach will make your system more manageable and reusable because it removes dependencies between components. 
  85.   </p>
  86.   </answer>
  87. </faq>
  88.  
  89. <faq>
  90.   <question>
  91.    Can I use the same XSLT processor and still specify different processing options for different
  92. pipelines?
  93.   </question>
  94.   <answer>
  95.    <p>For example, I found that PDF processing is faster with Xalan when I turn incremental processing off.</p>
  96.   <p>
  97. Yes. For Cocoon version 2.0.3, check out the following <link href="../snippet/snippet-xslt-options.html" >Cocoon snippet.</link></p>
  98.   </answer>
  99. </faq>
  100.  
  101. <faq>
  102.   <question>
  103.    Can I use different XSLT processors when processing different pipelines?
  104.   </question>
  105.   <answer>
  106.   <p>
  107. Yes. For Cocoon version 2.0.3, adapt the approach discussed in the following <link href="../snippet/snippet-xslt-options.html" >Cocoon snippet.</link></p>
  108.   </answer>
  109. </faq>
  110.  
  111. <faq>
  112.  <question>
  113. How can I remove namespaces from my xml files?
  114.  </question>
  115.  
  116.  <answer>
  117.   <p>
  118. Sometimes adding xsl:exclude-result-prefixes attributes
  119. to the <xsl:stylesheet> or literal result element is not effective
  120. in removing all namespace declarations. For example, namespace nodes copied 
  121. from the source document within <xsl:copy> or <xsl:copy-of> instructions 
  122. (commonly found in catch-all stylesheet templates) will not be excluded.
  123.   </p> 
  124.   <p>
  125. There are two approaches to this problem. 
  126.   </p> 
  127.    <p>
  128. One approach is to add a transformation step in your pipeline (or adjust your final stylesheet) with the following:
  129.   </p>
  130. <source><![CDATA[
  131. <?xml version="1.0" encoding="UTF-8"?>
  132. <xsl:stylesheet version="1.0"
  133. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  134.   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  135.     <xsl:template match="*">
  136.       <!-- remove element prefix (if any) -->
  137.       <xsl:element name="{local-name()}">
  138.         <!-- process attributes -->
  139.         <xsl:for-each select="@*">
  140.           <!-- remove attribute prefix (if any) -->
  141.           <xsl:attribute name="{local-name()}">
  142.             <xsl:value-of select="."/>
  143.           </xsl:attribute>
  144.         </xsl:for-each>
  145.         <xsl:apply-templates/>
  146.       </xsl:element>
  147.   </xsl:template>
  148. </xsl:stylesheet>
  149. ]]></source>
  150.  <p>
  151. Another approach is to extend your serializer component, described <link href="faq-serializers.html#faq-4">here</link>.
  152.   </p>
  153. </answer>
  154. </faq>
  155.  
  156. <faq>
  157.  <question>
  158. What's "wrong" with use of the document() function in Cocoon?
  159.  </question>
  160.  
  161.  <answer>
  162.   <p>
  163. Using the document() function for aggregation in Cocoon may break
  164. Separation of Concerns (SoC). That is, the designers of Cocoon
  165. view inclusion and transformation as different functions, best
  166. handled by separate Cocoon components. Treating them
  167. separately allows you to achieve performance gains and increases
  168. the resusability of your pipelines.
  169.   </p>
  170.   <p>
  171. Alternatives to the document() in the Cocoon environment include
  172. aggregation or the use of a multi-stage transformation using the
  173. XInclude Transformer. This involves transforming a list of documents
  174. (generated dynamically or statically) by adding xinclude elements which
  175. reference (via xpointer) specific document content, and then transforming
  176. again via the XInclude Transformer, to obtain the desired result. For an example of this, see this <link href="http://marc.theaimsgroup.com/?l=xml-cocoon-users&m=102617106411067&w=2">email.</link>
  177.   </p>
  178.   <p>
  179. You'll achieve better performance if you aggregate content prior to transformation.
  180. This allows you to take full advantage of Cocoon's pipeline caching. In contrast,
  181. making dynamic document() calls inside an XSLT within a cached pipeline is problematic.
  182. At this time, Cocoon does not recognize changes in documents (called by the document() function)
  183. until the requested page expires from cache.
  184.   </p>
  185.   <p>
  186. Understand that the document() function was designed *before* xinclude
  187. with xpointer facilities existed. Had such capabilities been available,
  188. perhaps the document() function, which essentially mimics xinclude and xpointer,
  189. would have never been added to XSLT.
  190.   </p>
  191.   <p>
  192. Please note that if you must work with your XML files outside of the
  193. Cocoon environment, you may need to use the document() function
  194. in order to utilize the limited capabilities of other pipeline engines.
  195. This includes engines which are not xinclude-capable or which
  196. lack a predefined way to indicate document processing steps. If you
  197. are working with legacy code from non-pipelined engines, you may need to use
  198. the document() function as well, at least initially.
  199.   </p>
  200.   <p>
  201. If you do use the document() function in Cocoon, you can still observe SoC by
  202. having separate XSLT stylesheets perform inclusion and transformation functions.
  203. For example, you can put multiple XSLT transforms in a pipeline and have the
  204. first one perform inclusion and the second one perform transformation. However,
  205. be mindful of some unresolved caching issues in Cocoon related to the document() function.
  206. At this time, Cocoon is unable to check validity of content included via the document()
  207. function. In addition, the document() function implemented by Xalan is inefficient. See:
  208.   http://nagoya.apache.org/bugzilla/show_bug.cgi?id=4257
  209. Until this bug is fixed, consider using Saxon instead for document() function-related parsing
  210. needs.
  211.   </p>
  212.   <p>
  213. For other aggregation/inclusion approaches, please stay tuned for XpathDirectoryGenerator (2.1 scratchpad),
  214. as well as Forrest's Libre (currently alpha in the Forrest cvs).
  215.   </p>
  216.   </answer>
  217.   </faq>
  218.  
  219. </faqs>
  220.