xml获取参数值childs并输出到csv
我有以下 xml 结构,
<transport titel="vervoer">
<type titel="car">
<brand titel="volvo">
<color titel="kleur">red</color>
</brand>
</type>
<type titel="car">
<brand titel="volvo">
<color titel="kleur">green</color>
</brand>
</type>
<type titel="car">
<brand titel="ford">
<color titel="kleur">red</color>
</brand>
</type>
<type titel="bike">
<brand titel="trek">
<color titel="kleur">blue</color>
</brand>
</type>
</transport>
我可以使用以下 xsl 创建一个 csv:
<xsl:template match="/">
<xsl:for-each select="/transport/type/brand/color">
<xsl:variable name="color" select="@titel" />
<xsl:variable name="brand" select="../@titel" />
<xsl:variable name="type" select="../../@titel"/>
<xsl:value-of select="concat($type,$brand,$color)" />
</xsl:for-each>
</xsl:template>
这给出了单行上每个节点的输出:
car,volvo,red
car,volo.green
car,ford,red
bike,trek,blue
但是这种方法有两个问题
1.有没有办法从顶部遍历树并在可用时显示所有标题?如果存在没有颜色子节点的节点,则不会显示该节点。
<type titel="bike">
<brand titel="trek">
</brand>
</type>
2.我想要我的csv的输出如下:
car,volvo,red
, ,green
,ford,red
bike,trek,blue
i have the following xml structure
<transport titel="vervoer">
<type titel="car">
<brand titel="volvo">
<color titel="kleur">red</color>
</brand>
</type>
<type titel="car">
<brand titel="volvo">
<color titel="kleur">green</color>
</brand>
</type>
<type titel="car">
<brand titel="ford">
<color titel="kleur">red</color>
</brand>
</type>
<type titel="bike">
<brand titel="trek">
<color titel="kleur">blue</color>
</brand>
</type>
</transport>
i can create a csv with the following xsl:
<xsl:template match="/">
<xsl:for-each select="/transport/type/brand/color">
<xsl:variable name="color" select="@titel" />
<xsl:variable name="brand" select="../@titel" />
<xsl:variable name="type" select="../../@titel"/>
<xsl:value-of select="concat($type,$brand,$color)" />
</xsl:for-each>
</xsl:template>
this gives output with every node on a single row:
car,volvo,red
car,volo.green
car,ford,red
bike,trek,blue
BUT there are two issues with this approach
1. is there a way to walk the tree from the top and show all titels when available? if there is a node without the color child it will not be displayed.
<type titel="bike">
<brand titel="trek">
</brand>
</type>
2.i want the output of my csv like this:
car,volvo,red
, ,green
,ford,red
bike,trek,blue
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此样式表:
输出:
编辑:按注释的请求进行分组,此样式表:
输出:
注意:不需要有序的输入源。固定字段,因为通用解决方案(按动态键嵌套分组)将是 XSLT 1.0 中最复杂的任务
This stylesheet:
Output:
EDIT: Grouping as request by comments, this stylesheet:
Output:
Note: There is no need for an ordered input source. Fixed fields because a general solution (nested grouping by dynamics keys) would be the most complex task in XSLT 1.0
您的问题是您在
for-each
中选择了
,因此您将错过每个没有的
后代。使用
for-each
进行处理的替代方法可能是这样的:示例输入
示例输出
要从输出中省略不需要的元素,只需添加一个空模板:
Your problem is that you select
<color>
in yourfor-each
so you will miss every<type>
that doesn't have a<color>
descendant.An alternative to processing with
for-each
could be this:sample input
sample output
To omit unwanted elements from your output just add an empty template: