> ## Documentation Index
> Fetch the complete documentation index at: https://docs.arc.cdata.com/llms.txt
> Use this file to discover all available pages before exploring further.

# ファンクション

> パイプで渡される入力値を必要とせずにアウトプットを生成する、CSV、JSON、XML、およびユーティリティ操作のためのArcScript ファンクションフォーマッタ。

export const siteNameShort = "Arc";

ファンクションは、アウトプット値を生成するものの、標準のフォーマッタのようにインプットを必要としない、特別な種類のフォーマッタと考えてください。ファンクションは単独で使用することも、その後に他のフォーマッタをパイプでつなぐ、より複雑な式の起点として使用することもできます。例えば、`[vault(foo)]` は単独で解決できますが、式の起点として使用することもできます（例えば、`foo` のVault 値が`bar` と等しいかどうかをチェックするには`[vault(foo) | equals(bar)]`）。

## CSV ファンクション

### csv(value)

CSV ファイルからデータを読み込んだ後に使用します。`csvListRecords` オペレーションはCSV ファイルを解析し、CSV フォーマッタは他のフォーマッタと同様に、CSV ドキュメントから値を生成することで動作します。

* **value**：値を見つけるためのCSV 列ヘッダーの文字列。CSV ファイルにヘッダーがない場合、*requireheader* 属性をfalse に設定すると、列に汎用インデックス（c1、c2、c3、...）でアクセスできます。

#### 例

以下のスニペットでは、CSV ドキュメントが{siteNameShort}Script のメモリ内で定義されています。

```xml theme={null}
<arc:set attr="data" value=
"column1,column2,column3
entry1,entry2,entry3
entry4,entry5,entry6"
/>
```

次に、`csvListRecords` オペレーションが呼び出され、サンプルCSV ドキュメントからCSV フォーマッタが表示されます。

```xml theme={null}
<arc:call op="csvListRecords">
Row [_index] values: [csv('column1')], [csv('column2')], [csv('column3')]
</arc:call>
```

このコードを実行すると、次の出力が表示されます：

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

## JSON ファンクション

### hasjsonpath(jsonpath)

入力jsonpath がJSON ドキュメント内に存在するかどうかをチェックし、boolean（true/false）を返します。

* **jsonpath**：チェックする相対jsonpath。

#### 例

```xml theme={null}
<arc:set attr="json.text">
  {
    "key1": null,
    "key2": "this is a string",
    "key3": "hello world",
    "key4": 123,
    "key5": {},
    "key6": \[],
    "key7": true
  }
</arc:set>
<arc:set attr="json.jsonpath" value="/json"/>
<arc:call op="jsonDOMSearch" in="json" out="result">
  <!-- This checks to see if the desired jsonpath exists and throws an error if it does not.  -->
  <arc:if exp="[hasjsonpath(key10) | equals(false)]">
    <arc:throw code="noJSONPath" desc="The jsonpath you were looking for does not exist!" />
  </arc:if>
</arc:call>
```

### isjsonpathnull(jsonpath, \[ifTrue], \[ifFalse])

入力jsonpath をチェックし、jsonpath がnull であるか存在しない場合にboolean（true/false）を返します。

* **jsonpath**：チェックする相対json（オプション）。
* **ifTrue**：フォーマッタがtrue に解決される場合に返す値（オプション）。
* **ifFalse**：フォーマッタがfalse に解決される場合に返す値（オプション）。

#### 例

```xml theme={null}
<arc:set attr="json.text">
  {
    "key1": null,
    "key2": "this is a string",
    "key3": "hello world",
    "key4": 123,
    "key5": {},
    "key6": \[],
    "key7": true
  }
</arc:set>
<arc:set attr="json.jsonpath" value="/json"/>
<arc:call op="jsonDOMSearch" in="json" out="result">
  <arc:if exp="[IsJSONPathNull(key1) | equals(true)]" >
    <!-- This executes because key1 has a value of null -->
    <arc:set attr="_log.info" value="The jsonpath for key1 either has a value of null or does not exist." />
    <!-- Overriding the default true/false output of the formatter to 1/0 -->
    <arc:if exp="[IsJSONPathNull(key10, 1, 0) | equals(1)]" >
      <!-- This executes because key10 does not exist within the input json text -->
      <arc:set attr="_log.info" value="The jsonpath for key10 either has a value of null or does not exist." />
    </arc:if>
  </arc:if>
</arc:call>
```

