基于子节点排序整个xdocument

时间:2011-02-27 21:26:27

标签: c# xml xslt xpath linq-to-xml

我有以下格式的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。

谢谢

3 个答案:

答案 0 :(得分:4)

你可以做这样的事情

var doc = XDocument.Load(/* ... */);

foreach (var g in doc.Descendants("contactGrp"))
{
    var customers = g.Elements("customer").ToList();
    customers.Remove();
    g.Add(customers.OrderBy(c => c.Attribute("name").Value));
}

答案 1 :(得分:2)

如果您想要一个样式表并使用它来转换文档,那么:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/contactGrp">
    <contactGrp name="Developers">
      <xsl:apply-templates select="contactGrp"/>
    </contactGrp>
  </xsl:template>

  <xsl:template match="contactGrp/contactGrp">
    <contactGrp>
      <xsl:attribute name="name">
        <xsl:value-of select="@name"/>
      </xsl:attribute>

      <xsl:for-each select="customer">
        <xsl:sort select="@name"/>
        <xsl:copy-of select="."/>
      </xsl:for-each>

    </contactGrp>
  </xsl:template>

</xsl:stylesheet>

答案 2 :(得分:2)

此转化

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="contactGrp">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*">
     <xsl:sort select="@name"/>
   </xsl:apply-templates>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档时:

<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>

会产生想要的正确结果:

<contactGrp name="People">
  <contactGrp name="Developers">
    <customer name="Brad" />
    <customer name="Mike" />
    <customer name="Smith" />
  </contactGrp>
  <contactGrp name="QA">
    <customer name="abi" />
    <customer name="John" />
  </contactGrp>
</contactGrp>

请注意:始终会生成正确的结果 - 无论contactGrp元素的嵌套级别如何