Confused about namespaces in Atom feed

ε祈祈猫儿з 提交于 2019-12-10 15:26:43

问题


Is there any difference between

<opensearch:totalResults>1000</opensearch:totalResults>

and

<totalResults xmlns="opensearch">1000</totalResults>

I'm using the SyndicationFeed class in .NET to generate an Atom feed, and I need to add some elements for the opensearch standard, but it keeps adding elements like the latter one above when I want it to add them like the former one.

The code:

feed.ElementExtensions.Add("totalResults", "opensearch", "2");

EDIT

The root feed tag looks like this

<feed xml:lang="en-US" p1:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:p1="xmlns" xmlns="http://www.w3.org/2005/Atom">

After changing my code as @Reddog suggested, the totalresults element looks like this

<totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/">1000</totalResults>

The code that adds the namespace to the feed tag looks like this

feed.AttributeExtensions.Add(
    new XmlQualifiedName("opensearch", "xmlns"),
    @"http://a9.com/-/spec/opensearch/1.1/");

And the code that adds the totalresults element now looks like this

feed.ElementExtensions.Add("totalResults", @"http://a9.com/-/spec/opensearch/1.1/", "1000");

回答1:


Nevermind. I realized that I was adding the namespace incorrectly. It should be

feed.AttributeExtensions.Add(
   new XmlQualifiedName("opensearch", "http://www.w3.org/2000/xmlns/"),
   "http://a9.com/-/spec/opensearch/1.1/");



回答2:


Namespaces

Default namespaces are inherited from the parent element. Or else, you can define new aliases for your children to use with the xmlns:alias= syntax or you can redefine the default namespace to use for an element (and of course it's children) using the xmlns= syntax.

You first example:

<opensearch:totalResults>1000</opensearch:totalResults>

Requires that the "opensearch" namespace alias be defined by a parent element - possibly in a different namespace. For example:

<myRoot xmlms:opensearch="http://a9.com/-/spec/opensearch/1.1/">
    <opensearch:totalResults>1000</opensearch:totalResults>
</myRoot>

Though this means that "myRoot" element is in a different namespace - namely, the default one (with a blank namespace or that defined by it's own parent).

Inserting

In order to actually add the element with the correct namespace, you'll need to use the namespace itself, rather than it's alias ("opensearch").

Therefore, to add your new element you'll need to either grab the namespace from the parent node (or else just know it and have it hard coded).

E.g.

feed.ElementExtensions.Add("totalResults", "http://a9.com/-/spec/opensearch/1.1/", 1000);

But note that you'll have limited or no control over the particular alias given to your namespace. In order to do this, you'll have to take some control over the XML serialization process...




回答3:


To be more complete.

Set specify namespace on channel element with:

feed.AttributeExtensions.Add(
  new XmlQualifiedName("opensearch", XNamespace.Xmlns.ToString()),
 "http://a9.com/-/spec/opensearch/1.1/");

And specify namespace on totalResults with:

feed.ElementExtensions.Add("totalResults", "http://a9.com/-/spec/opensearch/1.1/", "1000");

That will give you:

<opensearch:totalResults>1000</opensearch:totalResults>


来源:https://stackoverflow.com/questions/3160375/confused-about-namespaces-in-atom-feed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!