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

# xmlDOMGet

> xpath マッピングを使用してXML ドキュメントから値を取得します。繰り返し要素や配列もサポートします。

export const siteNameShort = "Arc";

XML ドキュメントから値を取得します。

## 必要なパラメータ

* **map:\***：1つ以上のインプットのセット。マッピングされるパラメータの名前がコロンの前に置かれ（例：`map:foo`）、値はドキュメント内の対象のxml 要素へのxpath です。詳細は[例](#例)を参照してください。

## オプションのパラメータ

* **uri**：XML ファイルのURI。インプットファイルに対する`FilePath` などの動的なファイル参照、ディスク上の静的なファイルパス、またはXML ファイルのパブリックURL を指定できます。
* **handle**：XML データへの読み取り可能なハンドル参照。このハンドルはxmlOpen オペレーションによって作成され、対象のXML がインプットファイルではない場合にのみ必要です。詳細と例については[xmlOpen](./op-xml-open) を参照してください。
* **repeatelement#**：ドキュメント内で繰り返される要素へのxpath。
* **repeatmode**：`repeatelement#` の値をアウトプットアイテムに格納する方法を定義します。使用できる値は`ITEM` および`ARRAY` です。`ITEM`（デフォルト）に設定すると、繰り返し要素から見つかった値は、オペレーションが生成する各アウトプットアイテムに個別の属性として格納されます。`ARRAY` に設定すると、繰り返し要素から見つかった値は単一のアウトプットアイテムに配列属性として格納され、1始まりのインデックスを介してアクセスする必要があります（例：`[result.color#2]` はresult アイテムの`color` 属性内の2番目の値を参照します）。

## アウトプット属性

* **\***：map パラメータを使用してマッピングされたXML 要素からのアウトプット値。これらは、アウトプットアイテムとマッピングされたインプットアイテムの名前を参照してアクセスします（例：`[results.foo]`）。アウトプットアイテムの名前（この場合は`results`）はユーザーが決定します。追加のコンテキストについては[例](#例)を参照してください。

## 例

### XML ドキュメントから値を取得する

次のXML データについて考えてみます。これは{siteNameShort} のScript コネクタへのインプットデータとして渡されます：

```xml theme={null}
<Items>
  <Car>
    <Make>Subaru</Make>
    <Model>WRX</Model>
  </Car>
</Items>
```

Script コネクタでは、{siteNameShort}Script でxmlDOMGet を呼び出して、このデータ内の車の`make` および`model` を取得し、それらをArc のメッセージのヘッダーとして追加できます。次の{siteNameShort}Script コードは、その方法の例を示しています。

```xml theme={null}
<!-- Setting the input uri and map parameters -->
<arc:set attr="xml.uri" value="[FilePath]" />
<arc:set attr="xml.map:make" value="/Items/car/make" />
<arc:set attr="xml.map:model" value="/Items/car/model" />

<!-- Calling the operation, passing in the xml item and creating a "result" output item -->
<arc:call op="xmlDOMGet" in="xml" out="result">
  <!-- result.make and result.model has the value of the xpath because map:make and map:model was 
       set in the input to the op, and result is the name of the output item. Once this
       op closes, result falls out of scope, so use your value inside the arc:call block --> 
  <!-- Setting the values for make and model as headers on the message -->
  <arc:set attr="output.header:carmake" value="[result.make]" />
  <arc:set attr="output.header:carmodel" value="[result.model]" />
</arc:call>

<!-- Setting the output file and pushing the file out -->
<arc:set attr="output.filepath" value="[FilePath]" />
<arc:push item="output" />
```

このコードが実行されると、{siteNameShort} は新しく追加されたヘッダーを持つファイルをアウトプットとしてフローの下流に送ります。

### 特定の繰り返しXML 要素にインデックスを使用してアクセスする

特定の繰り返しXML 要素にアクセスする必要がよくあります。例えば、Script コネクタへのインプットファイルとして渡される次のXML 内で、2番目の`order` の`id` にアクセスしたい場合があります。

```xml theme={null}
<Items>
    <orders>
        <order>
            <date>02/27/2023</date>
            <id>1234</id>
        </order>
        <order>
            <date>02/28/2023</date>
            <id>5678</id>
        </order>
        <order>
            <date>03/01/2023</date>
            <id>9876</id>
        </order>
    </orders>
</Items>
```

Script コネクタでは、{siteNameShort}Script でxmlDOMGet を呼び出し、繰り返される`<orders>` 要素のいずれかに存在する要素への1始まりのxpath を使用して、特定のorder の`id` にアクセスできます。次の{siteNameShort}Script コードは、その方法の例を示しています。

```xml theme={null}
<!-- Setting the input uri and map parameters -->
<arc:set attr="xml.uri" value="[FilePath]" />
<arc:set attr="xml.map:orderid" value="/Items/orders/order\[2\]/id" />

<!-- Calling the operation, passing in the xml item and creating a "result" output item -->
<arc:call op="xmlDOMGet" in="xml" out="result">
  <!-- Setting the result from the mapped id parameter as a header on the message -->
  <arc:set attr="output.header:orderid" value="[result.orderid]" />
</arc:call>

<!-- Setting the output file and pushing the file out -->
<arc:set attr="output.filepath" value="[FilePath]" />
<arc:push item="output" />
```

### repeatelement パラメータを使用する

次のXML データについて考えてみます。これはScript コネクタへのインプットファイルとして渡されます：

```xml theme={null}
<Items>
    <hello>world</hello>
    <colors>
        <color>yellow</color>
        <example>banana</example>
    </colors>
    <colors>
        <color>red</color>
        <example>apple</example>
    </colors>
    <colors>
        <color>orange</color>
        <example>orange</example>
    </colors>
</Items>
```

Script コネクタ内で、次のコードがインプットファイルに対して実行されます。`repeatelement#` パラメータは、繰り返しの値を持つ要素を定義するために使用されます。この例では、`color` および`example` の値は、`/Items/colors` XML 要素内で繰り返されています。`repeatelement#` を設定すると、xmlDOMGet オペレーションはその要素に存在する値をループします。

`repeatmode` パラメータは、`repeatelement#` の値をアウトプットとして格納する方法を定義します。`ITEM`（デフォルト）に設定すると、繰り返し要素によって見つかった値は、アウトプットアイテムに個別の属性として格納されます。`ARRAY` に設定すると、繰り返し要素によって見つかった値は、アウトプットアイテムに配列属性として格納され、1始まりのインデックスを介してアクセスする必要があります（例：`[result.color#2]` は`red` に解決され、`result.example#2` は`apple` に解決されます）。

```xml theme={null}
<!-- Initializing the output data -->
<arc:set attr="out.data" />
<!-- Setting the input uri, repeatmode and map parameters -->
<arc:set attr="xml.uri" value="[FilePath]" />
<arc:set attr="xml.repeatelement#" value="/Items/colors" />
<arc:set attr="xml.repeatmode" value="ITEM" />
<!-- Note, the values for the map are relative to the repeatelement path -->
<arc:set attr="xml.map:color" value="color" />
<arc:set attr="xml.map:example" value="example" /> 

<!-- Calling the operation, passing in the xml item and creating a "result" output item -->
<arc:call op="xmlDOMGet" in="xml" out="result">
  <!-- Setting the output data to the enumerated results of the call -->
  <arc:set attr="out.data">[out.data]
    <arc:enum list="color,example" separator=",">[_value]: [result.[_value]]\n
    </arc:enum>
  </arc:set>
</arc:call>

<!-- Setting the output filename and pushing the file out -->
<arc:set attr="out.filename" value="data.txt" />
<arc:push item="out" />
```

アウトプットファイル：

```
color: yellow
example: banana
color: red
example: apple
color: orange
example: orange
```
