dataset.writexml XML 的 xsl 转换
我有一个场景,将 .net 数据集导出到 xml 文件,但我想将 xml 输出的结构转换为更具层次结构的结构。以下是 dataset.xmlwrite() 方法导出的数据集的格式。
<NewDataSet>
<Table>
<id>100</id>
<empname>Joe Smith</empname>
<phone>111-111-1111</phone>
<mobile>222-222-2222</mobile>
</Table>
<Table>
<id>101</id>
<empname>Ann Jensen</empname>
<phone>111-111-0000</phone>
<mobile>222-222-0000</mobile>
</Table>
<NewDataSet>
我想将其转换为以下结构。我是 xsl 转换的新手,我不确定如何防止 元素对数据集中的每条记录重复。
<NewDataSet>
<Table>
<employee id="100">
<empname>Joe Smith</empname>
<phone>111-111-1111</phone>
<mobile>222-222-2222</mobile>
</employee>
<employee id="101">
<empname>Ann Jensen</empname>
<phone>111-111-0000</phone>
<mobile>222-222-0000</mobile>
</employee>
</Table>
<NewDataSet>
我尝试使用 xsl:for-each 和 xsl:if 语句的组合来获得我想要的结果,但到目前为止我还无法让它工作。任何帮助将不胜感激。
I have a scenario where I am exporting a .net dataset to an xml file, but I want to transform the stucture of the xml output to a more hierarchical structure. Below is the format of the dataset as exported by dataset.xmlwrite() method.
<NewDataSet>
<Table>
<id>100</id>
<empname>Joe Smith</empname>
<phone>111-111-1111</phone>
<mobile>222-222-2222</mobile>
</Table>
<Table>
<id>101</id>
<empname>Ann Jensen</empname>
<phone>111-111-0000</phone>
<mobile>222-222-0000</mobile>
</Table>
<NewDataSet>
I want to convert it to the below structure. I am a newbie at xsl transforms and I am not sure how keep the <Table>
element from repeating for every record in the dataset.
<NewDataSet>
<Table>
<employee id="100">
<empname>Joe Smith</empname>
<phone>111-111-1111</phone>
<mobile>222-222-2222</mobile>
</employee>
<employee id="101">
<empname>Ann Jensen</empname>
<phone>111-111-0000</phone>
<mobile>222-222-0000</mobile>
</employee>
</Table>
<NewDataSet>
I tried using a combination of xsl:for-each and xsl:if statements to get what I wanted, but so far I have not been able to get it to work. Any assistance would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此转换:
应用于提供的 XML 文档时(已更正为格式正确):
产生所需的正确结果:
说明:
身份规则“按原样”复制每个节点。
第一个
Table
元素与覆盖模板匹配。这将创建结果中唯一的Table
并将模板应用于所有Table
元素的子元素。id
元素与覆盖模板相匹配,该模板将其转换为具有id
的employee
元素> 属性与id
元素的值。这还将employee
元素内部的模板应用于id
的所有其他同级元素,并且它们由身份模板按原样复制。This transformation:
when applied on the provided XML document (corrected to be well-formed):
produces the wanted, correct result:
Explanation:
The identity rule copies every node "as-is".
The first
Table
element is matched by an overriding template. This creates the onlyTable
in the result and applies templates to the children of allTable
elements.The
id
element is matched by an overriding template, that converts it to anemployee
element having anid
attribute with the value of theid
element. This also applies templates from inside theemployee
element to all other siblings ofid
and they are copied as-is by the identity template.这应该可以解决问题
this should do the trick