在 php 中使用 xslt 替换 xml 元素的同义词有意义吗?
我正在使用 PHP 为 xml 格式 (SimpleXML) 的元数据建立索引。在过程中(以及稍后的时间)我想通过元素的属性/位置/名称重写(和删除)元素。我想 xslt 很适合这样做,但由于性能问题我有点害怕。所以最好形成一个正则表达式作为规则并直接使用 php 进行编辑?对于这些替代方案,我不喜欢我找不到可以进一步帮助我的操作语法。
那么你会如何做或将如何做呢? 感谢您的帮助, 罗伯特
i am indexing metadata in xml format (SimpleXML) with PHP. In process (and some later times) i want to rewrite (and delete) elements by their attributes/position/name . I guess that xslt is good for doing so but i am a bit scary because of performance. So it might be better to form a regexp as rule and edit directly with php? For these alternative i dislike that i could not find a manipulation syntax with help me further.
So how you do or would do this?
Thanks for your help,
Robert
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑在一般情况下是否可以用正则表达式表达对 XML 文档中任意深度的节点的删除/添加/替换操作(更不用说同时满足其他条件,例如基于属性,命名空间、深度、文本节点)。
这解释了为什么您找不到任何此类正则表达式。
另一方面,任何此类操作对于 XSLT 来说都是微不足道的,人们在半分钟内写出解决方案,然后忘记了这个“问题”,因为确实有更重要的事情要做。
郑重声明,下面是一个元素重命名操作(名为
a
的 amy 元素重命名为h
):例如应用于此 XML 文档:
生成了所需的正确结果:
I doubt it is possible in the general case to express with a regular expression a delete/add/replace operation for a node at an arbitrary depth in an XML document (not to mention simultaneously satisfying other conditions such as based on attributes, namespace, depth, text-nodes).
This explains why you couldn't find any such RegEx.
On the other side any such operation is trivial with XSLT and one writes the solution in half a minute and forgets about this "problem" because there are really more important things to do.
For the record, below is an element renaming operation (amy element named
a
is renamed toh
):when applied for example to this XML document:
the wanted, correct result is produced: