XML converter - "architectural" question

G

Giulio Petrucci

Hi everybody,

I have to build an application that translates documents from an A-XML
format to a B-XML format. The problems need a lot of elementary methods
translating an a-node to a b-node, so I thought to encapsule these
functions in some classes, using a sort of Command pattern.

Do you have any other suggestion about?

Thanks in advance,
Kind regards,
Giulio - Italia
 
M

Marc Gravell

Would xslt suffice? You can use this with streams for maximum efficiency,
plus it is designed to do this job...

Marc
 
G

Giulio Petrucci

Marc Gravell ha scritto:
Would xslt suffice? You can use this with streams for maximum efficiency,
plus it is designed to do this job...

I know...
But I have to implement in a .NET dll something that was before implemented
using XSLT... so I cannot give the customer back the same thing he gave me! :)

Anyway: we need s .NET dll because this "translator" must be used in a
complex scenario by some different applications.

Thanks,
Giulio
 
M

Marc Gravell

A .net dll doesn't preclude using xslt to do the work; quite the opposite,
as below; you can always pass in additional options with your xml to control
the output if you need. By using xslt you get to capitalise on the transform
functions, including indexing, Munchean grouping, pattern-matching etc. But
its your call.

I can't advise on the command-pattern usage, as it just isn't the way I
would have thought of doing it... back to the floor - any takers?

Marc

// load the xslt template as a string from a resx
XmlDocument sourceDoc = new XmlDocument();
sourceDoc.LoadXml(TestProject.Properties.Resources.Transform);

// prepare a transform object
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(sourceDoc);

xsl.Transform(source, target);
 
G

Giulio Petrucci

Hi Marc,

Marc Gravell ha scritto:
A .net dll doesn't preclude using xslt to do the work;

But how can I log the "activity" of my application using xslt?

Thanks,
Giulio
 
M

Marc Gravell

Depends on what you mean by "activity"; if you mean individual runs of
the tranform (i.e. not the detail), then just log it in C# one side of
..Transform(); if you mean a blow-by-blow account of the steps you go
through, then I think (never tried) that you can use xsl:message (in
the xslt) to fire events back into C#; you catch them like:

XsltArgumentList args = new XsltArgumentList()
args.XsltMessageEncountered += args_XsltMessageEncountered; //
some handler; look at .Message

And use the overload on Transform() that accepts an XsltArgumentList

Marc
 
M

Marc Gravell

Working example:

using System;
using System.Xml;
using System.Diagnostics;
using System.Windows.Forms;
using System.Xml.Xsl;
using System.IO;
public class Program {
static void Main() {
XmlDocument templateDoc = new XmlDocument();
templateDoc.LoadXml(@"<?xml version=""1.0"" encoding=""UTF-8""
?>
<xsl:stylesheet version=""1.0""
xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"">
<xsl:template match=""*"">
<xsl:message>Found <xsl:value-of select=""name()""/></xsl:message>
<xsl:element name=""Freaked{name()}"">
<xsl:apply-templates select=""*""/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>");

XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(templateDoc);
XsltArgumentList args = new XsltArgumentList();
args.XsltMessageEncountered +=new
XsltMessageEncounteredEventHandler(args_XsltMessageEncountered);

XmlDocument input = new XmlDocument();
input.LoadXml("<Xml><Some><Data/><Data/></Some></Xml>");

using(MemoryStream stream = new MemoryStream()) {
transform.Transform(input, args, stream);

Debug.WriteLine(transform.OutputSettings.Encoding.GetString(stream.ToArray()));
}
}

static void args_XsltMessageEncountered(object sender,
XsltMessageEncounteredEventArgs e) {
Debug.WriteLine(e.Message);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top