XSL Transformations


Presentation

Useful Links

Questions

  1. When converting an XML document to another XML document , what happens to the DTDs present in the original input file. It is given that the transformer may not include DTDs in output file. In that case how are the default values restored?
  2. Can some types of HTML documents be converted to XML ones using XSLT?
  3. How can we import XSLT documents from another XSLT document?
  4. XSLT operates by transforming one XML tree into another XML tree, i.e. the input and the output of an XSLT processor are both XML tree. What is the difference between these two trees?
  5. Is the default namespaces taken into account when processing XPATHs in an XSL stylesheet? What will be generated as the following is given: 
            <xsl:template xmlns= "http://foo.com" match='bo[@xpath]'>
                <foo/>
            </xsl:template>
  6. Is templates are pre-designed? Can the user specify the templates dynamically based on his need. Is so how can the user specify his template design?
  7. When specified attribute of xsl:apply-templates, does it recursively process all qualified elements?
  8. As we know, Cascading Style Sheet (CSS) is also used to generate the html file. What are the different functions between CSS and XSL?
  9. Suppose we have many elements of "book" each of which has a child element "price" in an XML document. Can you display the lowest price of the book using XSLT? If you can, show how to do it. If not, explain why?
  10. If an XML document is transformed into an HTML document in two different ways, using XSLT and XSLFO, which one is more effective?
  11. Show by example how to pass parameters to an XSL template?
  12. When XSLT is processing XPATHs in an XSL stylesheet, does it take into account the default namespaces?
  13. How do one can connect an XML source document to an XSLT stylesheet?
  14. As we know, the task of XSLT is to transform an XML document in one format to an XML document in another format. Can it add/insert elements to or modify the value of elements in the source document to produce a translated document?
  15. Can XSLT select elements from one or more XML documents and combine them together to get the translated document?


If you have any questions or comments regarding the answers to these questions please feel free to contact me.

