Loading ...
Sorry, an error occurred while loading the content.

dataof() use with index

Expand Messages
  • Martin Naskovski
    Hi - I have this code: $som- match( /Envelope/Body/[1] ); my @evtList; my $idx = 1; while ( defined $som- dataof( [$idx] ) ) { ... my $eventXML =
    Message 1 of 2 , Nov 5, 2004
    • 0 Attachment
      Hi - I have this code:

      $som->match('/Envelope/Body/[1]');
      my @evtList;

      my $idx = 1;
      while ( defined $som->dataof("[$idx]") ) {
      ...
      my $eventXML = $som->dataof("[$idx]/event");
      $event->set_eventName($eventXML->attr()->{eventName});
      $event->set_eventID($eventXML->attr()->{eventID});
      ...
      }

      Which basically takes MANY <regAPI> </regAPI>, elements in my XML stream,
      and the $idx is used to access each <regAPI> / </regAPI> pair as an array,
      by using 'dataof()' (or valueof() if you wish).

      The <regAPI> / <.regAPI> contains this:

      <regAPI>
      <event .... />
      </regAPI>

      My problem is, when I have a SOAP call which returns JUST ONE <regAPI> /
      </regAPI> , the above code does NOT work, so in order to get the 'event'
      part, I have to do:

      ->dataof('//event');

      everytime I have a SINGLE <regAPI> pair.

      Why cant I just process even a SINGLE element as a list through my while
      loop, with only a signle iteration through it, and return a LIST with ONE
      element in it?

      Am I missing something, or is this not possible wit hthe SOAP::Lite xpath
      language?

      Thanks.
    • Duncan Cameron
      ... There must be something wrong with the parts of the code that you have not shown. The following example works for 0, 1 or many regAPI elements, assuming
      Message 2 of 2 , Nov 6, 2004
      • 0 Attachment
        On 2004-11-06 at 00:26:35 Martin Naskovski wrote:

        >Hi - I have this code:
        >
        > $som->match('/Envelope/Body/[1]');
        > my @evtList;
        >
        > my $idx = 1;
        > while ( defined $som->dataof("[$idx]") ) {
        >...
        >my $eventXML = $som->dataof("[$idx]/event");
        >$event->set_eventName($eventXML->attr()->{eventName});
        >$event->set_eventID($eventXML->attr()->{eventID});
        >...
        > }
        >
        >Which basically takes MANY <regAPI> </regAPI>, elements in my XML
        >stream,
        >and the $idx is used to access each <regAPI> / </regAPI> pair as an
        >array,
        >by using 'dataof()' (or valueof() if you wish).
        >
        >The <regAPI> / <.regAPI> contains this:
        >
        ><regAPI>
        > <event .... />
        ></regAPI>
        >
        >My problem is, when I have a SOAP call which returns JUST ONE <regAPI>
        >/
        ></regAPI> , the above code does NOT work, so in order to get the
        >'event'
        >part, I have to do:
        >
        >->dataof('//event');
        >
        >everytime I have a SINGLE <regAPI> pair.
        >
        >Why cant I just process even a SINGLE element as a list through my
        >while
        >loop, with only a signle iteration through it, and return a LIST with
        >ONE
        >element in it?
        >
        >Am I missing something, or is this not possible wit hthe SOAP::Lite
        >xpath
        >language?
        There must be something wrong with the parts of the code that you have
        not shown. The following example works for 0, 1 or many regAPI elements,
        assuming that your xml is something like

        <Envelope>
        <Body>
        <a>
        <regAPI ID='regAPI 1'>
        <event eventName='event 1' eventID='1' />
        </regAPI>
        <regAPI ID='regAPI 2'>
        <event eventName='event 2' eventID='2' />
        </regAPI>
        <regAPI ID='regAPI 3'>
        <event eventName='event 3' eventID='3' />
        </regAPI>
        </a>
        </Body>
        </Envelope>

        $som->match('/Envelope/Body/[1]');
        my $idx = 1;
        while ( my $regAPI = $som->dataof("[$idx]") ) {
        my $eventXML = $som->dataof("[$idx]/event");
        print $regAPI->attr->{ID}, ' ', $eventXML->attr->{eventName}, "\n";
        $idx++;
        }
        If you don't need any information from the regAPI elements then you can
        simply get all the event elements

        my @event = $som->dataof('//event');

        Duncan
      Your message has been successfully submitted and would be delivered to recipients shortly.