将 XML 转换为新 XML 格式的方法;如果使用对象,会限制配置能力吗?

发布于 2024-11-03 23:39:19 字数 2545 浏览 1 评论 0原文

对将 XML 从一种格式转换为另一种格式的方法有疑问。

我正在执行一项任务,从我无法控制的对象中获取序列化数据。我已经为此 XML 生成了一个对象。我们将其称为“表单实例”。表单实例的嵌套非常复杂,并且有很多不相关的布局组件和嵌套,我的理解使得很难使用 XSLT 来实现这一点。

我需要获取 Form 实例,并将其属性分配给另一个对象中的值。我们将另一个对象称为“标准格式”。标准格式对象将自身序列化为我最终需要的格式。

表单实例是用户填写表单的序列化结果。这个表单“定义”可以随时更改,并且像表单布局更改这样简单的事情会完全改变生成的序列化的嵌套结构 - 一致的是序列化控件中的某个位置表示我需要来自的值 - 并且我想要配置我想要的值。

我正在创建自己的 XML“配置文件”,它将允许指定我希望最终加载到“标准格式”对象中的表单实例中的哪些控件。

表单实例的结构如下:

<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>

如您所见,我并不介意表单实例中存在字段的位置(如果有人删除组标题或添加一些新布局,它可以随时更改),因为我可以递归地访问它们,如果我使用控件“名称”创建一个配置文件。

我想让我的“配置文件”足够可重用,因为当数据添加到“表单实例”时,可以对我的配置文件进行调整,说“我现在想要这个其他控件添加到表单定义中”,这会将其写入“标准格式”。另外,我也希望可以从此文件中配置最终输出。

“标准格式”对象中的序列化可以满足我的需求 - 如果我以特定于我想要最终得到的结果的方式使用它(它是灵活的)。不过我想知道如何才能将表单实例数据放置到该对象中-可配置。如果我将其作为对象使用,那么当需要更改生成的序列化格式时,我需要更改代码。

我的想法是否错误,因为我知道“标准格式”对象输出什么,所以我可以在“配置文件”中创建一个类似令牌的配置部分以允许格式规范?

例如: 在我的“表单实例”上,我至少有 2 个字段,我希望最终以“标准格式”序列化的形式表示它们。如果有人更改了所定义的基础表单的定义,这两个字段可以随时更改“表单实例”序列化中的位置/嵌套。

由于我们的目标不是使用标准格式对象,而是最终得到它序列化的内容,因此我可以将对象步骤删除并仅针对我想要的 XML 进行建模吗?例如,如果我向标准格式添加“Person”对象,这就是标准格式将产生的结果。

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

像这样制作我的 XML 配置文件是否可行:

<FormInstanceOutputConfiguration> 
  <Control id="FirstName" type="TextField">
  <Control id="LastName" type="TextField">
  <Control id="Gender" type="Radio">
  <CustomOutputTemplate>
    <Person Gender="[Gender]">
      <Name>
        <First>[FirstName]</First>
        <Last>[LastName]</Last>
      </Name>
    </Person>
  </CustomOutputTemplate>
</FormInstanceOutputConfiguration>

所需的输出可以更改,而无需更改“标准格式”模型。如果在这种情况下 Person 被认为是一个参与者,那么它可以位于标准格式对象中的“”下,也可以嵌套在“”等其他内容下。我有多个表单,但每个表单中的所有“控件”都将具有相同的输出。因此,在这种情况下,主表单将有一堆不同的人员,所有人员都需要以相同的格式显示。

如果最终我们需要的是序列化输出,那么如果我绕过标准格式对象会很糟糕吗?

感谢您的任何想法。

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="LastName" 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小…楫夜泊 2024-11-10 23:39:19

就我个人而言,我认为 xslt 仍然是您最好的选择;它对于 XML 转换确实非常通用。

如果您的数据是基于对象的,您可以查看 XmlAttributeOverrides(您可以将其提供给 XmlSerializer)是否满足您的需要;它允许与代码属性无关的 XML 映射,因此允许同一模型的并行映射 - 但它不像 xslt 扫描那样丰富;并且您需要小心缓存和重用使用 XmlAttrributeOverrides 创建的任何 XmlSerializer 实例,否则会泄漏无法收集的动态程序集。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文