Topic
2 replies Latest Post - ‏2013-03-25T02:15:15Z by SystemAdmin
SystemAdmin
SystemAdmin
1245 Posts
ACCEPTED ANSWER

Pinned topic Streams 3, convertToXML() and xquery() ..

‏2013-02-27T02:05:20Z |
Hello Experts ..

Re;, Streams 3 and XML ..

(n) questions please ..

. Does someone have an end to end example using xquery ?

I understand its a function, and I see tons of XQuery doc from
other environments, but I am struggling seeing how to implement.

In particular none of my xqueryExpressions seem to work, but I
still am requesting a whole example.

. convertToXML only casts data types, correct, Ie rstring to xml ?

. Is there a means to take, for example, a csv, and xml format it ?

I'm pretty certain no, and my next thought is to use XSLT. Does
someone have an example of that ?
Thanks much in advance.

Daniel
  • hnasgaard
    hnasgaard
    200 Posts
    ACCEPTED ANSWER

    Re: Streams 3, convertToXML() and xquery() ..

    ‏2013-02-27T12:59:21Z  in response to SystemAdmin
    Hi Daniel,

    Here's a partial example of the use of xquery.
    
    type T = tuple<int32 id, tuple<rstring b, list<int32> x, float64 d> a, rstring c>; stream<T> OutTuples = Custom (Data) 
    { logic onTuple Data: 
    { 
    // extract string ‘c’ mutable list<rstring> results = xquery(Data.xmlVar, “/something/bar/c/text()”); mutable rstring s = results[0]; 
    // extract string ‘b’ attribute in ‘a’ mutable tuple<rstring b, list<int32> x, float64 d> a = 
    {
    }; results = xquery(Data.xmlVar, “/something/bar/a/@bdata”); a.b = results[0]; 
    // extract list<int32> ‘x’ attribute in ‘a’ results = xquery(Data.xmlVar, “/something/bar/foo/text()”); 
    
    for (rstring r in results) appendM (a.x, (int32) r); 
    // extract float64 ‘d’ attribute in ‘a’ results = xquery(Data.xmlVar, “/something/bar/a/d/text()”); a.d = (float64) results[0]; 
    // submit the final result submit (
    {id = Data.id, a = a, c = s
    }, OutTuples); 
    }
    


    convertToXML can be used in place of a cast if you want to ensure that a conversion failure does not cause your operator to terminate.

    You can convert an arbitrary tuple to XML via a cast. The xml that is created conforms to the Serialized Tuple Model format, the schema for which is in $STREAMS_INSTALL/etc/xsd/SPL/serializedTupleModel.xsd
    So you'd have to read the csv file into a tuple using a FileSource, then cast the resulting tuple to a var of type xml and write that out. From there you could use XSLT to transform it, but Streams doesn't provide any API to XSLT. You could write a native function to wrap the call to XSLT perhaps.
    • SystemAdmin
      SystemAdmin
      1245 Posts
      ACCEPTED ANSWER

      Re: Streams 3, convertToXML() and xquery() ..

      ‏2013-03-25T02:15:15Z  in response to hnasgaard
      Hi Howard ..
      Thanks. You're the best !!
      ----Cut File Here-------------------------------------
      stream <MyItem> MyCustom as MyOut =
      Custom (MyBeacon as MyIn) {
      logic state :
      {
      mutable MyItem l_MyItem ;
      //
      mutable rstring MyStr =
      "<?xml version='1.0' encoding='UTF-8'?>
      <RegisterOrders RegID='Register-100'>
      <ATransaction TxID='101' TxType='sale' >
      <ItemName count='2'>Helmet </ItemName>
      <ItemName count='4'>Cap </ItemName>
      </ATransaction>
      <ATransaction TxID='102' TxType='sale' >
      <ItemName count='1'>Jersey </ItemName>
      </ATransaction>
      <ATransaction TxID='103' TxType='return'>
      <ItemName count='3'>Swim Fin </ItemName>
      <ItemName count='6'>Swim Mask </ItemName>
      </ATransaction>
      <ATransaction TxID='104' TxType='sale' >
      <ItemName count='5'>Football </ItemName>
      <ItemName count='7'>Baseball </ItemName>
      <ItemName count='9'>Frisbee </ItemName>
      </ATransaction>
      </RegisterOrders>";
      mutable xml MyXML ;
      //
      mutable list<rstring> MyResult ;
      mutable int32 MyCntr = 0;
      mutable int32 MySizeof ;
      }
      onTuple MyIn :
      {
      convertToXML(MyXML,MyStr);
      MyResult = xquery(MyXML,
      "/RegisterOrders/ATransaction/ItemName/text()");
      MySizeof = size(MyResult);
      //
      while (MyCntr < MySizeof)
      {
      l_MyItem.ItemName = MyResultMyCntr;
      submit ( l_MyItem, MyOut );
      //
      MyCntr++;
      }
      }
      }