Using Xpark.exe to transform Xml
opensource, programming, spark, xml, xslt March 23rd, 2009A little while ago James Gregory introduced Docu – a simple doc gen for .Net.
From the Docu project site:
What’s so special about docu?
* One command to build your entire documentation
* Static html, nothing fancy, no dependencies
* Templates built using the Spark view engine
* No GAC, no hard-coded paths; completely redistributable
I thought the use of Spark in a command line utility was remarkably clever. I was inspired into adding a utility to the Spark project, Xpark.exe, for transforming any xml using spark templates.
It’s not part of the RC1 on Codeplex, but you can get the bits from CodeBetter’s TeamCity site.
It’s pretty nice if you’re looking for an xslt alternative. Especially if you are using LINQ to XML to create an anonymous class model at the top of your template.
<var results="
from s in Model.Elements('stories').Elements('story')
select new {
link = (string)s.Attribute('link'),
title = (string)s.Element('title'),
description = (string)s.Element('description'),
thumbnail = s.Elements('thumbnail').Any() ? new {
src = (string)s.Element('thumbnail').Attribute('src')
} : null
}"/>
<html>
<head>
<title>Top Stories</title>
<style type="text/css">
<include href="diggstyle.css" parse="text"/>
</style>
</head>
<body>
<div each="var story in results" class="story">
<h3><a href="!{story.link}">${story.title}</a></h3>
<p>
<img if="story.thumbnail != null" class="thumbnail"
src="!{story.thumbnail.src}"/>
${story.description}
</p>
</div>
</body>
</html>
It’s intended to be used as a step in automated processes, like nant, nunit, or a wget/curl of a rest GET endpoint. That’s why there are stdio/stdout fallbacks for the source xml and rendered output.
curl "http://services.digg.com/stories?count=10&appkey=http://hello.com" | (continued...)
..\xpark diggstories.spark >livestories.html
>Xpark.exe
Transforms Xml using a Spark template
XPARK templatefile [inputfile [outputfile]]
templatefile Path to a .spark file.
inputfile Source xml. Path to an file or url for http GET.
outputfile Target file to receive template output.
If inputfile or outputfile are not provided stdin and stdout are used.
The Model in the template is an XDocument loaded from the source.
The templatefile location may also contain _partial.spark files, and
a _global.spark file with common namespaces, macros, etc.
March 24th, 2009 at 5:27 am
Nice…forgive me for the dumb question is there anyway of doing it with code? (without using the process class).A .net class that would get the spark file and do the trasformation?
March 24th, 2009 at 10:29 am
Absolutely, it’s just a few lines of code to compile and execute a view from code. Here’s the Main() from xpark
http://github.com/loudej/spark/blob/0968236ef9800235931af12baf9bd37d70922720/src/Xpark/Program.cs
The only other thing you need is a base class with any additional properties and methods.
public abstract class SparkView : AbstractSparkView
{
public XDocument Model { get; set; }
}