Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa NSXMLDocument,带有名称空间的NodeForXPath_Cocoa_Xpath_Namespaces_Nsxmldocument - Fatal编程技术网

Cocoa NSXMLDocument,带有名称空间的NodeForXPath

Cocoa NSXMLDocument,带有名称空间的NodeForXPath,cocoa,xpath,namespaces,nsxmldocument,Cocoa,Xpath,Namespaces,Nsxmldocument,我想从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://

我想从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>contact@groundspeak.com</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>
有线索吗


编辑:有没有基于cocoa的软件,我可以在那里加载xml并测试不同的XPath?我对objective-c和cocoa很陌生,所以最好检查一下xpath是否真的错了。

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

这个
//缓存
意味着:没有名称空间(或空名称空间)下的子元素

您的
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>

卡多内特酒店
奥蒂
奥蒂
传统缓存
小的
1.5
2.
芬兰

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

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];

那么如何指定名称空间uri设置呢?它们似乎都不返回任何元素:-/@Vegar:真奇怪!这两种方法都适用于作为输入的文档。它是在样式表转换中工作,还是作为nodesForXPath::方法的输入工作?它与样式表一起工作。所以,它应该与XPath引擎一起工作,除非它不是标准的。不知道为什么一开始我会有麻烦。谢谢
NSArray * caches = [xmldoc.rootElement nodesForXPath:@"//mns:caches" error:&error];