Clarity

Expand all | Collapse all

GEL - Xpath How to modify/delete an attribute inside a node?

  • 1.  GEL - Xpath How to modify/delete an attribute inside a node?

    Posted 11-25-2015 11:49 AM

    Hi all,

     

    I am having troubles to modify/delete an attribute inside a node in XML using Xpath.
    This is what I have:

     

    <gel:set select="$xog_template//Tasks/Task[@taskID=$thisExtId]" var="taskEntry"/>                                   1) Select a node from xog_template

    <gel:set insert="true" select="$xogRequest//Tasks" value="${taskEntry}"/>                                                   2) Insert the node in xogRequest

    <gel:set asString="true" select="$xogRequest//Tasks/Task/@nextSiblingOf" var="sibling"/>                         3) Extract the attribute nextSiblingOf into variable sibling

    <core:if test="${sibling == '0'}">                                                                                                                        4) Check the value of sibling

         <******>                                                                                                                                                  5) Delete the attribute nextSiblingOf or give a new empty value ""

    </core:if>

     

     

     

    Could anyone provide me an example of Xpath syntax or help me to complete the step 5?

     

    Many thanks



  • 2.  Re: GEL - Xpath How to modify/delete an attribute inside a node?

    Posted 11-25-2015 01:06 PM

    So your taskEntry variable is holding a org.w3c.dom Element which has a getAttribute method.

     

    So given this gel script:

     

    <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 var="thisExtId"  value="3" />
    
        <gel:parse var="xog_template">
            <root>
                <Tasks code="CTU Team">
                    <Task taskID="1" theAttributeToCheck="One" />
                    <Task taskID="2" theAttributeToCheck="Two" />
                </Tasks>
                <Tasks code="WAT Team">
                    <Task taskID="3" theAttributeToCheck="Three" />
                    <Task taskID="4" theAttributeToCheck="Four" />
                </Tasks>
            </root>
        </gel:parse>
       
        <!-- taskEntry is a org.w3c.dom Element -->
        <gel:set select="$xog_template//Tasks/Task[@taskID=$thisExtId]" var="taskEntry"/>
    
        <!-- So we can pull off an attribute value with the getAttribute method -->
        <core:set var="theAttributeToCheck" value='${taskEntry.getAttribute("theAttributeToCheck")}' />
        <gel:log>This is the theAttributeToCheck value = ${theAttributeToCheck}</gel:log>
       
        <!-- do our check and perform some action if true -->
        <core:if test="${theAttributeToCheck.equalsIgnoreCase('Three')}">
            <gel:log>The If statement validated that theAttributeToCheck = Three</gel:log>
        </core:if>
    
    </gel:script>
    

     

    Produces the following:

     

     

    At this point you can modify the node in taskEntry to whatever you need and then add it to the $xogRequest document.

    V/r,

    Gene



  • 3.  Re: GEL - Xpath How to modify/delete an attribute inside a node?

    Posted 11-26-2015 08:44 AM

    Hi,

     

    First thanks for your answer.

    I see your point, but what I am actually interested in, is the action I want to perform inside the check condition.

    The action I want to perform is to modify or delete one specific attribute inside the node. Lets say I want to delete the attribute nextSiblingOf from the task node, or give an empty value ("").

     

    <core:if test="${sibling == '0'}">

              <Action to delete/modify the attribute/>

    </core:if>

     

    Thanks,



  • 4.  Re: GEL - Xpath How to modify/delete an attribute inside a node?

    Posted 11-26-2015 09:43 AM

    Here is the element documentation

     

    Element (Java Platform SE 7 b99)

     

    There are both a removeAttribute and setAttribute methods.

     

    <!-- To remove the action would be: -->
    <core:expr value='${taskEntry.removeAttribute("theAttributeToCheck")}'/>
    
    <!-- To change the value the action would be: -->
    <core:expr value='${taskEntry.setAttribute("theAttributeToCheck", "a new value for the attribute")}/>
    
    

     

     

    V/r,

    Gene