如何使用 XSLT 转换一段简单的 XML
如何转换
<person>
<personFirstName>FirstName</personFirstName>
<personLastName>LastName</personLastName>
<personAge>40</personAge>
</person>
为使用 XSLT,如下所示:
<person>
<name>
<first>FirstName</first>
<last>LastName</last>
</name>
<age>40</age>
</person>
此外,如果输入 XML 是人员节点的集合,
<persons>
<person>
...
</person>
</persons>
How can you convert
<person>
<personFirstName>FirstName</personFirstName>
<personLastName>LastName</personLastName>
<personAge>40</personAge>
</person>
to
<person>
<name>
<first>FirstName</first>
<last>LastName</last>
</name>
<age>40</age>
</person>
using XSLT, moreover, if the input XML is a collection of person nodes, like so:
<persons>
<person>
...
</person>
</persons>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关键是身份转换并在需要时覆盖它。
示例 XML
示例 XSLT
输出
The key is the identity transform and overriding it when needed.
Sample XML
Sample XSLT
OUTPUT
“推送式”解决方案:
应用于此 XML 文档时:
生成所需的正确结果:
说明:
< strong>使用和覆盖标识规则/模板来包装和重命名元素。
要包装的元素在模式下重命名
renameWrapped
。personAge
元素在非修改模板中重命名,该模板覆盖名为personAge
的元素的身份规则。A "push-style" solution:
when applied on this XML document:
the wanted, correct result is produced:
Explanation:
Using and overriding the identity rule/template for wrapping and renaming of elements.
The elements to be wrapped are renamed in mode
renameWrapped
.The
personAge
element is renamed in a non-moded template that overrides the identity rule for elements namedpersonAge
.这应该很容易。您可以尝试:
person
,然后打开name
,应用模板,关闭name
,打开age
,获取值来自personAge
,关闭age
personFirstName
,打开first
,获取值,关闭first
personFirstName
的personLastName
相同,我认为 3 个不带循环的模板就足够了。试试吧!
It should be very easy. You can try to:
person
then openname
, apply templates, closename
, openage
, get value frompersonAge
, closeage
personFirstName
, openfirst
, get value, closefirst
personFirstName
forpersonLastName
I think 3 templates wihtout loops should be enough. Try it!