nodesForXPath namespace mapping

434 views
Skip to first unread message

damian.oneill

unread,
Nov 24, 2010, 10:22:22 AM11/24/10
to KissXML
Hi Guys first post, apologies if this is really obvious, I looked at
the four posts that are available in the forum on this topic but I'm
not sure from these if there is a defacto solution.

First the xmlString that I am trying to parse.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><network-
element-list xmlns="http://btisystems.com/pronx/ems/schemas/
network"><network-element management-domain-name="Default" schema-
name="net_snmp" sys-name="vvx6" sys-location="&quot;proNX Host&quot;"
sys-description="Linux vvx6 2.6.35.6-45.fc14.x86_64 #1 SMP Mon Oct 18
23:57:44 UTC 2010 x86_64" sys-contact="in...@btisystems.com"
isReachable="true" time-discovered="2010-11-23T15:48:02.000Z"
version="1.28.1" class="NET-SNMP" type="WideCastOS"
address="172.27.5.106"><meta-data-list><meta-data value="" key="Postal
Code"/></meta-data-list></network-element><network-element management-
domain-name="Default" schema-name="bti704_v102" sys-name="BTI-704" sys-
location="Ottawa, Canada" sys-description="BTI Platform" sys-
contact="BTI Systems." isReachable="true" time-
discovered="2010-11-23T16:19:01.000Z" version=" 1.2" class="BTI-
DEVICE" type="BTI-704" address="172.27.7.101"><meta-data-list><meta-
data value="rt" key="Zayo"/><meta-data value="" key="Postal Code"/></
meta-data-list></network-element><network-element management-domain-
name="Default" schema-name="bti712_v102" sys-name="BTI-712" sys-
location="Ottawa, Canada" sys-description="BTI Platform" sys-
contact="BTI Systems." isReachable="true" time-
discovered="2010-11-23T16:19:02.000Z" version=" 1.2" class="BTI-
DEVICE" type="BTI-712" address="172.27.7.103"><meta-data-list><meta-
data value="" key="Postal Code"/></meta-data-list></network-
element><network-element management-domain-name="Default" schema-
name="bti704_v102" sys-name="BTI-704" sys-location="Ottawa, Canada"
sys-description="BTI Platform" sys-contact="BTI Systems."
isReachable="true" time-discovered="2010-11-23T16:19:03.000Z"
version=" 1.2" class="BTI-DEVICE" type="BTI-704"
address="172.27.7.104"><meta-data-list><meta-data value="" key="Postal
Code"/></meta-data-list></network-element><network-element management-
domain-name="Default" schema-name="bti7000_811" isReachable="true"
time-discovered="2010-11-24T14:00:37.000Z" version="8.1.1 R013"
class="BTI-DEVICE" type="BTI-7000" address="172.27.7.106"><meta-data-
list><meta-data value="" key="Postal Code"/></meta-data-list></network-
element><network-element management-domain-name="Default" schema-
name="bti7000_811" isReachable="true" time-
discovered="2010-11-24T08:42:27.000Z" version="8.1.1 C001" class="BTI-
DEVICE" type="BTI-7000" address="172.27.7.110"><meta-data-list><meta-
data value="" key="Postal Code"/></meta-data-list></network-element></
network-element-list>

As you can see the root element has a default namespace.

<network-element-list xmlns="http://btisystems.com/pronx/ems/schemas/
network">

Running the following command returns an array length of zero.

DDXMLDocument *ddDoc = [[DDXMLDocument alloc]
initWithXMLString:xmlString options:0 error:nil];
NSArray *elementNodes = [ddDoc nodesForXPath:@"//network:network-
element-list/network-element" error:&err];

Can someone tell me how I should supply the namespace to the
nodesForXPath so that I will get back a NSArray with 6 elements.

Thanks in advance,
Damian.




Loren Cahlander

unread,
May 10, 2012, 11:28:24 AM5/10/12
to kis...@googlegroups.com
I am having a similar problem.

Here is a sample of the data that returns from the webservice call:

<?xml version="1.0" encoding="utf-8"?>
<IndividualMAPPResults xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xml.assessment.com/Service/MAPPResults">
   
<MAPP_Member>
       
<MemberID>1234567890</MemberID>
       
<FirstName>John</FirstName>
       
<MiddleInitial/>
       
<LastName>Doe</LastName>
       
<Email>john...@example.com</Email>
       
<ErrorCode>0</ErrorCode>
   
</MAPP_Member>
</IndividualMAPPResults>







Here is my code:

    NSLog(@"DONE. Received Bytes: %d", [self.webdata length]);

    NSString *theXML = [[NSString alloc]

                        initWithBytes: [self.webdata mutableBytes]

                        length:[self.webdata length]

                        encoding:NSUTF8StringEncoding];

    //---shows the XML---

    NSLog(@"%@", theXML);

    NSError *err = nil;

    DDXMLDocument *document = [[DDXMLDocument alloc] initWithXMLString:theXML options:0 error:&err];

    DDXMLElement *rootElement = [document rootElement];

    NSArray *docs=[rootElement nodesForXPath:@"//FirstName" error:&err];

    NSLog(@"Doc Count %i",[docs count]);

    

    for (DDXMLElement *documents in docs)

    {

        NSLog(@"First Name: %@", [documents stringValue]);

    }


I should get back an array of 1 element and it should contain "John"


Thank you,

Loren


Loren Cahlander

unread,
May 10, 2012, 3:25:19 PM5/10/12
to kis...@googlegroups.com
I found a workaround.  If I remove the string "xmlns=\"http://xml.assessment.com/Service/MAPPResults\""

    theXML = [theXML stringByReplacingOccurrencesOfString:@"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" withString:@""];

    theXML = [theXML stringByReplacingOccurrencesOfString:@"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" withString:@""];

    theXML = [theXML stringByReplacingOccurrencesOfString:@"xmlns=\"http://xml.assessment.com/Service/MAPPResults\"" withString:@""];

Then the following call works:

    NSArray *docs=[rootElement nodesForXPath:@"//FirstName" error:&err];

KissXML does not like default namespaces.  This is contrary to the other XPath processors that I use.  I consider this a bug.


Cheers,
Loren
Reply all
Reply to author
Forward
0 new messages