Clarity PPM1

Expand all | Collapse all

Substring in GEL

Jump to Best Answer
  • 1.  Substring in GEL

    Posted 01-21-2015 02:32 PM

    I am parsing an XML file and loading it to clarity. I would like to substring a date field that comes in. This is easy enough to do via SQL, but I would like to catch it in the load.

     

    Does anyone have an example of that?

     

    Thanks.



  • 2.  Re: Substring in GEL

    Posted 01-21-2015 04:37 PM

    If you know that your parsed date field is a string, you can just use substring method.

    <?xml version="1.0" encoding="utf-8"?>
    <gel:script xmlns:core="jelly:core"
        xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       
        <core:set value="2015-01-28" var="testOne" />
        <gel:out>${testOne}</gel:out>
        <!-- public String substring(int beginIndex, int endIndex) -->
        <gel:out>${testOne.substring(5,7)}</gel:out>
        </gel:script>
    

     

     

    If it is a date object, you can use the toString() method and then substring to pull off the value you need.

     

    V/r,

    Gene



  • 3.  Re: Substring in GEL

    Posted 01-21-2015 04:57 PM

    The field I am pulling in is set like this:

     

    <gel:set asString="true" select="$gelXmlData/ReceivedDate/text()" var="ReceivedDate"/>

     

    So I would take and substring it after? I tried:

    <gel:set asString="true" select="$ReceivedDate.substring(0,10)" var="ssReceivedDate"/>

     

    Getting an error on that though.



  • 4.  Re: Substring in GEL
    Best Answer

    Posted 01-21-2015 05:03 PM

    Try:

    <core:set var="ssReceivedDate" value="${ReceivedDate.substring(0,10)}" />
    


  • 5.  Re: Substring in GEL

    Posted 01-21-2015 05:06 PM

    Did that and got:

     

    You must define an attribute called 'select' for this tag, which is the same error I got when I tried to do the substring using gel:set and had the select set to the original field variable.



  • 6.  Re: Substring in GEL

    Posted 01-21-2015 05:10 PM

    with core:set instead of gel:set? Core doesn't have a select attribute I don't think.

    Is core declared at the top of the file (ie xmlns:core="jelly:core")



  • 7.  Re: Substring in GEL

    Posted 01-21-2015 05:12 PM

    Yeah. I tried both. Cancelled the process and restarted just to be sure. I was surprised as well because as you said, core:set doesn't have a select. I'll give it one more go, but neither permutation seemed to work.



  • 8.  Re: Substring in GEL

    Posted 01-21-2015 05:14 PM

    You can give this a try, dont think I've ever tried this before so not sure it'll work but I'm somewhat curious:

    <gel:set asString="true" select="$gelXmlData/ReceivedDate/text().substring(0,10)" var="ReceivedDate"/>


  • 9.  Re: Substring in GEL

    Posted 01-21-2015 05:19 PM

    Same error.



  • 10.  Re: Substring in GEL

    Posted 01-21-2015 05:28 PM

    I would suggest that you select your node and then dump your context to see what the underlying object is for ReceivedDate

     

    <gel:set asString="true" select="$gelXmlData/ReceivedDate/text()" var="ReceivedDate"/>
    
    
    <gel:log>Dump Context Variables</gel:log>  
    <core:set var="entries" value="${context.getVariables().entrySet().toArray()}" />      
    <core:forEach var="entry" items="${entries}">  
    <core:if test="${!entry.getKey().equalsIgnoreCase('systemScope')}" >  
        <gel:log> ${entry.getKey()} = ${entry.getValue()} |  ${entry.getValue().getClass().getName()} </gel:log>  
    </core:if>  
    </core:forEach>  
    

     

     

    Once you know the object type, you should be able to get in into a string format that you can use substring with.

     

    V/r,

    Gene



  • 11.  Re: Substring in GEL

    Posted 01-21-2015 06:00 PM

    I believe that select="$gelXmlData/ReceivedDate/text() is still returning a node of type text.  The asString attribute is pulling the text out of the node and saving it in a string ReceivedDate.

     

    <gel:set asString="true" select="$gelXmlData/ReceivedDate/text().substring(0,10)" var="ReceivedDate"/> 
    

     

    So this tosses a "You must define an attribute called 'select' for this tag," error?

     

    Here is a small test with a parsed xml var.

    <?xml version="1.0" encoding="utf-8"?>
    <gel:script
       xmlns:core="jelly:core"
       xmlns:x="jelly:org.apache.commons.jelly.tags.xml.XMLTagLibrary"
       xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
       xmlns:soap="jelly:com.niku.union.gel.SOAPTagLibrary"
       xmlns:nikuq="http://www.niku.com/xog/Query"
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
       <gel:parse var="xogReadProjects">
          <Envelope>
             <Body>
                <ReadProject>
                   <NikuDataBus>
                        This is a test
                   </NikuDataBus>
                </ReadProject>
             </Body>
          </Envelope>
       </gel:parse>
    
        <gel:set asString="true" select="$xogReadProjects/Envelope/Body/ReadProject/NikuDataBus/text()" var="ReceivedDate"/>
        <gel:log>ReceivedDate = ${ReceivedDate}</gel:log>
        <core:set var="ssReceivedDate" value="${ReceivedDate.substring(0,10)}" />  
        <gel:log>ssReceivedDate = ${ssReceivedDate}</gel:log>
    
        <gel:log>Dump Context Variables</gel:log>    
        <core:set var="entries" value="${context.getVariables().entrySet().toArray()}" />        
        <core:forEach var="entry" items="${entries}">    
            <core:if test="${!entry.getKey().equalsIgnoreCase('systemScope')}" >    
                <gel:log> ${entry.getKey()} = ${entry.getValue()} |  ${entry.getValue().getClass().getName()} </gel:log>    
            </core:if>    
        </core:forEach>  
    
    </gel:script>
    

     

     

     

    V/r,

    Gene



  • 12.  Re: Substring in GEL

    Posted 01-21-2015 06:17 PM

    Gene-

     

    I would assume the same thing as you set it to asString. And even if it was still interpreting it as a date field upon read in the gel:set, it would seem to certainly be a string if the substring occurred in a subsequent gel:set or core:set statement using the variable...unless of course the value I used didn't read properly. But I've tried both core and gel with no luck.

     

    I will give the above a try first thing tomorrow AM. I could just go in and do something like

     

    <sql:query var="ssd">

    select

    substr(${ReceivedDate},0,10)

    from

    dual

    <sql:query>

    <core:set...

     

    But I'd like to see if I can get it to work within the gel:set.

     

    Thanks for the help so far, everyone!



  • 13.  Re: Substring in GEL

    Posted 01-22-2015 11:29 AM

    Some times you just need another day.

     

    This works:

     

    <gel:set asString="true" select="$gelXmlData/ReceivedDate/text()" var="ReceivedDate"/>

    <core:set var="ssReceivedDate" value="${ReceivedDate.substring(0,10)}"/>


    Must've been the end of the day lapses.


    Thanks for the help, gcubed L.Elias