### jsonpath(jsonpath)

このフォーマッタは、JSON DOM が利用可能なJSON オペレーションへの呼び出し内で使用できます。[jsonDOMSearch](../operations/op-json-dom-search) および[jsonDOMGet](../operations/op-json-dom-get) オペレーションを使用して、JSON ドキュメントを解析できます。jsonpath フォーマッタはDOM オブジェクトのフォーマッタであり、他のフォーマッタと同様に、JSON ドキュメントから値を生成することで動作します。JSON ドキュメントの現在のjsonpath の位置を返します。オプションで、関連するJSON 値を見つけるために相対jsonpath の文字列を指定できます。

* **jsonpath**：関連するJSON 値を見つけるための相対jsonpath（オプション）。

#### 例

```xml theme={null}
<arc:set attr="json.text">
  {
    "key1": null,
    "key2": "this is a string",
    "key3": "hello world",
    "key4": 123,
    "key5": {},
    "key6": \[],
    "key7": true
  }
</arc:set>
<arc:set attr="json.jsonpath" value="/json"/>
<arc:call op="jsonDOMSearch" in="json" out="result">
  <!-- This prints 'I just want to say, hello world!' to the application log -->
  <arc:set attr="_log.info" value="I just want to say, [jsonpath(key3)]!"/>
</arc:call>
```

### jsonsubtree(jsonpath)

jsonsubtree を使用して、ネストされたJSON 構造からJSON サブツリーを解析できます。

* **jsonpath**：構造のどのレベルから開始するかを示す相対jsonpath の文字列（オプション）。

#### 例

以下のスクリプトには、JSON データ内の`/json/Event/Attendees` オブジェクトの下にある3つの`User` エントリが含まれています。`Attendees` オブジェクトは、ルートオブジェクトのサブツリーです。

```xml theme={null}
<arc:setc attr="json.text">
{
  "Event": {
    "Subject": "Meeting",
    "Attendees": {
      "User": [
        {
          "Code": "1",
          "Name": "Jane Doe"
        },
        {
          "Code": "2",
          "Name": "John Smith"
        },
        {
          "Code": "3",
          "Name": "Alex Johnson"
        }
      ]
    }
  }
}
</arc:setc>
<arc:set attr="json.jsonpath" value="/json/Event/Attendees" />
<arc:call op="jsonDOMSearch" in="json">
<arc:set attr="_log.info">  <!-- Logging the results to the application log -->
[jsonsubtree(.)]  <!-- Insert your desired jsonpath inside the parentheses -->
</arc:set>
</arc:call>
```

この例では、jsonpath は`.` 文字であり、これはルートオブジェクト内のどこにいるかに基づいて現在のjsonpath を使用するようスクリプトに指示します。ここでは、そのパスは`/json/Event/Attendees` であり、3つすべてのエントリを返します。

