Clarity

  • 1.  How do I Insert an Attribute?

    Posted Feb 10, 2014 01:21 AM


    Background:
    I'm trying to write a gel script which xogs out a project, performs manipulations and additions, and xogs it back in.

    What's working:
    Xogging out
    Manipulating attributes which do exist
    Xogging back in.

    What's not working:
    Adding attributes to the 'Project' element which don't exist in the xog-out, but which do exist in "prj_projects_write.xml", such as entityCode, and billExpenseType, etcetera.

    I've tried the following, and I just know I'm doing something stoopid, but I don't know what!!! (And I don't get xsl/xslt enough, yet). Can I actually insert an attribute to the Project in this way?

    In each case, the error I get is 'org.apache.commons.jelly.JellyTagException: null:260:86: <gel:set> Missing or invalid XML'

    Here's a cut down version of my script, with the trouble hitting on GEL line trying to set @billExpenseType to value 'CAP_EXPENSE'.

    What is the correct syntax I should be using?

         <gel:forEach select="$runresult//Projects/Project" var="v_item">
          <core:if test="${DEBUG>1}">
           <gel:set asString="true" select="$v_item/@projectID" var="v_xog_project_id"/>
           <gel:log category="XOG" level="INFO">Project:${v_xog_project_id}</gel:log>
          </core:if>
          
          <!-- set those which should already be there -->
          <gel:set select="$v_item/@financialStatus" value="O"/>

          <!-- this gel:set causes the exception -->
          <core:catch var="gel_error">
           <gel:set value="CAP_EXPENSE" select="$v_item/@billExpenseType" insert="true"/>
          </core:catch>
          <core:if test="${gel_error != null}">
           <gel:log category="GEL" level="WARN">Gel Error: ${gel_error}</gel:log>
          </core:if>

    On the offending gel:set line, I've tried
    <gel:set value="CAP_EXPENSE" select="$v_item/@billExpenseType" insert="true"/>
    as well as without the @
    <gel:set value="CAP_EXPENSE" select="$v_item/billExpenseType" insert="true"/>

    but both give me the same exception...



  • 2.  RE: How do I Insert an Attribute?
    Best Answer

    Posted Feb 10, 2014 01:31 PM

    To add an attribute just call the setAttribute method on each of your project nodes.

     

    <core:expr value="${v_item.setAttribute('billExpenseType', 'CAP_EXPENSE')}" />

     

    Here is my test gel script:

    <?xml version="1.0" encoding="utf-8"?>
    <gel:script
        xmlns:core="jelly:core"
        xmlns:xog="http://www.niku.com/xog"
        xmlns:jsl="jelly:jsl"
        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:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
        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:out>Starting GetProject List</gel:out>
    
        <!-- Process SQL and XML section -->
        <gel:parse var="runresult">
            <Projects>
                <Project active="false" materialExchageRateType="AVERAGE" name="Engineering" projectID="Eng1">
                    <Allocations />
                    <scenarioDependencies />
                    <InvestmentAssociations>
                        <Allocations>
                            <ParentInvestment InvestmentID="PRJ00000003" InvestmentType="project" defaultAllocationPercent="1">
                                <CustomInformation>
                                    <ColumnValue name="partition_code" />
                                </CustomInformation>
                            </ParentInvestment>
                        </Allocations>
                        <Hierarchies />
                    </InvestmentAssociations>
                    <General addedBy="admin" addedDate="2011-03-30" />
                    <OBSAssocs complete="false">
                        <OBSAssoc id="Pprojects" name="Department OBS" unitPath="/MS" />
                    </OBSAssocs>
                </Project>
            </Projects>
        </gel:parse>
    
        <gel:forEach select="$runresult/Projects/Project" var="v_item">
            <gel:out>${v_item.getAttribute('name')}</gel:out>
    
            <gel:set asString="true" select="$v_item/@projectID" var="v_xog_project_id" />
            <gel:log category="XOG" level="INFO">Project:${v_xog_project_id}</gel:log>
    
            <!-- This seem to only work if the attribute exits 
            <gel:set value="CAP_EXPENSE" select="$v_item/@billExpenseType" insert="true" />
            -->
    
            <!-- This calls the setAttribute method on the project node -->
            <core:expr value="${v_item.setAttribute('billExpenseType', 'CAP_EXPENSE')}" />
        </gel:forEach>
    
        <gel:out>
            <gel:expr select="$runresult" />
        </gel:out>
    
    </gel:script>

     

    V/r,

    Gene



  • 3.  RE: How do I Insert an Attribute?

    Posted Feb 10, 2014 05:37 PM

    Awesome, thanks so much Gene! -Sam



  • 4.  Re: How do I Insert an Attribute?

    Posted Feb 22, 2016 09:29 AM

    Hi Gene,

     

    I am trying to get your solution to work for me. Thanks.

    I am in a similiar situation but with a slight difference.

     

    I am also trying to add an attribute and set its value in xml.

    Below is part of the script i am trying to run.

     

    <!-- Get value of project_manager attribute-->
    <gel:set var="pmgr" select="$xogresponse/soapenv:Envelope/soapenv:Body/NikuDataBus/Projects/Project/CustomInformation/ColumnValue[@name='project_manager']/text()" asString="true"/> 
    
    <gel:forEach select="$xogresponse/soapenv:Envelope/soapenv:Body/NikuDataBus/Projects/Project" var="v_item">
        <core:expr value="${v_item.setAttribute('managerResourceID', '$pmgr')}" />
    </gel:forEach>
    

     

    The issue is with the second argument of setAttribute method highlighted in red color. Hard coding a value like 'ABC' works fine.

    THe value in the variable is not getting assigned in the xml.

    I have tried passing the following arguments..

    '$pmgr'

     

     

    ${pmgr}

     

    $pmgr

    '${pmgr}'

     

     

     

    Not sure how to pass the value of variable pmgr.

    Any ideas?

     

    regards,

    Deepu.



  • 5.  Re: How do I Insert an Attribute?

    Posted Feb 22, 2016 11:05 AM

    It should be just pmgr without the quotes.

     

    <!-- Get value of project_manager attribute-->  
    <gel:set var="pmgr" select="$xogresponse/soapenv:Envelope/soapenv:Body/NikuDataBus/Projects/Project/CustomInformation/ColumnValue[@name='project_manager']/text()" asString="true"/>   
    
    <gel:forEach select="$xogresponse/soapenv:Envelope/soapenv:Body/NikuDataBus/Projects/Project" var="v_item">  
    <core:expr value="${v_item.setAttribute('managerResourceID', pmgr)}" />  
    </gel:forEach>
    

     

     

     

    Here an example where I change the projectID

     

    <?xml version="1.0" encoding="utf-8"?>
    <gel:script
        xmlns:core="jelly:core"
        xmlns:xog="http://www.niku.com/xog"
        xmlns:jsl="jelly:jsl"
        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:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
        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:out>Starting GetProject List</gel:out>
    
        <!-- Process SQL and XML section -->
        <gel:parse var="runresult">
            <Projects>
                <Project active="false" materialExchageRateType="AVERAGE" name="Engineering" projectID="Eng1">
                    <Allocations />
                    <scenarioDependencies />
                    <InvestmentAssociations>
                        <Allocations>
                            <ParentInvestment InvestmentID="PRJ00000003" InvestmentType="project" defaultAllocationPercent="1">
                                <CustomInformation>
                                    <ColumnValue name="partition_code" />
                                </CustomInformation>
                            </ParentInvestment>
                        </Allocations>
                        <Hierarchies />
                    </InvestmentAssociations>
                    <General addedBy="admin" addedDate="2011-03-30" />
                    <OBSAssocs complete="false">
                        <OBSAssoc id="Projects" name="Department OBS" unitPath="/MS" />
                    </OBSAssocs>
                </Project>
            </Projects>
        </gel:parse>
        
        <gel:set var="projectID" select="$runresult/Projects/Project/@projectID" asString="true"/>   
        <core:set var='projectID' value='MyProject' />
        <gel:log category="XOG" level="INFO">projectID:${projectID}</gel:log>
    
        <gel:forEach select="$runresult/Projects/Project" var="v_item">
            <gel:log category="XOG" level="INFO">getAttribute name:${v_item.getAttribute('name')}</gel:log>
            <gel:set asString="true" select="$v_item/@projectID" var="v_xog_project_id" />
            <gel:log category="XOG" level="INFO">Project:${v_xog_project_id}</gel:log>
    
            <core:expr value="${v_item.setAttribute('projectID', projectID)}" />
            
            <!-- This calls the setAttribute method on the project node -->
            <core:expr value="${v_item.setAttribute('billExpenseType', 'CAP_EXPENSE')}" />
        </gel:forEach>
    
        <gel:out>
            <gel:expr select="$runresult" />
        </gel:out>
    
    </gel:script>
    

     

     

     

    V/r,

    Gene



  • 6.  Re: How do I Insert an Attribute?

    Posted Feb 23, 2016 01:40 AM

    Yup! that's right Sir.

    I was able to figure it out after a couple of tries.

     

    Thank you so much for replying Gene!!

     

    regards,

    D