如何将 xml 属性的值映射到其他一些值
我有一个类似于以下内容的 XML 文件:
<a>
<b value="a123" />
<b value="b234" />
<b value="c345" />
</a>
我需要将属性映射到其他值。例如,我想将 a123
替换为 q999
,将 b234
替换为 z998
和 c345
> 与u997
。使用 XSLT 是否可以有效地进行此类转换?映射本身是生成的,因此我可以将其转换为几乎任何格式。现在,假设它是以下 XML 文件:
<map>
<item from="c345" to="u997" />
<item from="b234" to="z998" />
<item from="a123" to="q999" />
</map>
也许有比 XSLT 更好的工具来执行此类转换?目前我只是通过该文件多次 sed
。显然,这个解决方案效率极低,而且根本无法扩展。
I have a XML file similar to the following:
<a>
<b value="a123" />
<b value="b234" />
<b value="c345" />
</a>
I need to map the attributes to some other value. For example, I want to replace a123
with q999
, b234
with z998
and c345
with u997
. Is it possible to do such transformation efficiently using XSLT? The mapping itself is generated, so I can convert it into almost any format. For now, let's say it's the following XML file:
<map>
<item from="c345" to="u997" />
<item from="b234" to="z998" />
<item from="a123" to="q999" />
</map>
Maybe there's a better tool than XSLT to do such transformation? Currently I just sed
through the file many times. Obviously this solution is horribly inefficient and doesn't scale at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就这么简单:
当此转换应用于提供的 XML 文档时:
生成所需的正确结果:
解释< /strong>:
覆盖身份模板 对于
value
属性whos 值等于地图中的from
属性。地图在转换中内联显示,并使用
进行访问document()
函数。或者,包含地图的文件的文件路径可以作为外部参数传递给转换,并且可以再次使用document()
函数,将此文件路径作为参数传递给它。As easy as this:
when this transformation is applied on the provided XML document:
the wanted, correct result is produced:
Explanation:
Overriding the identity template for
value
attributes whos value is equal to afrom
attribute in the map.The map is presented inline in the transformation and accessed using the
document()
function. Alternatively, the filepath to the file containing the map can be passed as external parameter to the transformation and the Map XML document can be accessed using again thedocument()
function, passing as argument to it this filepath.