Liquid XML Studio
First
See Also Send Feedback
Data Mapping > Data Mapper Functions > By Category > Set > First

Glossary Item Box

Function Name
First
Category
Set
Icon
First
Description
Map only the first in a sequence of values
Inputs
Nodes Sequence to map the first value of
Outputs
Result First in the specified input sequence
Properties
None -

Usage

The Data Mapper Set First function allows you to map the first in a set of sibling items in your XML source. There are many possible reasons for mapping only the first in a set of data values. For example, you may be subject to storage limitations in your target application, or may be sorting your data and mapping the first in a set of resulting values. To apply the Set First function, use the following process:

Create a new Data Mapper file, dragging your XML data source and targets into the editor area.

For this example we are using clubs_source.xsd, an XML Schema Definition

The following source XML will be used:

Source XML Copy Code
<sport_clubs>
 <club>
  <name>New Town Football Club</name>
  <member>
   <active>Yes</active>
   <member_name>Chris Black</member_name>
   <member_since>2005</member_since>
   <won>League 2009</won>
   <won>Cup 2007</won>
  </member>
  <member>
   <active>Yes</active>
   <member_name>Jim Thomson</member_name>
   <member_since>2003</member_since>
   <won>League 2010</won>
  </member>
  <member>
   <active>No</active>
   <member_name>Barry Jones</member_name>
   <member_since>1996</member_since>
   <won>League 1999</won>
   <won>Cup 1999</won>
   <won>League 2000</won>
  </member>
 </club>
 <club>
  <name>City Netball Team</name>
  <member>
   <active>No</active>
   <member_name>Jacqueline Johnson</member_name>
   <member_since>1999</member_since>
   <won>National Championship 2001</won>
   <won>Regional Championship 2002</won>
   <won>National Championship 2002</won>
  </member>
  <member>
   <active>Yes</active>
   <member_name>Joanne Smith</member_name>
   <member_since>2005</member_since>
   <won>Regional Championship 2007</won>
   <won>National championship 2007</won>
  </member>
  <member>
   <active>Yes</active>
   <member_name>Mary Owens</member_name>
   <member_since>2007</member_since>
   <won>National Championship 2008</won>
  </member>
 </club>
</sport_clubs>
 

For our target schema we will be using clubs_target.xsd

 

The data models sporting clubs and their members. Some of the elements can appear multiple times.

Let's assume that we want the member from each club that has been their the longest. Here is the Data Mapper with Source and Target imported:

Data Mapper Source and Target

Drag the Set First function from the Component Palette into the mapping area:

Set First Component

 

We will use a sort to get the member with the longest time at the club at the top, then we will take only the first entry for each club.

So add a 'Sort' and a 'First' sub component to the transform.

We can now connect the 'Xml source->member' to the 'Sort' functions 'Nodes' input, notice when you do this the the Sort functions output nodes appear.

You can now use the Sort output 'member' to feed into the 'First' components 'Nodes' input, again the First functions outputs are created when you do this.

We can now execute the transform by pressing Shift-F5 or the Execute button (Execute). The transform is applied and the file we selected as output opens in the editor:

Output XML Copy Code
<clubs>
  <team t_name="New Town Football Club">
    <player p_name="Barry Jones" is_active="No">
      <joined>1996</joined>
      <title>League 1999</title>
      <title>Cup 1999</title>
      <title>League 2000</title>
    </player>
  </team>
  <team t_name="City Netball Team">
    <player p_name="Jacqueline Johnson" is_active="No">
      <joined>1999</joined>
      <title>National Championship 2001</title>
      <title>Regional Championship 2002</title>
      <title>National Championship 2002</title>
    </player>
  </team>
</clubs>

The longest serving player for each team is now shown.

If the First function receives an empty input sequence, it will output nothing.

See Also