xpathetlscriptella

Scriptella: How to handle the error while fetching data from XML through Xpath


i have one query regarding data fetching from XML xpath.

 <query connection-id="in">
    CommunicationCenter/Response/MenuData/Menu/Noun
<!--something I have to do inside script using the data fetched from xpath-->
</query>

My question is - suppose 1 of the XML doesn't have this structure of Xpath. It has "OtherCommCenter/MenuData/Menu/Noun" or something else structure. Then, when I run the job, it says the job is executed with no exception & as it does not get any value from xpath, nothing is happened. Means it gives back null. So, how do I catch the error there? I have to know which element in xpath creates the problem or if that is not possible, at least which xml is creating this error in structure ?

(because in my project, there are multiple XMLs I have to handle & I am doing that by submitting jobs to ExecutorService like you described in How to ETL multiple files using Scriptella?)

P.S. for the last part, I am doing like this

<connection id="in" driver="xpath" url="$input"/>

where "input" is the map key for the different xml file name.

can anybody help me? it is necessary to know asap for my project.


Solution

  • Here is an artificial example. Let's say there are kind of XMLs - people and cars.

    people.xml:

    <people>
        <person></person>
    </people>
    

    Cars.xml:

    <cars>
        <car/>
    </car>
    

    The following xml runs 2 queries for /people/person and /cars/car and set global flags if at least one record was found:

    <etl>
        <connection driver="jexl" id="jexl"/>
        <connection driver="xpath" id="xpath" url="input.xml"/>
        <connection id="log" driver="text"/>
    
        <query connection-id="xpath">
            /people/person
            <script connection-id="jexl">
                # set flag to true if at least one element was found
                etl.globals['people']=true;
            </script>
        </query>
        <script connection-id="log" if="!etl.globals['people']">
            WARNING: No people found in the XML file
        </script>
    
        <query connection-id="xpath">
            /cars/car
            <script connection-id="jexl">
                etl.globals['cars']=true;
            </script>
        </query>
    
        <script connection-id="log" if="!etl.globals['cars']">
            WARNING: No cars found in the XML file
        </script>
    </etl>