NSXMLDocument,带有命名空间的nodesForXPath

发布于 2024-09-25 07:42:59 字数 2957 浏览 2 评论 0原文

我想从 xml 文件中获取一组元素,但是一旦元素涉及命名空间,它就会失败。

这是 xml 文件的片段:

<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     version="1.0" creator="Groundspeak Pocket Query"
     xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.groundspeak.com/cache/1/0 http://www.groundspeak.com/cache/1/0/cache.xsd" 
     xmlns="http://www.topografix.com/GPX/1/0"> 
  <name>My Finds Pocket Query</name>   
  <desc>Geocache file generated by Groundspeak</desc>   
  <author>Groundspeak</author>   
  <email>[email protected]</email> 
  <time>2010-09-15T16:18:55.9846906Z</time> 
  <keywords>cache, geocache, groundspeak</keywords>   
  <bounds minlat="41.89687" minlon="5.561883" maxlat="70.669967" maxlon="25.74735" />    
  <wpt lat="62.244933" lon="25.74735">
    <time>2010-01-11T08:00:00Z</time>
    <name>GC22W1T</name>
    <desc>Kadonneet ja karanneet by ooti, Traditional Cache (1.5/2)</desc>
    <url>http://www.geocaching.com/seek/cache_details.aspx?guid=4af28fe9-401b-44df-b058-5fd5399fc083</url>
    <urlname>Kadonneet ja karanneet</urlname>
    <sym>Geocache Found</sym>
    <type>Geocache|Traditional Cache</type>
    <groundspeak:cache id="1521507" available="True" archived="False" xmlns:groundspeak="http://www.groundspeak.com/cache/1/0">
      <groundspeak:name>Kadonneet ja karanneet</groundspeak:name>
      <groundspeak:placed_by>ooti</groundspeak:placed_by>
      <groundspeak:owner id="816431">ooti</groundspeak:owner>
      <groundspeak:type>Traditional Cache</groundspeak:type>
      <groundspeak:container>Small</groundspeak:container>
      <groundspeak:difficulty>1.5</groundspeak:difficulty>
      <groundspeak:terrain>2</groundspeak:terrain>
      <groundspeak:country>Finland</groundspeak:country>
      <groundspeak:state>
      </groundspeak:state>
      <groundspeak:short_description html="True">
      </groundspeak:short_description>
      <groundspeak:encoded_hints>
      </groundspeak:encoded_hints>
      <groundspeak:travelbugs />
    </groundspeak:cache>   
  </wpt>
</gpx>

我想获取所有 grounspeak:cache 元素,但既不是 //groundspeak:cache 也不是 //cache code> 似乎返回任何内容。

NSArray *caches = [self.xml nodesForXPath:@"//cache" error:&error];

有什么线索吗?

编辑:有没有基于可可的软件,我可以在其中加载 xml 并测试不同的 xpath?我对 Objective-C 和 Cocoa 很陌生,所以最好检查一下我的 xpath 是否真的是错误的。

I want to get a set of elements from a xml-file, but as soon the the elements involve namespaces, it fails.

This is a fragment of the xml file:

<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     version="1.0" creator="Groundspeak Pocket Query"
     xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.groundspeak.com/cache/1/0 http://www.groundspeak.com/cache/1/0/cache.xsd" 
     xmlns="http://www.topografix.com/GPX/1/0"> 
  <name>My Finds Pocket Query</name>   
  <desc>Geocache file generated by Groundspeak</desc>   
  <author>Groundspeak</author>   
  <email>[email protected]</email> 
  <time>2010-09-15T16:18:55.9846906Z</time> 
  <keywords>cache, geocache, groundspeak</keywords>   
  <bounds minlat="41.89687" minlon="5.561883" maxlat="70.669967" maxlon="25.74735" />    
  <wpt lat="62.244933" lon="25.74735">
    <time>2010-01-11T08:00:00Z</time>
    <name>GC22W1T</name>
    <desc>Kadonneet ja karanneet by ooti, Traditional Cache (1.5/2)</desc>
    <url>http://www.geocaching.com/seek/cache_details.aspx?guid=4af28fe9-401b-44df-b058-5fd5399fc083</url>
    <urlname>Kadonneet ja karanneet</urlname>
    <sym>Geocache Found</sym>
    <type>Geocache|Traditional Cache</type>
    <groundspeak:cache id="1521507" available="True" archived="False" xmlns:groundspeak="http://www.groundspeak.com/cache/1/0">
      <groundspeak:name>Kadonneet ja karanneet</groundspeak:name>
      <groundspeak:placed_by>ooti</groundspeak:placed_by>
      <groundspeak:owner id="816431">ooti</groundspeak:owner>
      <groundspeak:type>Traditional Cache</groundspeak:type>
      <groundspeak:container>Small</groundspeak:container>
      <groundspeak:difficulty>1.5</groundspeak:difficulty>
      <groundspeak:terrain>2</groundspeak:terrain>
      <groundspeak:country>Finland</groundspeak:country>
      <groundspeak:state>
      </groundspeak:state>
      <groundspeak:short_description html="True">
      </groundspeak:short_description>
      <groundspeak:encoded_hints>
      </groundspeak:encoded_hints>
      <groundspeak:travelbugs />
    </groundspeak:cache>   
  </wpt>
</gpx>

I want to get all the grounspeak:cache elements, but neither //groundspeak:cache nor //cache seems to return anything.