次の画像は、**Logs** ページの[アプリケーションログ](../../getting-started/administration/activity#アプリケーションログ)にどのように表示されるかを示しています：

<img src="https://mintcdn.com/cdata-arc/AKthyJ-LnqphL8js/public/images/jsonsubtree_activity_log.png?fit=max&auto=format&n=AKthyJ-LnqphL8js&q=85&s=f73d948b74e758708f9bc2afe72898e5" alt="Activity ログ内のjsonsubtree の結果" width="800" data-path="public/images/jsonsubtree_activity_log.png" />

以下は、生のJSON サブツリーの結果です：

```
"Attendees": {
      "User": [
        {
          "Code": "1",
          "Name": "Jane Doe"
        },
        {
          "Code": "2",
          "Name": "John Smith"
        },
        {
          "Code": "3",
          "Name": "Alex Johnson"
        }
      ]
    }
```

`/json/Event/Attendees` 内の`User` jsonpath の2番目の出現箇所のサブツリーのみを表示するには、`jsonsubtree` フォーマッタ呼び出し内のパスを`jsonsubtree(User/\[2\])` に置き換えます。これにより、次の出力が得られます：

```
{
  "Code": "2",
  "Name": "John Smith"
}
```

### jsontype(jsonpath)

現在のJSON 名前と値のペアのデータ型（string、number、object、array、またはboolean）を返します。

* **jsonpath**：関連するJSON 値を見つけるための相対jsonpath（オプション）。

#### 例

```xml theme={null}
<arc:set attr="json.text">
  {
    "key1": null,
    "key2": "this is a string",
    "key3": "hello world",
    "key4": 123,
    "key5": {},
    "key6": \[],
    "key7": true
  }
</arc:set>
<arc:set attr="json.jsonpath" value="/json"/>
<arc:call op="jsonDOMSearch" in="json" out="result">
  <!-- Creating some output data that describes various elements of the input json -->
  <arc:set attr="output.data">The jsontype of key4 is [jsontype(key4)]
The jsontype of key5 is [jsontype(key5)]
The jsontype of key6 is [jsontype(key6)]
The jsontype of key7 is [jsontype(key7)]
  </arc:set>
</arc:call>

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

<!-- contents of output.txt -->

The jsontype of key4 is NUMBER
The jsontype of key5 is OBJECT
The jsontype of key6 is ARRAY
The jsontype of key7 is BOOL
```

## XML ファンクション

### hasxpath(xpath)

入力xpath がXML ドキュメント内に存在するかどうかをチェックし、boolean（true/false）を返します。

* **xpath**：チェックする相対xpath。

#### 例

```xml theme={null}
<arc:set attr="xml.text">
  <Items>
    <Foo>Bar</Foo>
    <Bar>Foo</Bar>
  </Items>
</arc:set>
<arc:set attr="xml.xpath" value="/Items"/>
<arc:call op="xmlDOMSearch" in="xml" out="result">
  <!-- This checks to see if the desired xpath exists and throws an error if it does not  -->
  <arc:if exp="[hasxpath(helloworld) | equals(false)]">
    <arc:throw code="noXPath" desc="The xpath you were looking for does not exist!" />
  </arc:if>
</arc:call>
```

### isxpathnull(xpath, \[ifTrue], \[ifFalse])

入力xpath をチェックし、xpath がnull であるか存在しない場合に`xsi:nil` XML 属性（`xsi:nil="true/false"`）の形式でboolean を返します。条件が満たされる場合と満たされない場合の代替値を指定するには、*ifTrue* および*ifFalse* パラメータを使用します。

* **xpath**：チェックする相対xpath（オプション）。
* **ifTrue**：フォーマッタがtrue に解決される場合に返される値（オプション）。
* **ifFalse**：フォーマッタがfalse に解決される場合に返される値（オプション）。

#### 例

```xml theme={null}
<arc:set attr="xml.text">
  <Items>
    <Foo>Bar</Foo>
    <Bar>Foo</Bar>
    <helloworld xsi:nil="true" />
  </Items>
</arc:set>
<arc:set attr="xml.xpath" value="/Items"/>
<arc:call op="xmlDOMSearch" in="xml" out="result">
  <arc:if exp="[IsXPathNull(helloworld) | contains(true)]" >
    <!-- This executes because the 'helloworld' has the xsi:nil attribute set to true -->
    <arc:set attr="_log.info" value="The xpath for 'helloworld' either has a value of null or does not exist." />
    <!-- Overriding the default output of the formatter to use true/false. If the exp is true, the script inside runs. -->
    <arc:if exp="[IsXPathNull(waldo, true, false)]" >
      <!-- This executes because the 'waldo' element does not exist within the input xml text -->
      <arc:set attr="_log.info" value="The xpath for 'waldo' either has a value of null or does not exist." />
    </arc:if>
  </arc:if>
</arc:call>
```

### xpath(xpath)

XML DOM が利用可能なXML オペレーションへの呼び出し内で使用します。[xmlDOMSearch](../operations/op-xml-dom-search) および[xmlDOMGet](../operations/op-xml-dom-get) オペレーションを使用して、XML ドキュメントを解析できます。XPath フォーマッタはDOM オブジェクトのフォーマッタであり、他のフォーマッタと同様に、XML ドキュメントから値を生成することで動作します。XML ドキュメントの現在のXPath の位置を返します。

* **xpath**：関連するXML 値を見つけるための相対XPath の文字列（オプション）。

#### 例

以下のスニペットでは、XML ドキュメントが{siteNameShort}Script のメモリ内で定義されています。

```xml theme={null}
<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>
```

このスニペットでは、[xmlDOMSearch](../operations/op-xml-dom-search) オペレーションが呼び出され、サンプルXML ドキュメントからXPath フォーマッタが表示されます。

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

前述のXML ドキュメントに対して実行すると、このコードは次の出力を表示します。

```
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
```

XPath フォーマッタは、反復処理される3つの`A` 要素を表示しますが、`[xpath(B)]` は他の要素の値を無視するため、最後の`B` 要素の`Value_Three` のみが表示されます。

### xpathcount(xpath)

XPath フォーマッタに似ていますが、提供されたXPath に一致する要素の*カウント*を返す点が異なります。XML DOM が利用可能なXML オペレーションへの呼び出し内で使用します。[xmlDOMSearch](../operations/op-xml-dom-search) および[xmlDOMGet](../operations/op-xml-dom-get) オペレーションを使用して、XML ドキュメントを解析できます。XPathCount フォーマッタはDOM オブジェクトのフォーマッタであり、他のフォーマッタと同様に、XML ドキュメントからカウントを生成することで動作します。

* **xpath**：カウントを計算する相対XPath の文字列（オプション）。

#### 例

```xml theme={null}
<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-sensitive 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" />
```

このコードを実行すると、出力として`2` を返します。

### xsubtree(xpath)

ネストされたXML 構造からXML ツリーを解析します。

* **xpath**：構造のどのレベルから開始するかを示す相対XPath の文字列（オプション）。

#### 例

以下のサンプルXML ドキュメントでは、`/Event/Attendees` の下に3つのエントリがあります。

```xml theme={null}
<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>
```

`xsubtree` コマンド内の`.` 文字は、現在のxpath を使用するようスクリプトに指示します。この例では、そのパスは`/Event/Attendees` であり、3つすべてのエントリを返します：

```xml theme={null}
<User>
  <Code>1</Code>
  <Name>TEST1</Name>
</User>
<User>
  <Code>2</Code>
  <Name>TEST2</Name>
</User>
<User>
  <Code>3</Code>
  <Name>TEST3</Name>
</User>
```

`/Event/Attendees` 内の`User` xpath の2番目の出現箇所のサブツリーのみを表示するには、コマンドを`xsubtree(User[2])` に置き換えます。その結果は次のとおりです：

```xml theme={null}
<User>
  <Code>2</Code>
  <Name>TEST2</Name>
</User>
```

## その他のファンクション

### guid(includehyphens)

グローバルに一意な識別子（GUID）の値を生成します。デフォルトでは、フォーマッタはすべての文字を単一のフォーマットされていない文字列で返します。標準の8-4-4-4-12 GUID 形式に準拠したハイフンを含めるには、*includehyphens* パラメータを使用し、*true* に設定します。

* **includehyphens**：返されるGUID にハイフンを含めるには*true* に設定します。

このフォーマッタは入力属性（変数）を変更しないため、そのような入力属性は必要ありません。

#### 例

```xml theme={null}
<arc:set attr="myGUID" value="[guid(true)]" />
```

### vault(ItemName, \[ifnotexists])

*ItemName* に指定された値と一致する、[Global Settings Vault](../../getting-started/administration/settings/global-settings-vault) 内のVault アイテムの値を返します。デフォルトでは、Vault アイテムが存在しない場合、エラーがスローされます。

* **ItemName**：取得するVault アイテムの名前。
* **ifnotexists**：Vault アイテムが存在しない場合にエラーがスローされるのを防ぐには、このオプションのパラメータと、`value does not exist` などのデフォルト値を指定します。指定された*ItemName* を持つVault アイテムが存在しない場合に、その値が返されます。

<Note>スクリプトのコンテキストで暗号化されたVault アイテムを参照する場合は、機密情報がログに含まれないように注意してください。</Note>

#### 例

```xml theme={null}
<arc:set attr="URLtoResource" value="[Vault(commonURL)]" />
```
