2
我需要将xml数据转换为其他格式。我做了很少XSL的东西,我的互联网搜索没有给我需要的结果。我有这个样本XML:使用XSL将XML转换为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>
几点需要注意:
- ColMetaData/ColList /列的输入XML的名称属性最终成为输出xml的每个Row元素的属性名称。
- RowList/Row/ColList/Col在输入xml中的值最终成为输出xml中属性的值,该属性与ColMetaData/ColList/Col部分中的Pos具有相同的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
我希望得到任何帮助,我能搞定! 感谢 SRINIVAS
非常感谢你Dimitre这按预期工作。 – 2012-01-03 17:13:42
@SrinivasPalakala:不客气。 – 2012-01-03 17:23:16