【发布时间】:2021-11-08 05:30:46
【问题描述】:
我想使用 xslt 将所有属性元素转换为逗号分隔的字符串作为 xml 中的一个元素
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Products>
<Product>
<productId>1</productId>
<ean>12345</ean>
<title>title A</title>
<Properties>
<Property><key>Colour</key><value>Red</value></Property>
<Property><key>Material</key><value>Plastic</value></Property>
</Properties>
</Product>
<Product>
<productId>2</productId>
<ean>54321</ean>
<title>title B</title>
<Properties>
<Property><key>Colour</key><value>Black</value></Property>
<Property><key>Gender</key><value>Boys</value></Property>
<Property><key>Material</key><value>Leather</value></Property>
</Properties>
</Product>
</Products>
使用 XSLT 的必需输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Products>
<Product>
<productId>1</productId>
<ean>12345</ean>
<title>title A</title>
<Properties><Property>Colour:Red,Material:Plastic</Property></Properties></Product>
<Product>
<productId>2</productId>
<ean>54321</ean>
<title>title B</title>
<Properties><Property>Colour:Black,Gender:Boys,Material:Leather</Property></Properties>
</Product>
</Products>
结果 xml 在没有嵌套结构的情况下被展平。如果有人有更好的想法将属性类型作为元素名,那就更好了。
如果可以的话:
<Properties><Colour>Black</Colour><Gender>Boys</Gender><Material>Leather</Material></Properties>
希望收到您的来信!
【问题讨论】:
-
请就您在尝试完成此操作时遇到的困难提出一个具体问题。否则,看起来您只是在寻找某人为您编写代码。另请说明您的处理器支持哪个版本的 XSLT。
-
请注意,只有当原始字符串始终是有效的 XML 元素名称时,才能使用字符串作为元素名称。
-
我不熟悉 xslt,所以我不知道该怎么做,也无法在网上找到它。所以是的,我要求在正确的方向上给我一个 xslt 来完成这个。
-
花一个小时学习 XSLT 教程,你就会知道怎么做。
-
在询问 XSLT 问题时,您需要提供 minimal reproducible example: (1) 输入 XML。 (2) 你的逻辑,以及试图实现它的 XSLT。 (3) 期望的输出。 (4) XSLT 处理器及其对 XSLT 标准的遵从性:1.0、2.0 或 3.0。