tags:

views:

264

answers:

3

Given:

  • Two similar and complex Schemas, lets call them XmlA and XmlB.
  • We want to convert from XmlA to XmlB
  • Not all the information required to produce XmlB is contained withing XmlA (A database lookup will be required)

Can I use XSLT for this given that I'll need to reference additional data in the database? If so what are the arguments in favour of using XSLT rather than plain old object mapping and conversion? I'm thinking that the following criteria might influence this decision:

  • Performance/speed
  • Memory usage
  • Code reuse/comlpexity

The project will be C# based.

Thanks.

+1  A: 

If the data isn't in the xml, then xslt will be a pain. You can provide additional documents with xsl:document(), or you can use xslt extension methods (but that is not well supported between vendors). So unless you are dead set on xslt, it doesn't sound like a good option on this occasion (although I'm a big fan of xslt when used correctly).

So: I would probably use regular imperative code - streaming (IEnumerable<T>) if possible. Of course, unless you have a lot of data, such nuances are moot.

Marc Gravell
+4  A: 

With C# you can always provide extension objects to XSLT transforms, so that's a non-issue.

It's hard to qualitatively say without having the schemas and XML to hand, but I imagine a compiled transform will be faster than object mapping since you'll have to do a fair amount of wheel reinventing.

Further, one of the huge benefits of XSLT is it's maintainability and portability. You'll be able to adapt the XSLT doc really quickly with schema changes, and on-the-fly without having to do any rebuilds and takedowns if you're monitoring the file.

Could go either way based on what you've given us though.

annakata
+1 from me -- seems nothing more to add, at least for XSLT 1.0. One could describe in an answer the possibilities with XSLT 2.0 Schema-Aware processor, but at present doing this in combination with .NET may seem a little bit exotic. Nevertheless, this would be ideal.
Dimitre Novatchev
+2  A: 

My question is how likely are the set-of-transformations to change?

If they won't change much, I favor doing it all in one body of source code -- here that would be C#. I would use XSD.exe (.NET XSD tool) generated serialization classes in conjunction with data layers for this kind of thing.

On the other hand, if the set-of-transformations are likely to change -- or perhaps need to be 'corrected' post installation -- then I would favor a combination of XSLT and C# extensions to XSLT. The Extension mechanism is straightforward, and if you use the XslCompiledTransform type the performance is quite good.