使用 XSLT 反转数据和属性
使用 XSLT 我想将此 XML: 转换
<exchangeRates>
<rate country="aud">0.97</rate>
</exchangeRates>
为此 XML:
<xchgRates>
<entry xrate="0.97">aud</entry>
</xchgRates>
编辑:exchangeRates 需要变为 xchgRates。将 xRate 更改为 xrate 以匹配正确的解决方案。
感谢大家的帮助!
Using XSLT I want to tranform this XML:
<exchangeRates>
<rate country="aud">0.97</rate>
</exchangeRates>
into this XML:
<xchgRates>
<entry xrate="0.97">aud</entry>
</xchgRates>
EDIT: exchangeRates needs to become xchgRates. Changed xRate to xrate to match correct solution.
Thanks for all your help guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我还没有尝试过这个,但类似这样的东西应该有效:
I haven't tried this but something like this should work:
完整而简短的解决方案:
当此转换应用于提供的 XML 文档时:
生成所需的正确结果:
解释:
使用和覆盖身份规则/template
使用 AVT (属性值模板)是推荐的,因为它需要更少的输入,并且可以生成更短、更易于理解和维护的代码。
除了少数例外(特别是
select
属性)之外,XSLT 指令的几乎所有属性都允许 AVT。A complete and short solution:
when this transformation is applied on the provided XML document:
the wanted, correct result is produced:
Explanation:
Using and overriding the identity rule/template
Using AVT (Attribute-Value-Templates) is recommended as it needs less typing and results in shorter, more understandable and maintainable code.
Almost all attributes of the XSLT instructions with a few exceptions (notably the
select
attribute) allow AVTs.