Showing posts with label XSLT. Show all posts
Showing posts with label XSLT. Show all posts

Monday, July 15, 2013

Comparison of XSLT and XQuery

Recently I am working on one project which gets involved in transforming a large XML document in Oracle SOA Suite 11g.  Initially this transformation is done using XSLT.   We did the end-to-end testing with a large payload and noticed that the XSLT transformation took the most of the message processing time.  Later someone suggests that with large payload XQuery is more efficient than XSLT.   So we decided to implement the same transformation using XQuery.   The comparison result is a bit surprise to me.  
  
<ein:ProcessRequest>
 <ein:DocumentID>4.IPM_013407</ein:DocumentID>
 <ein:InvoiceNumber>12345</ein:InvoiceNumber>
 <ein:InvoiceType>PO</ein:InvoiceType>
 <ein:InvoiceDescription>LYRECO JUN 2013</ein:InvoiceDescription>
 <ein:SupplierName>LYRECO PTY LTD</ein:SupplierName>
 <ein:SupplierSiteName>E-INVOICE</ein:SupplierSiteName>
 <ein:POInvoice>
  <ein:POInvoiceItems>
  <ein:InvoiceNo>2331940908</ein:InvoiceNo>
  <ein:Amount>195.42</ein:Amount>
  <ein:GSTAmount>17.77</ein:GSTAmount>
  <ein:InvoiceDate>2013-05-01</ein:InvoiceDate>
  <ein:Description>Some description text</ein:Description>
  <ein:Quantity>1</ein:Quantity>
  <ein:UnitPrice>195.42</ein:UnitPrice>
  <ein:ExpenseType>STATIONERY</ein:ExpenseType>
  <ein:ProjectNumber>200363</ein:ProjectNumber>
  <ein:TaskNumber>11040</ein:TaskNumber>
  <ein:ExpenditureType>Office Supplies</ein:ExpenditureType>
  </ein:POInvoiceItems>
  
  ............
  
 </ein:POInvoice> 
<ein:ProcessRequest> 

Firstly I tried the transformation of a XML message like the above but with over 2700 <POInvoiceItems> elements using XMLSpy.  The result shows XQuery is much faster than XSLT when transforming this message.   Roughly XQuery is 3~4 times faster. 

Then I put these two transformations into one Oracle SOA application respectively and invoke the application with the same large message payload.   The comparison show that under Oracle Weblogic environment there is no obvious difference between XQuery and XSLT and XSLT is even faster than XQuery.   I am not quite sure what cause the different results using XMLSpy and Oracle Weblogic.  Maybe it is related to the implementation of XQuery and XSLT enegines within XMLSpy and Oracle Weblogic.

Monday, April 8, 2013

Use preceding-sibling to find all unique elements

Recently I need to write a xpath expression to find all unique elements from one XML message.  The below is the simplified XML document that demonstrate the problem to be solved.  Here I want to get a list of all unique of all a elements.  The of values in the result list should be 123, 120, 129,345 and 340.

<?xml version="1.0" encoding="UTF-8"?>

<!--Sample XML file generated by XMLSpy v2008 sp1 (http://www.altova.com)-->

<db:p>

 <db:ss>

  <db:tt>

   <db:a>123</db:a>

   <db:a>120</db:a>

  </db:tt>

  <db:tt>

   <db:a>123</db:a>

   <db:a>129</db:a>

  </db:tt>

 </db:ss>

 <db:ss>

  <db:tt>

   <db:a>345</db:a>

   <db:a>120</db:a>

   <db:a>123</db:a>

   <db:a>340</db:a>

  </db:tt>

 </db:ss>

</db:p>


After some tries I come out with the following xpath expression.

/*:p/*:ss/*:tt/*:a[ not(. = ../preceding-sibling::*/*:a)  and not(. =  ../../preceding-sibling::*/*:tt/*:a)]

Here I use two preceding-sibling because <a> element is under <ss> and <tt> elements.

Thursday, February 17, 2011

Identity template in XSLT

Identity template in XSLT


Identity template is used to copy everything from the source to the destination. It demonstrates the charm of the recursion ability of XSLT. The below is the basic form of the identity template. 


Figure 1. Identity template
How does it work?


1. Firstly by using match=”@* node()” the template will be used for each attribute and each node which include element node, text node, comment and processing-instruction. But the namespace node is not included here.


2. Then for each matched node (attribute, element, text node or whatever) do the copy.


3. Finally inside apply the template recursively to child elements if the node has any. This will show the charm of XSLT’s recursive ability which makes the XSLT template very clean and powerful.

However that is just the basic form of the identity template. In the reality there seems no chance to make the exact copy from the source to destination. Quite often some modifications are required while doing the copy. For example a portion of XML needs to be copied but at the same time the value of one particular element is needed to be assigned a new value or a new attribute is needed to add to this particular element.

These variations to the basic form of XSLT identity template can be achieved via another template which works together with identity template. This template will handle the copy of one particular element or attribute. The key point is that this template has the specific match than the identity template and when one element or attribute matches this template, XSLT engine will choose this template to process the element or attribute instead of identity template although the element or attribute also matches the identity template. Why? Because the identity template is less specific.



Example1 


This example is quite simple and it just copies everything from the source to the target except the value of one element. 
 

Figure 2. Source


Figure 3. Transformation

Figure 4. Result

Example2
 
This example is a bit complicated than example 1.  What we need to do is to convert one root element from one name to another name and at the same time change the namespace XYZ to ABC.  All subelements keep the same name.     
 Figure 5. Source

 Figure 6. Transformation

 Figure 7. Result