【发布时间】:2012-02-01 04:37:47
【问题描述】:
我需要将 xml 数据转换成不同的格式。我做了很少的 XSL 东西,我的互联网搜索并没有给我需要的结果。我有这个示例 xml:
<MailStatusReport>
<Output>
<ColMetaData ColCount="5">
<ColList>
<Col Name="parcelid" Pos="1"/>
<Col Name="currentlocationid" Pos="2"/>
<Col Name="deliverystatus" Pos="4"/>
<Col Name="requestedlocationid" Pos="3"/>
<Col Name="requestor" Pos="5"/>
</ColList>
</ColMetaData>
<RowList>
<Row>
<ColList>
<Col Pos="2">Delaware</Col>
<Col Pos="1">001</Col>
<Col Pos="3">NewYork</Col>
<Col Pos="4">InRoute</Col>
<Col Pos="5">John</Col>
</ColList>
</Row>
<Row>
<ColList>
<Col Pos="1">002</Col>
<Col Pos="2">Sanjose</Col>
<Col Pos="3">Michigan</Col>
<Col Pos="4">Delivered</Col>
<Col Pos="5">Rob</Col>
</ColList>
</Row>
</RowList>
</Output>
</MailStatusReport>
期望的输出:
<MailStatusReport>
<Row parcelid="001" currentlocationid="Delaware" requestedlocationid="NewYork" deliverystatus="InRoute" requestor="John"/>
<Row parcelid="002" currentlocationid="Sanjose" requestedlocationid="Michigan" deliverystatus="Delivered" requestor="Rob"/>
</MailStatusReport>
注意事项:
- 输入 xml 中 ColMetaData/ColList/Col 的 Name 属性最终作为输出 xml 的每个 Row 元素的属性名称。
- 输入 xml 中的 RowList/Row/ColList/Col 的值最终成为输出 xml 中与 ColMetaData/ColList/Col 部分中具有相同 Pos 的属性的属性值。
- ColMetaData 的 ColCount 属性值得信赖,如果需要可以用作计数器。
我能想到的伪代码是:
For each CurrentRow in the RowList
Begin Building OutputRow
For each Col in ColMetaData/ColList
attrName = ColMetaData/ColList/Col/Name
attrPos = ColMetaData/ColList/Col/Pos
attrVal = CurrentRow/ColList/Col[@Pos=$attrPos]
Add $attrName=$attrVal to the outputRow
如果能得到任何帮助,我将不胜感激! 谢谢 斯里尼瓦斯
【问题讨论】: