【问题标题】:consolidate xml namespaces整合 xml 命名空间
【发布时间】:2011-11-06 19:15:06
【问题描述】:

我有以下 xml。

命名空间非常分散,但 xml 本身是有效的。有什么方法可以配置编写器以在顶部输出所有相关名称空间的 xml,以及 xml 清洁器?我的 xml 阅读器/编写器组合似乎忠实地再现了这一点。我宁愿合并所有的命名空间。

如果可能的话,我真的不想不使用 xslt。

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:a="http://www.w3.org/2005/Atom/extensions/property" a:length="1">
    <title type="text">Search Results: '80706'</title>
    <subtitle type="text">search results</subtitle>
    <id>80706</id>
    <updated>2011-08-30T09:05:21+02:00</updated>
    <author>
        <name>XXXXXX</name>
        <uri>contact@xxxx</uri>
        <email>contact@xxxx.com</email>
    </author>
    <entry">
        <id>8000</id>
        <title type="text">Investment 0000000</title>
        <summary type="text">pre populated form</summary>
        <published>2011-08-30T14:57:45Z</published>
        <updated>2011-08-30T09:05:21+02:00</updated>
        <content type="application/xml">
            <prop:query-result xmlns:prop="http://www.w3.org/2005/Atom/extensions/property" xmlns="http://www.w3.org/2005/Atom/extensions/property">
                <prop:PartyId>000000</prop:PartyId>
                <prop:IDReferenceNumber>000000</prop:IDReferenceNumber>
                <prop:FullName></prop:FullName>
                <prop:FirstName>FIRST</prop:FirstName>
                <prop:LastName>LAST</prop:LastName>
                <prop:Title>Mr</prop:Title>
                <prop:BirthDate>1967-05-24T00:00:00</prop:BirthDate>
                <prop:PartySystemKey source="Number">000000</prop:PartySystemKey>
                <prop:Address type="Mailing">BOX ...</prop:Address>
                <prop:Address type="Home">39 ...</prop:Address>
                <prop:ContactNumber>0000000</prop:ContactNumber>
                <prop:InvestmentNumber source="ContractNumber">0000000</prop:InvestmentNumber>
                <prop:InvestmentNumber source="AccountNumber">0000000</prop:InvestmentNumber>
            </prop:query-result>
        </content>
    </entry>
</feed>

【问题讨论】:

  • 何必呢?命名空间的放置位置无关紧要,只要它们在使用之前出现即可。
  • 完美,真的没有别的好理由。

标签: c# xslt namespaces


【解决方案1】:

好吧,XmlReader 只能向前工作,因此使用 XmlReader/Writer 组合在任何地方找到已使用的命名空间以将它们放在根目录上有点困难。 但是,当我运行代码 (.NET 4.0) 时,您可以尝试 LINQ to XML

    XDocument feed = XDocument.Load(@"input.xml");
    feed.Root.Descendants().Attributes().Where(a => a.IsNamespaceDeclaration && feed.Root.Attributes().Any(a2 => a.Value == a2.Value)).Remove();
    feed.Save(Console.Out, SaveOptions.OmitDuplicateNamespaces);

反对样本输入

<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:a="http://www.w3.org/2005/Atom/extensions/property" a:length="1">
  <title type="text">Search Results: '80706'</title>
  <subtitle type="text">search results</subtitle>
  <id>80706</id>
  <updated>2011-08-30T09:05:21+02:00</updated>
  <author>
    <name>XXXXXX</name>
    <uri>contact@xxxx</uri>
    <email>contact@xxxx.com</email>
  </author>
  <entry>
    <id>8000</id>
    <title type="text">Investment 0000000</title>
    <summary type="text">pre populated form</summary>
    <published>2011-08-30T14:57:45Z</published>
    <updated>2011-08-30T09:05:21+02:00</updated>
    <content type="application/xml">
      <prop:query-result xmlns:prop="http://www.w3.org/2005/Atom/extensions/property" xmlns="http://www.w3.org/2005/Atom/extensions/property">
        <prop:PartyId>000000</prop:PartyId>
        <prop:IDReferenceNumber>000000</prop:IDReferenceNumber>
        <prop:FullName></prop:FullName>
        <prop:FirstName>FIRST</prop:FirstName>
        <prop:LastName>LAST</prop:LastName>
        <prop:Title>Mr</prop:Title>
        <prop:BirthDate>1967-05-24T00:00:00</prop:BirthDate>
        <prop:PartySystemKey source="Number">000000</prop:PartySystemKey>
        <prop:Address type="Mailing">BOX ...</prop:Address>
        <prop:Address type="Home">39 ...</prop:Address>
        <prop:ContactNumber>0000000</prop:ContactNumber>
        <prop:InvestmentNumber source="ContractNumber">0000000</prop:InvestmentNumber>
        <prop:InvestmentNumber source="AccountNumber">0000000</prop:InvestmentNumber>
      </prop:query-result>
    </content>
  </entry>
</feed>

我得到了输出

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:a="http://www.w3.org/2005/Atom/extensions/property" a:length="1">
  <title type="text">Search Results: '80706'</title>
  <subtitle type="text">search results</subtitle>
  <id>80706</id>
  <updated>2011-08-30T09:05:21+02:00</updated>
  <author>
    <name>XXXXXX</name>
    <uri>contact@xxxx</uri>
    <email>contact@xxxx.com</email>
  </author>
  <entry>
    <id>8000</id>
    <title type="text">Investment 0000000</title>
    <summary type="text">pre populated form</summary>
    <published>2011-08-30T14:57:45Z</published>
    <updated>2011-08-30T09:05:21+02:00</updated>
    <content type="application/xml">
      <a:query-result>
        <a:PartyId>000000</a:PartyId>
        <a:IDReferenceNumber>000000</a:IDReferenceNumber>
        <a:FullName></a:FullName>
        <a:FirstName>FIRST</a:FirstName>
        <a:LastName>LAST</a:LastName>
        <a:Title>Mr</a:Title>
        <a:BirthDate>1967-05-24T00:00:00</a:BirthDate>
        <a:PartySystemKey source="Number">000000</a:PartySystemKey>
        <a:Address type="Mailing">BOX ...</a:Address>
        <a:Address type="Home">39 ...</a:Address>
        <a:ContactNumber>0000000</a:ContactNumber>
        <a:InvestmentNumber source="ContractNumber">0000000</a:InvestmentNumber>

        <a:InvestmentNumber source="AccountNumber">0000000</a:InvestmentNumber>
      </a:query-result>
    </content>
  </entry>
</feed>

够了吗?你还没有说你想要什么样的结果,以及是否存在输入变体,其中名称空间被使用到一些之前没有声明的需要提升的级别。使用您当前的示例,只需删除重复的命名空间声明就足够了。

【讨论】:

    猜你喜欢
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多