---
schema_version: "1"
id: b6a793eda30fef129faa491643bfd67d714b67c20a57dda20a864dafcdb603f2
title: Choice (<choice/>)
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/anypoint-code-builder/acb-component-choice.md
canonical_url: https://docs.mulesoft.com/jp/anypoint-code-builder/acb-component-choice
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 31f1b1e5f4d4558f01017027400ed769cc49f25bba7befd789c9ea890cadc29f
description: MuleSoft Documentation Site
latest: true
---
# Choice (<choice/>)

![logo cloud IDE](/mulesoft/current/ja-jp/_/img/logos/deployment-cloud-ide.png "Cloud IDE") Cloud IDE

![logo desktop IDE](/mulesoft/current/ja-jp/_/img/logos/deployment-desktop-ide.png "Desktop IDE") Desktop IDE

メッセージコンテンツを評価する一連の DataWeave 式に従ってフローで動的にメッセージをルーティングします。

各式は、異なるルーティングオプションに関連付けられます。これにより、大部分のプログラミング言語の ​`if`​/​`then`​/​`else`​ コードブロックのような条件付き処理がフローに追加されます。

Choice ルーターが実行するルートは 1 つのみです。つまり、最初に ​`true`​ と評価された式がルートの実行をトリガーし、他のルートはチェックされません。どの式も ​`true`​ でない場合は、デフォルトルートが実行されます。

Choice ルーターを設定するには、1 つ以上の評価する条件と、いずれかの条件が ​`true`​ と評価された場合に実行されるメッセージプロセッサーを指定します。さらに、定義された条件がいずれも ​`true`​ でない場合に実行されるデフォルトルートを設定します。

## [](#component-xml)コンポーネント XML

このコンポーネントは、次の XML 構造をサポートします。

```xml
<choice doc:name="Choice" doc:id="qwolaa" tracking:enable-default-events="true" >
  <when expression=${expressionToEvaluate}> (1)
    <!-- Message processors --> (2)
  </when>
  <otherwise>
    <!-- Message processors --> (3)
  </otherwise>
</choice>
```

<table><tbody><tr><td><i class="conum" data-value="1"></i><b>1</b></td><td><code>expression</code>​ 属性には、評価する条件が含まれています。</td></tr><tr><td><i class="conum" data-value="2"></i><b>2</b></td><td><code>expression</code>​ が ​<code>true</code>​ と評価された場合に実行される任意の数のメッセージプロセッサーを追加します。</td></tr><tr><td><i class="conum" data-value="3"></i><b>3</b></td><td>条件がいずれも ​<code>true</code>​ と評価されなかった場合に実行される任意の数のメッセージプロセッサーを追加します。</td></tr></tbody></table>

Choice (​`<choice/>`​) 属性は UI および XML で設定できます。

<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 60%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">属性名</th>
<th class="tableblock halign-left valign-top">属性 XML</th>
<th class="tableblock halign-left valign-top">説明</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Choice (選択肢)</strong>​ (デフォルト)</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>doc:name</code></p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>キャンバスに表示されるコンポーネントの編集可能な名前。</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">なし</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>doc:id</code></p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>コンポーネントの自動生成された識別子。</p>
</div></div></td>
</tr>
</tbody>
</table>

Choice (​`<choice/>`​) には次の子要素があります。

<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 25%;">
<col style="width: 75%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">子要素</th>
<th class="tableblock halign-left valign-top">説明</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>&lt;when/&gt;</code></p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>Mule イベントを処理する埋め込みコンポーネントを実行する前に照合する条件を指定するための子要素。1 つ以上の ​<code>&lt;when/&gt;</code>​ 要素が許可されます。</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>&lt;otherwise/&gt;</code></p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p><code>&lt;when/&gt;</code>​ 要素内で設定された条件が満たされない場合に実行する子要素。</p>
</div></div></td>
</tr>
</tbody>
</table>

子要素 ​`<when/>`​ には次の属性があります。

<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 20%;">
<col style="width: 20%;">
<col style="width: 60%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">属性名</th>
<th class="tableblock halign-left valign-top">属性 XML</th>
<th class="tableblock halign-left valign-top">説明</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><strong>Expression (式)</strong></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>expression</code></p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>入力を評価するために使用する DataWeave 式。式が ​<code>true</code>​ に評価されると、このルーティングオプションが実行されます。</p>
</div></div></td>
</tr>
</tbody>
</table>

## [](#choice-ルーターへのルートの追加)Choice ルーターへのルートの追加

デフォルトでは、Choice ルーターは設定された DataWeave 式が ​`true`​ に評価されると実行される 1 つのルートオプションと、既存ルートのどの式も ​`true`​ に評価されなかったときに実行されるデフォルトルートを持ちます。複数の条件を評価して、成立した条件に応じて異なる操作を実行する必要がある場合は、ルートを追加してください。

新しいルートを追加する手順は、次のとおりです。

1. `<choice/>`​ 要素内に ​`<when/>`​ 要素を追加します。
2. `expression`​ 属性を設定して、このルートを実行する条件を定義します。

## [](#例)例

次の例では、​`language`​ クエリパラメーターを受け取る HTTP リスナーと、このパラメーターを評価して値に応じて対応するルートを実行する Choice ルーターを設定します。

```xml
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core"
  xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
  xmlns:spring="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
  <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
    <http:listener-connection host="0.0.0.0" port="8081" />
  </http:listener-config>
  <flow name="content-based-routingFlow" >
    <http:listener config-ref="HTTP_Listener_config" path="/" doc:name="Listener" />
    <set-variable variableName="language" value="#[attributes.queryParams.language]" doc:name="Set Variable" />
    <!-- START OF CHOICE BLOCK-->
    <choice doc:name="Choice" >
      <when expression="#[vars.language == 'Spanish']" >
        <set-payload value="Hola!" doc:name="Reply in Spanish" />
      </when>
      <when expression="#[vars.language == 'French']" >
        <set-payload value="Bonjour!" doc:name="Reply in French" />
      </when>
      <otherwise>
        <flow-ref name="reply-in-default-languageSub_Flow" doc:name="reply-in-default-languageSub_Flow" />
      </otherwise>
    </choice>
    <!-- END OF CHOICE BLOCK-->
    <logger level="INFO" doc:name="Log the reply" message='#["The reply $(payload) means hello in $(vars.language)" ]'/>
  </flow>
  <sub-flow name="reply-in-default-languageSub_Flow" >
    <logger level="INFO" doc:name="Logger" message="No language specified. Using English as a default. "/>
    <set-variable variableName="language" value="English" doc:name="Set Language to English" />
    <set-payload value="Hello!" doc:name="Reply in English" />
  </sub-flow>
</mule>
```

## [](#see-also)関連情報

- [フローおよびサブフロー](https://docs.mulesoft.com/mule-runtime/latest/about-flows)
- [データの抽出](https://docs.mulesoft.com/jp/dataweave/2.4/dataweave-cookbook-extract-data)
- [DataWeave スクリプト](https://docs.mulesoft.com/jp/dataweave/2.4/dataweave-language-introduction)
- [ビジネスイベント](https://docs.mulesoft.com/mule-runtime/latest/business-events)