对 Atom feed 中的命名空间感到困惑
有什么区别吗?
<opensearch:totalResults>1000</opensearch:totalResults>
之间
<totalResults xmlns="opensearch">1000</totalResults>
我在 .NET 中使用 SyndicateFeed 类来生成 Atom 提要,并且我需要为 opensearch 标准添加一些元素,但当我希望它添加时,它会不断添加类似于上面后一个元素的元素,这两者 他们喜欢前一个。
代码:
feed.ElementExtensions.Add("totalResults", "opensearch", "2");
编辑
根提要标签如下所示
<feed xml:lang="en-US" p1:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:p1="xmlns" xmlns="http://www.w3.org/2005/Atom">
在按照 @Reddog 建议更改我的代码后,totalresults 元素如下所示
<totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/">1000</totalResults>
将命名空间添加到提要标签的代码如下所示
feed.AttributeExtensions.Add(
new XmlQualifiedName("opensearch", "xmlns"),
@"http://a9.com/-/spec/opensearch/1.1/");
代码如下添加totalresults元素现在看起来像这样
feed.ElementExtensions.Add("totalResults", @"http://a9.com/-/spec/opensearch/1.1/", "1000");
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");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没关系。我意识到我错误地添加了命名空间。应该是
Nevermind. I realized that I was adding the namespace incorrectly. It should be
命名空间
默认命名空间是从父元素继承的。或者,您可以使用
xmlns:alias=
语法为子元素定义新的别名,或者可以使用重新定义用于元素(当然还有子元素)的默认命名空间>xmlns=
语法。第一个示例:
要求“opensearch”命名空间别名由父元素定义 - 可能在不同的命名空间中。例如:
尽管这意味着“myRoot”元素位于不同的名称空间中 - 即默认名称空间(具有空白名称空间或由其自己的父级定义的名称空间)。
插入
为了实际添加具有正确命名空间的元素,您需要使用命名空间本身,而不是它的别名(“opensearch”)。
因此,要添加新元素,您需要从父节点获取命名空间(或者只知道它并对其进行硬编码)。
例如,
但请注意,您对命名空间的特定别名的控制能力有限或无法控制。为了做到这一点,您必须对 XML 序列化过程进行一些控制......
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 thexmlns=
syntax.You first example:
Requires that the "opensearch" namespace alias be defined by a parent element - possibly in a different namespace. For example:
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.
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...
为了更完整。
设置在通道元素上指定命名空间:
并在 TotalResults 上指定命名空间:
这将为您提供:
To be more complete.
Set specify namespace on channel element with:
And specify namespace on totalResults with:
That will give you: