Special Formatters

Version 22.0.8473


Special Formatters

Version 22.0.8473


The following is a list of formatters that are used in conjunction with operations that parse XML and CSV. Examples of each formatter are provided at the bottom of the page.

xpath(value)

The XPath formatter can be used inside calls to XML operations that have the XML DOM available. The xmlDOMSearch and xmlDOMGet operations are used to parse an XML document and the XPath formatter is a formatter of the DOM object that functions like other formatters by generating the values from an XML document. The XPath formatter returns the current XPath location of an XML document.

  • value: The optional string of a relative XPath to find the associated XML value from.

xsubtree(value)

You can use xsubtree to parse XML trees from nested XML structures. In the example below, there are three entries under /Event/Attendees.

<arc:set attr="xml.text">
<Event>
    <Subject>Meeting</Subject>
    <Attendees>
        <User>
            <Code>1</Code>
            <Name>TEST1</Name>
        </User>
        <User>
            <Code>2</Code>
            <Name>TEST2</Name>
        </User>
        <User>
            <Code>3</Code>
            <Name>TEST3</Name>
        </User>
    </Attendees>
</Event>
</arc:set>

<arc:set attr="xml.xpath" value="/Event/Attendees" />
<arc:call op="xmlDOMSearch" in="xml">
  [xsubtree(.)]  <!-- Insert desired xpath in parentheses  -->
</arc:call>

The . character in the xsubtree command instructs the script to use the current xpath. In this example, that path is /Event/Attendees, which returns all three entries:

<User>
  <Code>1</Code>
  <Name>TEST1</Name>
</User>
<User>
  <Code>2</Code>
  <Name>TEST2</Name>
</User>
<User>
  <Code>3</Code>
  <Name>TEST3</Name>
</User>

If you wanted to only show the subtree for the second occurrence of the User xpath within /Event/Attendees, you would replace the command with xsubtree(User\[2\]). The result of that would be:

<User>
  <Code>2</Code>
  <Name>TEST2</Name>
</User>

csv(value)

The CSV formatter can be used after loading the data from a CSV file. The csvListRecords operation is used to parse a CSV file and the CSV formatter is a formatter of the CSV data that functions like other formatters by generating the values from a CSV document.

  • value:The string of a CSV column header to find the values from that column.Note that if the CSV file does not have headers, columns can be accessed by a generic index (c1, c2, c3, …) instead if the ‘requireheader’ attribute is set to false.

[csv(value) | csvescape()]

The csvescape formatter returns the CSV data from the column that matches a value with each element output as individual results.

Examples

xpath

In the following snippet, an XML document is defined in memory in ArcScript.

<arc:set attr="text">
  <root>
    <A>Value_One</A>
    <A>
      <B>
        <C>Value_Two</C>
      </B>
    </A>
    <A>
      <B>Value_Three</B>
    </A>
  </root>
</arc:set>

In the following snippet, the xmlDOMSearch operation is called and XPath formatters are displayed from the sample XML document.

<arc:call op="xmlDOMSearch?xpath=/root/A">
  Current XPath is [xpath] and the 'B' element value is [xpath(B) | empty("not present")]
</arc:call>

Run against the preceding XML document, this code would display the following output.

Current XPath is /root/A[1] and the 'B' element value is not present
Current XPath is /root/A[2] and the 'B' element value is not present
Current XPath is /root/A[3] and the 'B' element value is Value_Three

In the previous example, the XPath formatter displays the three A elements that are iterated over. But only Value_Three from the final B element is displayed as [xpath(B)] ignores the values in other elements.

csv and csvescape

In the following snippets, a CSV document is defined in memory in ArcScript.

<arc:set attr="data" value=
"column1,column2,column3
entry1,entry2,entry3
entry4,entry5,entry6"
/>

In the following example, the csvListRecords operation is called and CSV formatters are displayed from the sample CSV document.

<arc:call op="csvListRecords">
Row [_index] values: [csv('column1')], [csv('column2')], [csv('column3')]
</arc:call>

This code would display the following output:

Row 1 values: entry1, entry2, entry3
Row 2 values: entry4, entry5, entry6

In the following example, both the CSV formatter and the csvescape options are output from the sample CSV document.

<arc:call op="csvListRecords">
Standard output: [csv('column1')], [csv('column2')], [csv('column3')]
Output with csvescape(): [csv('column1') | csvescape()], [csv('column2') | csvescape()], [csv('column3') | csvescape()]
</arc:call>

This code would display the following output for the first row under the CSV headers.

Standard output: entry1, entry2, entry3
Output with csvescape(): "entry1", "entry2", "entry3"