Answers to Questions

  1. When converting an XML document to another XML document , what happens to the DTDs present in the original input file. It is given that the transformer may not include DTDs in output file. In that case how are the default values restored?
  2. XSLT doesn't do anything with the DTD's present in the input file. XSLT works with source XML documents and not with DTD's.

    When you do a transformation, you have the idea of what's the structure of the XML you want to generate. So, there is no need to include the DTD in the ouput file.

    DTD processing is done by the XML parser. When an XML parser is validating a XML document, it adds all default attribute values to elements in the source document. The "default rule for attributes" in XSLT copies all the attribute values to the output. Default Rule:
                                         <xsl:template match="@*">
                                                <xsl:value-of select="."/>
                                          </xsl:template>

    Because there's no default rule that ever applies templates to attributes, this rule won't be activated for attributes unless you specifically add a nondefault rule somewhere in the style sheet that does apply templates to attributes of one or more elements.

  3. Can some types of HTML documents be converted to XML ones using XSLT?
  4. HTML documents doesn't have a DTD to conform to it. But XML lets you work without a DTD and such DTDless documents must be "well-formed". Then such HTML documents can be taken as an input tree, and it can be converted to XML documents in whatever way we want using XSLT.

  5. How can we import XSLT documents from another XSLT document?
  6. You can use different style sheets for a single XML document. By using the <xsl:import> elements we can import the XSLT stylesheets into another XSLT stylesheet. For example:
           <xsl:stylesheet version="1.0" 
                     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
               <xsl:import href="genealogy.xsl"/>
               <xsl:import href="standards.xsl"/>
               <!-- other child elements follow -->
         </xsl:stylesheet>

    Here it is importing the stylesheets genealogy.xsl and standards.xsl

    Rules in the imported style sheets may conflict with rules in the importing style sheet. If so, rules in the importing style sheet take precedence. If two rules in different imported style sheets conflict, then the rule in the last style sheet imported (standards.xsl above) takes precedence.

  7. XSLT operates by transforming one XML tree into another XML tree, i.e. the input and the output of an XSLT processor are both XML tree. What is the difference between these two trees?
  8. The input tree is our original XML document which is fed to the XSLT processor with a stylesheet. The result tree is the transformed XML document which is obtained by applying the rules defined in the stylesheet.

  9. Is the default namespace taken into account when processing XPATHs in an XSL stylesheet? What will be generated as the following is given: 
            <xsl:template xmlns= "http://foo.com" match='bo[@xpath]'>
                <foo/>
            </xsl:template>
  10. No, the default namespace is not taken into account while processing XPaths in an XSL stylesheet. XPath syntax never uses the default namespace, not only when in XSLT.

    In the above example, the template rule will only match "bo" elements that are not in a namespace. If you want to match (or select) "bo" elements in a namespace, you must use a namespace prefix. If a match of "bo" element is found, then it outputs the empty element "<foo/>".

  11. Is templates are pre-designed? Can the user specify the templates dynamically based on his need. Is so how can the user specify his template design?
  12. Yes, there are three default template rules.
     
    1. The first default rule applies to element nodes and the root node:
            <xsl:template match="*|/">
            <xsl:apply-templates/>
        </xsl:template>

    The purpose of this rule is to ensure that all elements are recursively processed even if they aren't reached by following the explicit rules.

    2. The second default rule applies to text nodes and attributes:
            <xsl:template match="text()|@*">
           <xsl:value-of select="."/>
        </xsl:template>

    This rule matches all text and attribute nodes and outputs the value of the node.

    3. The default rule for processing instructions and comments:
                        <xsl:template match="processing-instruction()|comment()"/> 
    It simply says to do nothing; that is, drop the processing instructions and comments from the output as if they didn't exist.

    I don't think we can define the templates dynamically, because XSLT is a declarative language. There is nothing we can do at runtime.


  13. When specified attribute of xsl:apply-templates, does it recursively process all qualified elements?
  14. Yes, it processes all the elements recursively until no new source nodes are selected for processing. 

  15. As we know, Cascading Style Sheet (CSS) is also used to generate the html file. What are the different functions between CSS and XSL?
  16. CSS allows you to define the colours, backgrounds, and font-types for an HTML web page. But XSL can do lot more than CSS. One of its parts, XSLT is used to transform the input XML document into a result XML document using the rules defined in the stylesheet. The other part XSLFO is used to present the document in different ways on different devices.

  17. Suppose we have many elements of "Book" each of which has a child element "Price" in an XML document. Can you display the lowest price of the book using XSLT? If you can, show how to do it. If not, explain why?
  18. First we sort the Price elements in ascending order using the <xsl:sort> element. Then we find the value of the first Price element using the "position()" function which gives the lowest price.

               <xsl:template match="Book">    
                   <xsl:apply-templates>
                       <xsl:sort data-type="number" match="Price"/>
                   </xsl:apply-templates>
               </xsl:template>
               <xsl:template match="Price[position()=1]">
                    <xsl:value-of select="."/>
               </xsl:template>

    May be you can find a better solution than this.

  19. If an XML document is transformed into an HTML document in two different ways, using XSLT and XSLFO, which one is more effective?
  20. They are the two different parts of XSL. XSLT is used for XML - XML and XML - HTML transformations. This transformation is achieved by giving both the XSLT stylesheet and input XML document to the XSLT processor. The XSLT processor transforms the input XML document by using the rules defined in the stylesheet. Where as XSLFO is used to present the XML document in different ways on different devices, by using the formatting semantics.

  21. Show by example how to pass parameters to an XSL template?
  22. Parameters are passed to the templates using the <xsl:with-param> element.

    Parameter declaration is done by: <xsl:param name="parmeter-name">parameter-contents</xsl:param>

    EX:
    <xsl:template name="ATOM_CELL">
           <xsl:param name="file">index.html</xsl:param>
           <td>
              <font face="Times, serif" color="blue" size="2">
                <b>
                  <a href="{$file}"><xsl:value-of select="."/></a>
                </b>
              </font>
          </td>
      </xsl:template>


    Now Pass the parameter "file" using <xsl:with-param>

       <xsl:template match="ATOMIC_NUMBER">
          <xsl:call-template name="ATOM_CELL">
              <xsl:with-param name="file">atomic_number.html</xsl:with-param>
          </xsl:call-template>
     </xsl:template>


  23. When XSLT is processing XPATHs in an XSL stylesheet, does it take into account the default namespaces?
  24. Refer anwer 5.

  25. How do one can connect an XML source document to an XSLT stylesheet?
  26. An XML style sheet declaration is used to connect an XML document to its style sheet.

    The style sheet declaration is placed after the XML version declaration and before the root element. Here's what one looks like:

    <?xml-stylesheet type="text/xsl" href="urstylesheet.xsl"?>

  27. As we know, the task of XSLT is to transform an XML document in one format to an XML document in another format. Can it add/insert elements to or modify the value of elements in the source document to produce a translated document?
  28. Yes, we can insert the elements into the output using <xsl:element> element and we can insert attributes into the output using <xsl:attribute> element. 

    XSLT can add completely new elements into the output file, or remove elements. It can rearrange and sort the elements, and test and make decisions about which elements to display, and a lot more. We can also give a new value to a particular element in the output.

  29. Can XSLT select elements from one or more XML documents and combine them together to get the translated document?
  30. Yes, we can select elements from two different XML documents and combine them together using XSLT. This is done first by selecting the document, using the function "document("example.xml")" and then selecting the particular elements in each XML document.


If you have any questions or comments regarding the answers to these questions please feel free to contact me.