Friday, March 9, 2012

Split string using XSLT 1.0

I hadn't got lots of prior experience with XSLT so far, but now I take part in a c++ project, part of a large scientific project, should create a simple report from xml data and display it in a browser. XSLT is a declarative language that describes well-defined rules to transform a source tree into a desired format (xhtml, xml, plain text, etc.) and it works pretty well, till the transformation logic is not too complex. If you want to split a string into parts with a given delimiter, it's not a problem at all with tokenize() in XSLT 2.0 and XPath 2.0, but as far as I know there is no free XSLT processor for c++ supports standard 2.0, so I created an own template for splitting strings into a table: 

My dummy xml looks very simple:

Any delimiter can be used by given at the parameter separator. Numerical data can be formatted with parameter numformat.

Template usage:
After transformation with Xalan XSLT processor, result is shown here:
x y z
1146.00 -626.28 16.16
1759.50 -494.00 55.54
400.35 -97.17 .05
1919.96 148.49 631.89
674.07 564.22 49.21

Visibility Flag 1 Flag 2 Color
visible on off orange
invisible off unknown white
visible on on RGB(128,30,230)

No comments:

Post a Comment