Good alternative to XSLT for generating text output from [closed]
Good alternative to XSLT for generating text output from [closed]
I would like to create an online web application that performs text transformation from a visualized object into text output using a template system.
I would like to turn each of these instances of structured objects into text output based on a template specified at run time (from text file or database). (i.e., think these are blocks in a flowchart diagram with different attributes (but well-defined schema) in each instance.) The UI is out of scope here.
For example, the internal data for an object could be:
"InstanceName": "dummySquare"; "ShapeType": "Square"; "Length": "5"; "Unit": "cm"
"InstanceName": "dummySquare"; "ShapeType": "Square"; "Length": "5"; "Unit": "cm"
I would like to take that data and "mail merge" it with a text template to generate text based on the template.
Output could be something like:
<InstanceName value="dummySqure"><ShapeType value="Square" /><Length value="5" /><Unit value="cm" /></InstanceName>
<InstanceName value="dummySqure"><ShapeType value="Square" /><Length value="5" /><Unit value="cm" /></InstanceName>
I wonder if there is an elegant framework that would excel in this kind of transformation? Prefer solutions in languages that work well in web applications such as Python/Django, c# (ideally cross-platform), etc. If XML/XSLT is the best solution, please do let me know too!
Note: I haven't decided on the internal data format yet, but they will most likely be something like an XML or a JSON string, etc..
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
Thanks! This might work. I will investigate. Does the razor template need to be well formed?
– The Lyrist
Aug 21 at 10:06
1 Answer
1
It is easy with xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
public class Program
private static void Main()
string[,] input = "InstanceName", "dummySquare" , "ShapeType", "Square" , "Length", "5" , "Unit", "cm" ;
XElement instance = null;
for (int i = 0; i <= input.GetUpperBound(0); i++)
if (i == 0)
instance = new XElement(input[i, 0], new XAttribute("value", input[i, 1]));
else
instance.Add(new XElement(input[i,0], new XAttribute("value", input[i, 1])));
Thanks for your answer but it doesn’t really solve my problem because I need to be able to specify the template at run time.
– The Lyrist
Aug 21 at 10:08
Is the template a string? You said input was an object so I'm not sure what you actually mean.
– jdweng
Aug 21 at 10:24
Input (object) + template (string) = output (string). Similar to the good old mail merge or email template, etc. The template will be from a text file (or database) that allows users to change at run time as required.
– The Lyrist
Aug 21 at 10:25
The mail merge template was not a string template, it was an object that built using a tool. Not the same that you are looking at. You are looking at a custom parser which can be done in c#. Probably use Regex.
– jdweng
Aug 21 at 10:34
An email template is a string. If the template is “Hi %%first_name%%. %%message%%. From %%sender%%” and merged with an object with the appropriate attributes. it will become “hi jdweng, thanks for your help. From TheLyrist”. That’s what a template does and how mail merge done. If I update the template to “bonjour %%first_name%%. Etc the output will then change accordingly. It has nothing to do with parsing or regex.
– The Lyrist
Aug 21 at 10:39
razor engine perhaps ?
– Marc Gravell♦
Aug 20 at 21:52