NSArray *caches = [self.xml nodesForXPath:@"//cache" error:&error];

Any clue?

Edit: Are there any cocoa-based software out there, where I can load my xml and test different xpaths? I'm quite new to objective-c and cocoa, so it would be nice to check that it is really my xpath that is wrong..

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

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

发布评论

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

评论(3

脱离于你 2024-10-02 07:42:59

//cache 表示:无命名空间(或空命名空间)下的后代元素

您的 groundspeak:cache 元素位于命名空间 URI 下 http://www.groundspeak.com/cache/1/0

因此,如果您无法声明名称空间前缀绑定(我认为您不能使用 cocoa...),您可以使用以下 XPath 表达式:

//*[namespace-uri()='http://www.groundspeak.com/cache/1/0' and
    local-name()='cache']

如果您不想对名称空间如此严格...

//*[local-name()='cache']

<但是最后一个做法是不好的做法,因为您最终可能会选择错误的节点,而且在处理 XML 时,您的工具应该支持名称空间。

作为证明,这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
            <xsl:copy-of select="//*[namespace-uri() =
                                     'http://www.groundspeak.com/cache/1/0' and
                                     local-name() = 'cache']"/>
    </xsl:template>
</xsl:stylesheet>

输出:

<groundspeak:cache id="1521507" available="True" archived="False"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://www.topografix.com/GPX/1/0"
            xmlns:groundspeak="http://www.groundspeak.com/cache/1/0">
    <groundspeak:name>Kadonneet ja karanneet</groundspeak:name>
    <groundspeak:placed_by>ooti</groundspeak:placed_by>
    <groundspeak:owner id="816431">ooti</groundspeak:owner>
    <groundspeak:type>Traditional Cache</groundspeak:type>
    <groundspeak:container>Small</groundspeak:container>
    <groundspeak:difficulty>1.5</groundspeak:difficulty>
    <groundspeak:terrain>2</groundspeak:terrain>
    <groundspeak:country>Finland</groundspeak:country>
    <groundspeak:state></groundspeak:state>
    <groundspeak:short_description html="True"></groundspeak:short_description>
    <groundspeak:encoded_hints></groundspeak:encoded_hints>
    <groundspeak:travelbugs />
</groundspeak:cache>

This //cache means: a descendant element under no namespace (or empty namespace)

Your groundspeak:cache element is under a namespace URI http://www.groundspeak.com/cache/1/0.

So, if you can't declare a namespace-prefix binding (I think you can't with cocoa...), you could use this XPath expression:

//*[namespace-uri()='http://www.groundspeak.com/cache/1/0' and
    local-name()='cache']

If you don't want to be so strict about namespace...

//*[local-name()='cache']

But this last is a bad practice, because you could end up selecting wrong nodes, and because when dealing with XML, your tool should support namespaces.

As proof, this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
            <xsl:copy-of select="//*[namespace-uri() =
                                     'http://www.groundspeak.com/cache/1/0' and
                                     local-name() = 'cache']"/>
    </xsl:template>
</xsl:stylesheet>

Output:

<groundspeak:cache id="1521507" available="True" archived="False"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://www.topografix.com/GPX/1/0"
            xmlns:groundspeak="http://www.groundspeak.com/cache/1/0">
    <groundspeak:name>Kadonneet ja karanneet</groundspeak:name>
    <groundspeak:placed_by>ooti</groundspeak:placed_by>
    <groundspeak:owner id="816431">ooti</groundspeak:owner>
    <groundspeak:type>Traditional Cache</groundspeak:type>
    <groundspeak:container>Small</groundspeak:container>
    <groundspeak:difficulty>1.5</groundspeak:difficulty>
    <groundspeak:terrain>2</groundspeak:terrain>
    <groundspeak:country>Finland</groundspeak:country>
    <groundspeak:state></groundspeak:state>
    <groundspeak:short_description html="True"></groundspeak:short_description>
    <groundspeak:encoded_hints></groundspeak:encoded_hints>
    <groundspeak:travelbugs />
</groundspeak:cache>
何必那么矫情 2024-10-02 07:42:59

您需要向文档的根节点添加一个新的命名空间属性,定义一个在查询子级时可以使用的前缀:

NSXMLDocument *xmldoc = ...
NSXMLElement *namespace = [NSXMLElement namespaceWithName:@"mns" stringValue:@"http://mynamespaceurl.com/mynamespace"];
[xmldoc.rootElement addNamespace:namespace];

然后当您稍后查询内容时,可以使用该前缀来引用命名空间:

NSArray * caches = [xmldoc.rootElement nodesForXPath:@"//mns:caches" error:&error];

You need to add a new namespace attribute to the root node of your document, defining a prefix that you can use when querying the children:

NSXMLDocument *xmldoc = ...
NSXMLElement *namespace = [NSXMLElement namespaceWithName:@"mns" stringValue:@"http://mynamespaceurl.com/mynamespace"];
[xmldoc.rootElement addNamespace:namespace];

then when you query things later, you can use that prefix to refer to the namespace:

NSArray * caches = [xmldoc.rootElement nodesForXPath:@"//mns:caches" error:&error];
爺獨霸怡葒院 2024-10-02 07:42:59

//groundspeak:缓存应该可以工作。您可能还需要命名空间 uri 设置

//groundspeak:cache should work. You might need a namespace-uri setting as well

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