我的 PHP 应用程序需要导出为一系列不同的 XML 格式:我应该使用 XSLT 还是本机 PHP?
我的 PHP 应用程序需要能够导出(和导入)一系列不同的数据格式,主要是基于 XML 的。
我可以选择
- 在 PHP 中,使用 DOM 导出到某种基于 XML 的格式,该格式是其他格式所需的所有数据的超集,并为我想要支持的每种输出格式创建一个单独的 XSLT 样式表,通过运行 DOM 输出PHP 的 XSL 扩展。
或者
- 不使用 PHP 的 XSL 扩展,而是将每种输出格式实现为本机 PHP 中的一个类,该类使用 DOM 直接从内部对象/结构转换为给定的 XML 格式,每个此类都实现相同的接口,因此它们可以互换。
该应用程序将由大学使用,是一种以各种方式管理“人员”记录以及从各种来源(例如人力资源系统等)导入/导出的工具。我将自己实现一组输入和输出格式,但是将来,使用它的人可能会想要修改它以支持他们自己的格式。
我考虑不使用 XSLT 的原因之一是,如果将来除了我之外还有其他人维护该应用程序,似乎很少有人知道 XSLT - 似乎有更多的人知道 PHP。
另一个是,第二个似乎是一个更高效和“编程”的解决方案,并且更灵活,因为我可以通过重载来轻松地输出和导入非 XML 格式(如 CSV 或基于列的文本)课堂上必要的部分,但并不经常是必要的。
第三个但非常小且无关紧要的原因是 PHP 需要重新编译才能启用 XSL,而 DOM 默认情况下启用,因此它会更便携一些。 不过,这并不是什么大问题,因为重新配置 PHP 很容易。
你觉得我的推理怎么样?
My PHP application will need to be able to export to (and import from) a range of different data formats, mostly XML based.
I have the option of
- In PHP, use DOM to export to some XML based format that is a superset of all the data needed by the others, and create a separate XSLT stylesheet for each output format I want to support, running the DOM output through PHP's XSL extension.
or
- Not using PHP's XSL extension, but implementing each output format as a class in native PHP that translates directly from the internal objects/structures to a given XML format using DOM, each such class implementing the same interface so they are interchangeable.
The app will be used by universities and is a tool that manages 'people' records in various ways, and imports/exports from various sources like their HR system, etc. I'll be implementing the set of input and output formats myself, but in future there is the chance that someone using it will want to modify it to support their own format.
One reason I'm considering NOT using XSLT is that if anybody else is going to maintain the app in future other than me, it seems that very few people even know XSLT - a lot more people seem to know PHP.
Another is that the second seems like a more efficient and 'programmery' solution, and more flexible in the sense that I'd be able to output to and import from non-XML formats like CSV or column based text just as easily by overloading the necessary parts of the class, not that that would often be necessary.
A third, but very small and insignificant reason is that PHP needs recompiling to enable XSL whereas DOM is enabled by default, so it would be a tiny bit more portable. However this isn't too much of a problem as it's easy to reconfigure PHP.
What do you think of my reasoning?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我个人的观点是,您的决定应该主要基于您如何判断目标受众的 XSLT 知识这一事实。 如果很明显 XSLT 在必须使用该系统的人员(包括您自己)中某种程度上是“未知领域”,那么您可以排除 XSLT 解决方案。 学习 XSLT 的需要和努力将抵消您从 XSLT 解决方案中获得的优势(非常优雅 - 特别是在将 XML 转换为 XML 时,无需弄乱 PHP 代码,无需 PHP 知识)。
也许双向解决方案可能是正确的选择。 您可以构建一个用于导入和导出的适配器系统,该系统将 XSLT 用于 XML 数据格式,并提供对所有不基于 XML 的数据格式使用 PHP 代码的能力。 这样每个开发人员都可以选择他更舒服的方式。
My personal opinion is that your decision should be based strongly on the fact how you'd judge the XSLT knowledge in your intended audience. If it's clear that XSLT is somehow "terra incognita" among the people having to work with the system (that includes yourself), you can rule out the XSLT-solution. The need and the effort to learn XSLT will negate the advantages (very elegant - especially when transforming XML to XML, no need to mess with PHP code, no PHP knowledge needed) you'll get from the XSLT-solution.
Perhaps a two-way solution could be the right thing to go with. You could build an adapter-system for importing and exporting that uses XSLT for the XML data formats and provides the ability to use PHP code for all the data formats that aren't XML based. This way every developer can choose the way he's more comfortable with.
我认为你的推理是合理的,这也是我会采取的方式。
基本上你所说的是桥/适配器/外观类。 它们(恕我直言)比 XSL 模板更灵活、更容易理解。
第三个原因并不是真正的原因,因为 XSL 支持只不过是取消 PHP 扩展的注释。
我也很高兴看到您希望通过 DOM 扩展(或等效库)来完成此操作,而不是将 XML 编写为文本,这会引入所有转义问题以及您将避免的问题。
就我个人而言,我还认为 XSL 模板更容易更改(因为它们对绝大多数程序员来说更加神秘),因此如果您的超集格式发生变化(让我们面对现实,它会发生变化),您可能需要更改您的所有模板。 当然,您可能也必须使用代码来执行此操作,但代码可能会更容易维护。
I think your reasoning is sound and it's the way I would go as well.
Basically what you're talking about are bridge/adapter/facade classes. They are (imho) more flexible and easier to understand than XSL templates.
The third reason isn't really one because XSL support involves nothing more than uncommenting a PHP extension.
I'm also glad to see you want to do this via the DOM extensions (or equivalent library) rather than writing XML as text, which introduces all the escaping issues and whatnot you'll avoid your way.
Personally I also think that XSL templates are more fragile to change (by virtue of them being somewhat more cryptic to the vast majority of programmers) so if your superset format ever changes (which, let's face it, it will) you'll potentially need to change all your templates. Of course you may have to do this with code too but code will probably be easier to maintain.
有趣的问题。
两种解决方案都有效,但我想您知道这一点。
我可能会自己使用编码解决方案。 但这可能与令我头疼的 XSLT 有关。
XSLT 有一个优点,那就是您可以要求由为您提供未更改的 XML 的人员来生成它们。
如果总是由您生成它们,那么这可能并不重要,并且编码解决方案会更容易大部分时间维持。
Interesting problem.
Both solutions will work, I guess you know this however.
I would probably go with the coded solution myself. But thats probably to do with XSLT giving me a headache.
There is an upside to XSLT and that is you can ask for them to be produced by the people giving you the unaltered XML..
If it is always you going to be producing them then it probably doesn't matter and coded solution will be easier to maintain most of the time.