Showing posts with label xslt. Show all posts
Showing posts with label xslt. Show all posts

Dec 23, 2008

XSLT note - xsl:output method

There are 3 method option in xsl:output element, xml, html, text. only xml option will output xml declaration. only text will disables output escaping. In your style sheet, you want to export < , text will output "<", but xml and html will output <

Dec 22, 2008

XSLT note 6 - fast search node

We can make search much faster if we generate index. We generate index by using xsl:key.




  
  
  









   
      
        
        
         

XSLT note 5 - Cross tab transformation





  
    1
    Meal
    Breakfast
    
  
  
    1
    Meal
    Dinner
    lots of stuff
  
  
    1
    Tour
    Great wall
    
  
  
    2
    Meal
    Breakfast
    
  
  
    2
    Airport
    Transfer to hotel
    
  
  
    3
    Airport
    Transfer to airport
    
  
  
    3
    Meeting
    Face to face meeting
    
  





  

  
    
    
    
      

Cross tab demo

Type
,

Cross tab demo

Type 1 2 3
Meal Breakfast , Dinner Breakfast
Tour Great wall
Airport Transfer to hotel Transfer to airport
Meeting Face to face meeting

XSLT note 3 - attribute tricks

When you want to output the value of node to destination, you can use . This works only when the destination is not in side of an attribute. If the output is in side of an attribute, you need to use {}. For example.


ascending

  
    
  



   

If you want to output a attribute of source element, you need to use @attributeName, or attribute::attributeName.

To add an attribute to an output node, you can also use xsl:attribute, like the following.

  
    
      http://google.com
      Google
    
  

Some time we want to convert element to attribute. For example, we want to convert


  Fred
  18



We can use this template


  
    
      
        
      
    
  


or 


  
    
      
        
      
    
  


Dec 19, 2008

XSLT note 3 - variable

You can basically build a variable just as building any output. The simple way is


But you don't have to use select. Yiou can construct your variable in the constructor. Following is some sample


  
 
  
   
  
 



 

This is demo

How to use variable

If a variable contains a set of nodes. you can use the variable in other XPath expressions without any limitations. For example,






If you are using constructor to build a constructor, the variable can hold any arbitrary content, this content is called result tree fragment. You can imagine a result tree fragment (RTF) as a fragment or a chunk of XML code. You can assign a result tree fragment to a variable directly, or result tree fragment can arise from applying templates or other XSLT instructions. The following code assigns a simple fragment of XML to the variable $author.


  Jirka
  Kosek
  jirka@kosek.cz

Now let's say we want to extract the e-mail address from the $author variable. The most obvious way is to use an expression such as $author/email. But this will fail, as you can't apply XPath navigation to a variable of the type "result tree fragment."


  ...
  
  
  ...

If you don't reuse the content of result tree, you can put copy-of statement to where you want your output to be.


variable has a scope, like the following example shows, the $castList is referenced outside of the definition scope, so it is illegal.


XSLT note 2 - copy template

Here is a template that copy original xml file.

  
    
      
    
  

XSLT note 1 - built-in template

A simple xslt is a empty xslt.



    
  
  

But it does something, it actually is equivalent to the following style sheet. Those template are built-in and can not be removed.



    
  
  
    
  

  
    
  

  

Let's take a look what is the function of the template. The first template apply all the nodes match "* | /" with this template. * is any node, "/" is root node. The element first selects a set of nodes using the expression specified in the select attribute. If this attribute is left unspecified, which here is this case, all children of the current node are selected. For each of the selected nodes, directs the XSLT processor to find an appropriate to apply. Templates are tested for applicability by comparing the node to the XPath expression specified in the template's match attribute. If more than one template satisfies the match pattern, the one appearing with the highest priority is chosen. If several templates have the same priority, the last in the style sheet is chosen.

It seems that is same as , but it is not. It is same as . If we want attribute is included we should use . This template basically output all the value of element nodes.

Feb 26, 2008

xsl:sort

Normally, this element used with for-each and apply-templates

            

            

Feb 25, 2008

current()

The function returns a node set that has the current node as its only member. For an outermost expression, an expression not occurring within another expression, the current node is always the same as the context node. Thus,


is the same as

However, within square brackets, the current node is usually different from the context node. For example,


processes all elements that have a parent element and a name attribute with value equal to the value of the current node's ref attribute. This is different from


which means the same as
            
and so would process all elements that have a parent element and that have a name attribute and a ref attribute with the same value.

Feb 18, 2008

import external style sheet

            
            

            
            

            

asp.net xslt output

XslTransform xslt = new System.Xml.Xsl.XslTransform();             xslt.Load(xslFile);  
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
StringWriter writer = new StringWriter();
XmlTextWriter xmlwriter = new XmlTextWriter(writer);
XsltArgumentList list = new XsltArgumentList();
list.AddParam("Age", "", "108");
xslt.Transform(doc, list, xmlwriter);
xmlwriter.Close();
writer.Close();
Response.Write(writer.ToString());
writer.Dispose();
Response.Flush();
Response.Close();

xslt comparison

Below is an example of comparision.
            
            
            He is male
            
            
            She is a female
            
            
            
If the xpath expression return more than one value, that is a sequence. For example Program/@flag = 'favorite' , Program/@flag has more than one value, XSLT processor goes through each of the flag attributes in turn, if it finds any flag attribute that has the value 'favorite', then the comparison returns true. This expression means if the sequence has any value equals to 'favorite'. If you mean, none of sequence has value of 'favorite', you can not use Program/@flag != 'favorite', (which means any of sequence is not equal to 'favorite'), you should use not(Program/@flag = 'favorite')

Template priority

XSLT processor will only ever process one template when you apply templates to a node, so it has to choose one among all matched templates based on the matched priority.