【发布时间】:2009-04-06 14:02:50
【问题描述】:
我不确定标题是否清楚地解释了我的问题,我会尽量包含尽可能多的细节。
我需要使用 Xslt 1 将下面的 xml 转换为格式正确的格式,以便我可以将其反序列化为 .net 类型。
源 XML
<ax21:result type="test.ws.Result">
<ax21:columnNames>fileName</ax21:columnNames>
<ax21:columnNames>lockedState</ax21:columnNames>
<ax21:columnNames>currentLockOwner</ax21:columnNames>
<ax21:columnNames>UUID</ax21:columnNames>
<ax21:resultData>Test1.doc</ax21:resultData>
<ax21:resultData>true</ax21:resultData>
<ax21:resultData>analyst</ax21:resultData>
<ax21:resultData>f48f0450-9ecc-4a44-b063-898d9d72d112</ax21:resultData>
<ax21:resultData>Test2.doc</ax21:resultData>
<ax21:resultData>false</ax21:resultData>
<ax21:resultData/>
<ax21:resultData>f48f0450-9ecc-4a44-b063-898d9d72d112</ax21:resultData>
<ax21:resultData>Test3.doc</ax21:resultData>
<ax21:resultData>true</ax21:resultData>
<ax21:resultData>analyst</ax21:resultData>
<ax21:resultData>f48f0450-9ecc-4a44-b063-898d9d72d112</ax21:resultData>
<ax21:resultData>Test4.doc</ax21:resultData>
<ax21:resultData>false</ax21:resultData>
<ax21:resultData/>
<ax21:resultData>f48f0450-9ecc-4a44-b063-898d9d72d112</ax21:resultData>
</ax21:result>
目标 XML
<result>
<item>
<fileName>Test1.doc</fileName>
<lockedState>true</lockedState>
<currentLockOwner>analyst</currentLockOwner>
<UUID>f48f0450-9ecc-4a44-b063-898d9d72d112</UUID>
</item>
<item>
<fileName>Test2.doc</fileName>
<lockedState>true</lockedState>
<currentLockOwner>analyst</currentLockOwner>
<UUID>f48f0450-9ecc-4a44-b063-898d9d72d112</UUID>
</item>
<item>
<fileName>Test2.doc</fileName>
<lockedState>true</lockedState>
<currentLockOwner>analyst</currentLockOwner>
<UUID>f48f0450-9ecc-4a44-b063-898d9d72d112</UUID>
</item>
</result>
这可以使用 xslt 完成吗?如果是,请发布链接或示例 xslt 供我尝试。
我正在使用 .net 2.0、c#、XSLT 1.0
【问题讨论】:
-
源 XML 只是格式的 XML。它绝对不符合 XML 的“精神”,因此任何使用 XSL 的解决方案充其量都是 hackish。如果您对源格式有任何控制权,我强烈建议您将其更改为分层格式。
-
解决方案中没有任何“hackish”。 XSLT 可用于为许多“非传统”或看似“不可能解决”的问题提供优雅的解决方案。