4

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>

grounspeak:cacheすべての要素を取得したいのですが、どちら//groundspeak:cache//cache何も返さないようです。

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

どんな手掛かり?

編集:xmlをロードしてさまざまなxpathをテストできるココアベースのソフトウェアはありますか?私はobjective-cとcocoaにまったく慣れていないので、間違っているのは本当に私のxpathであることを確認するといいでしょう..

4

3 に答える 3

5

これは、名前空間のない (または空の名前空間) の下にある子孫要素を//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>
于 2010-09-23T22:33:36.160 に答える
1

ドキュメントのルート ノードに新しい名前空間属性を追加し、子をクエリするときに使用できるプレフィックスを定義する必要があります。

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];
于 2012-04-30T08:55:17.710 に答える
0

//groundspeak:キャッシュが機能するはずです。namespace-uri 設定も必要になる場合があります

于 2010-09-23T22:06:41.367 に答える