Liquid Studio 2021
Data Mapping / Data Mapper Functions / By Category / String / Trim
In This Topic
    Trim
    In This Topic

    Function Name
    Trim
    Category
    String
    Icon
    Trim
    Description
    Trim leading and trailing whitespace from an input String
    Inputs
    String Text String to trim whitespace from
    Outputs
    Result Text String trimmed with whitespace characters removed from the start and end
    Properties
    none -

    Usage

    The Data Mapper String Trim function returns an input String with any whitespace characters removed from the start and end. Since much XML data comes from unpredictable sources such as user input with a system, it is often necessary to trim whitespace characters during mapping, ensuring that the mapped data is ready for use and does not waste any unnecessary storage space. To apply the String Trim transform 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 review_source.xsd, an XML Schema Definition inferred from the following source:

    Source XML
    Copy Code
    <product_reviews>
     <review>
      <product_id>123</product_id>
      <content>
    
        I thought this was a really excellent product.
     
      </content>
     </review>
     <review>
      <product_id>234</product_id>
      <content>
           
         I really hate this product.
    
      </content>
     </review>
    </product_reviews>
    

    We are intentionally including additional whitespace characters at the start and end of the content for demonstration. For our target schema we will be using review_target.xsd, inferred from the following XML:

    Target XML
    Copy Code
    <user_reviews>
     <review_item product="456">
      This is a wonderful item.
     </review_item>
     <review_item product="567">
      This item has good points and bad points.
     </review_item>
    </user_reviews>
    

    The data models feedback reviews for products. Here is the Data Mapper with Source and Target imported:

    Data Mapper String Trim Function

    Let's drag the String Trim function from the Component Palette into the mapping area:

    String Trim Component

    String Trim Transform Added

    The String Trim function takes one input and gives one output. The String input should be connected to the source text String item to trim leading and trailing whitespace characters from. The String Trim function output should be connected to the target item in the XML Writer or another component. First connect the "content" item in the XML Reader to the String input of the Trim function:

    Reader to Trim String Input

    Next connect the Trim output to the Element Value item in the XML Writer:

    Trim Output to Writer Input

    Notice that we are mapping an element in the source XML to an element value in the target XML. The Mapper maps freely between elements, attributes and values.

    Finally let's make the remaining input and output connections:

    Input and Output Connections Mapped

    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
    <user_reviews>
     <review_item product="123">I thought this was a really excellent product.</review_item>
     <review_item product="234">I really hate this product.</review_item>
    </user_reviews>
    

    The input element value has been mapped without the leading and trailing whitespace we had in the XML source within the element content. When text Strings come from varied sources such as text files and other data stores, it is common to end up with whitespace characters such as new lines, carriage returns and spaces being appended at one or both ends. For this reason the Trim function can save considerable time spent carrying out manual alterations. Trimming your Strings means that your mapped text is immediately ready for user display and other uses.

    This is a scalar function so the standard rules apply if multiple values are presented to one or more inputs.

    See Also