Special Formatters

Version 23.4.8839


Special Formatters


The following is a list of formatters that are used in conjunction with operations that parse XML and CSV.

xpath(value)

The XPath formatter can be used inside calls to XML operations that have the XML DOM available. You can use the xmlDOMSearch and xmlDOMGet operations 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.

xpath Example

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>

When run against the preceding XML document, this code displays 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.

xpathcount(value)

The XPathCount formatter is very similar to the XPath formatter, except that it returns the count of the elements that match the provided XPath. It can be used inside calls to XML operations that have the XML DOM available. You can use the xmlDOMSearch and xmlDOMGet operations to parse an XML document, and the XPathCount formatter is a formatter of the DOM object that functions like other formatters by generating counts from an XML document.

  • value: The optional string of a relative XPath from which to calculate the count.

xpathcount Example

<arc:set attr="xml.xpath" value="/Items/Cars/Subaru" />
<arc:set attr="xml.text">
 <Items>
  <Cars>
    <Subaru>
      <Color>Blue</Color>
      <Year>2017</Year>
    </Subaru>
    <Honda>
      <Color>Red</Color>
    </Honda>
  </Cars>
</Items>
</arc:set>
<arc:call op="xmlDOMSearch" in="xml" out="result">
  <!-- xpathcount is a context-senstive function in arcscript. If an xml document is loaded in a 
       search, xpathcount returns the count of the number of elements that match the provided xpath. -->
  <arc:set attr="output.Data" value="[xpathcount('/Items/Cars/*/Color')]" />
</arc:call>

<arc:set attr="output.filename" value="test.txt" />
<arc:push item="output" />

When this code is run, it returns 2 as output.

xsubtree(value)

You can use xsubtree to parse XML trees from nested XML structures.

xsubtree Example

In the example XML document 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 want to only show the subtree for the second occurrence of the User xpath in /Event/Attendees, replace the command with xsubtree(User\[2\]). The result of that is:

<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. 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 Example

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>

Running this code displays the following output:

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

[myitem.value | csvescape()]

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

csvescape Example

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>

Running this code displays the following output for the first row under the CSV headers.

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