根据子节点对整个 xdocument 进行排序

发布于 2024-10-19 05:59:28 字数 1092 浏览 3 评论 0原文

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

谢谢

I have an xml of the following format:

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

I'd like to sort the list of customers based on their names, and return the document in the following format:

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

I am using c# and currently xmldocument.

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最初的梦 2024-10-26 05:59:28

你可以做这样的事情

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));
}

You could do something like this

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));
}
蓝眼睛不忧郁 2024-10-26 05:59:28

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

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

If you want to have a stylesheet and use it to transform the document then:

<?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>
方觉久 2024-10-26 05:59:28

此转换

<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 元素的嵌套级别如何,始终会生成正确的结果

This transformation:

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

when applied on the provided XML document:

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

produces the wanted, correct result:

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

Do note: The correct results will always be produced -- regardless of the level of nesting of the contactGrp elements

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文