【发布时间】:2011-02-27 21:26:26
【问题描述】:
我有一个如下格式的 xml:
<?xml version="1.0" encoding="utf-8"?>
<contactGrp name="People">
<contactGrp name="Developers">
<customer name="Mike" ></customer>
<customer name="Brad" ></customer>
<customer name="Smith" ></customer>
</contactGrp>
<contactGrp name="QA">
<customer name="John" ></customer>
<customer name="abi" ></customer>
</contactGrp>
</contactGrp>
我想根据客户的姓名对客户列表进行排序,并以以下格式返回文档:
<?xml version="1.0" encoding="utf-8"?>
<contactGrp name="People">
<contactGrp name="Developers">
<customer name="Brad" ></customer>
<customer name="Mike" ></customer>
<customer name="Smith" ></customer>
</contactGrp>
<contactGrp name="QA">
<customer name="abi" ></customer>
<customer name="John" ></customer>
</contactGrp>
</contactGrp>
我正在使用 c#,目前是 xmldocument。
谢谢
【问题讨论】:
-
我很困惑,因为
xpath标记... 如果您想选择一个节点集,那么 XPath 可能是正确的技术。如果要正确排序节点集,则需要 XPath 引擎宿主语言。但是如果要转换 XML 树,标准资源是 XSLT。 -
我知道,我似乎无法解决我的问题。
-
好问题,+1。请参阅我的答案,了解适用于
contactGrp元素的任何嵌套级别的完整、简短且简单的 XSLT 解决方案。 :)
标签: c# xml xslt xpath linq-to-xml