How to check the existence of XML attributes and elements

How-to(s), solutions to common problems, pearls of wisdom

csochor
Posts: 1
Joined: Mon Feb 23, 2009 4:10 pm

How to check the existence of XML attributes and elements

Postby csochor » Fri Mar 13, 2009 6:50 pm

It is possible to create graph, that read XML and check for existence of some attribute or element? Attribute may or may not have a value, it could be ssd=”” or ssd=”vvvv”.

Yes. You may use XPath functions in the mapping xpath attribute. Function "empty()" is useful in this scenario.

Example input xml:

Code: Select all

<employees>

    <employee>
        <empID>1</empID>
        <name ssd="dd">
            <firstname>Mark</firstname>
            <surname>Fish</surname>
        </name>
        <ShieldedRecordNotification/>
    </employee>
      <employee>
        <empID>2</empID>
        <name ssd="">
            <firstname>Jane</firstname>
            <surname>Simson</surname>
        </name>
    </employee>
      <employee>
        <empID>3</empID>
        <name>
            <firstname>Brandon</firstname>
            <surname>Morrison</surname>
        </name>
        <ShieldedRecordNotification>abcde</ShieldedRecordNotification>
    </employee>

</employees>


Example mapping:

Code: Select all

<Context xpath="/employees/employee" outPort="0">
        <Mapping xpath="empID" cloverField="empID"/>
        <Mapping xpath="name/firstname" cloverField="firstname"/>
        <Mapping xpath="name/surname" cloverField="surname"/>
        <Mapping xpath="not(empty(name/@ssd))" cloverField="ssdAttributeExists"/>
        <Mapping xpath="not(empty(ShieldedRecordNotification))" cloverField="ShieldedRecordNotificationElementExists"/>
</Context>


Example result:

Code: Select all

1;Mark;Fish;true;true
2;Jane;Simson;true;false
3;Brandon;Morrison;false;true


cron