开发者

Approach for transforming XML to new XML format; If using Objects, limits configuration abilities?

Have a question regarding an approach to transforming XML from one format to another.

I am working on a task in which I am taking serialized data from an object I do not have control over. I have generated an object for this XML. Lets called this the "Form Instance". Form Instance is very complicated with nesting, and lots of irrelevant layout components and nesting which my understanding makes it hard to use XSLT for this.

I need to take the Form instance, and assign properties from it - to values in another object. Lets call the other object "Standard Format". The standard format object serializes itself to the format I need to ultimately get to.

The Form Instance is the resulting serialization from a user filling out a Form. This Form "definition" can be changed anytime and something as simple as a layout change to the form completely changes the nesting structure of the resulting serialization - what is consistent is that somewhere in the serialization Controls are represented that I need values from - and I want to configure which ones I want the values from.

I am creating my own XML "Configuration File", which will allow specification of which Controls from the Form Instance I want to ultimately have loaded into the "Standard Format" object.

Form Instance is structured like this:

<Form name="MainForm">
<Control type="GroupHeader">
<Control type="GroupHeader">
<Control type="text" name="FirstName"/>
<Control type="text" name="LastName"/>
</Control>
<Control type="Radio" name="Gender"/>
</Control>
<Control>
<Form name="SomeOtherSubForm">
<Control type="text" name="AnotherPersonFirstName"/>
<Control type="text" name="AnotherPersonLastName"/>
</Form>
</Control>
</Form>

As you can see I don't mind really where the fields exist in Form instance(and it can change anytime if someone removes a group header or adds some new layout) as I can get to them recursively if I create a configuration file with the Control "name".

I would like to make my "Configuration File", re-usable enough in the sense that - when data is added to the "Form Instance" - an adjustment can be made to my configuration file, saying "I now want this other control which was added to the Form definition" which will write it to the "Standard Format". Also, the ultimate output I would like to be configurable from this file as well.

Serialization in the "Standard Format" object gets me what I need - if I work with it in a way specific to what I'm trying to end up with (it is flexible). However I am wondering, how I can make how the placement of my Form Instance data into that object - configurable. If I work with it as an object, then when the resulting serialization format needs to change I need to change code.

Am I wrong in thinking, that since I know what the "Standard Format" object outputs, that I can create a token-like configuration section in my "Configuration File" to allow for the specification of the format?

For example: On my "Form Instance" I have at least 2 fields that I want to eventually have represented in what "Standard Format" serializes to. These 2 fields can change position/nesting in the "Form Instance" serialization anytime if someone changes the definition of the underlying Form that is defined.

Since our objective is not to work with the Standard Format object, but end up with what it serializes, can I cut the object step out and just model towards the XML I want. For example this is what the Standard Format would produce if I added a "Person" object to it.

<StandardFormat>
  <Person Gender="M">
    <Name>
      <First>Joe</First>
      <Last>Smith</Last>
    </Name>
  </Person>
</StandardFormat>

Would it be OK approach to make my XML Configuration file like this:

<FormInstanceOutputConfiguration> 
  <Control id="FirstName" type="TextField">
  <Control id="LastNa开发者_运维技巧me" type="TextField">
  <Control id="Gender" type="Radio">
  <CustomOutputTemplate>
    <Person Gender="[Gender]">
      <Name>
        <First>[FirstName]</First>
        <Last>[LastName]</Last>
      </Name>
    </Person>
  </CustomOutputTemplate>
</FormInstanceOutputConfiguration>

The desired output can change without the "Standard Format" model needing to change. If Person in this case was thought of as an actor, it can go under "" in the standard format object, or it could be nested under something else like "". I have multiple forms, but all of the "Controls" in each form will have same output. So in this case the main Form will have a bunch of different Persons all needing to come out in the same format.

So is it bad if I go around the Standard Format object, if ultimately all we need is a serialization output?

Thank you for any ideas.


Personally I think xslt is still your best bet here; it really is very versatile for XML transforms.

If your data is object-based, you could see whether XmlAttributeOverrides (which you can feed to XmlSerializer) does what you need; it allows XML maps that are unrelated to the code attributes, and thus allows parallel maps for the same model - but it is not as rich as an xslt sweep; and you need to be careful to cache and reuse any XmlSerializer instances created using XmlAttrributeOverrides, or it will leak uncollectable dynamic assemblies.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