【问题标题】:sorting entire xdocument based on subnodes基于子节点对整个 xdocument 进行排序
【发布时间】: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


【解决方案1】:

你可以这样做

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

【讨论】:

    【解决方案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>
    

    【讨论】:

    • 谢谢。我知道 xslt 是正确的方法,但无法让 xml 对正确的级别进行排序。谢谢
    【解决方案3】:

    这种转变:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 2010-09-25
      • 1970-01-01
      • 2019-07-14
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多