---
schema_version: "1"
id: 2c4626c9a6bf1f67100306f9a854e18d71f18a1d295dc9eeecad6d4f3c4536dd
title: Parse Template リファレンス
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/mule-runtime/4.9/parse-template-reference.md
canonical_url: https://docs.mulesoft.com/jp/mule-runtime/4.9/parse-template-reference
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 5439d148fb0cbb4ed75ad2c434756343198d282663b6807a87bd2b38f83abaed
description: MuleSoft Documentation Site
release: "4.9"
latest: false
---
# Parse Template リファレンス

Parse Template はテンプレートを処理して結果を得るために使用する Mule コンポーネントです。テンプレートはテキストとして定義され、埋め込まれた Mule 式は評価され、結果に置き換えられます。

外部ファイル参照を使用してテンプレートを設定することも、コンポーネント定義自体に埋め込むこともできます。

[例](#code_example)

## [](#parse-template-の設定)Parse Template の設定

1. Studio で、Parse Template メッセージプロセッサーをパレットからキャンバスまでドラッグします。
2. この表で説明している項目を設定します。
    
    ![エラーメッセージが表示されている Parse Template](/mulesoft/current/ja-jp/mule-runtime/4.9/_images/parse.png)
    
    <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">値</th>
<th class="tableblock halign-left valign-top">説明</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p><strong>Content (コンテンツ)</strong></p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>テンプレート文字列</p>
</div></div></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"><div class="content"><div class="paragraph">
<p><strong>Display Name (表示名)</strong></p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>Parse Template</p>
</div></div></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"><div class="content"><div class="paragraph">
<p><strong>Location (場所)</strong></p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>ファイルパス</p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>メッセージプロパティまたは変数から抽出された値を挿入するテンプレートとして Mule が使用するファイルの場所を定義します。</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p><strong>Target Variable (対象変数)</strong></p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>変数名</p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>[Target Value (対象値)] で定義された式を評価した結果を保存する変数の名前。</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p><strong>Target Value (対象値)</strong></p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>式</p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>Parse Template の実行後に評価される Mule 式。この式の結果は、[Target Variable (対象変数)] 項目で定義された名前の変数に保存されます。</p>
</div></div></td>
</tr>
</tbody>
</table>

Studio または Standalone Mule インスタンスで XML エディターを使用している場合:

- `parse-template`​ 要素をフローに追加してから、下の表に従って設定します。
    
    ```xml
    <parse-template location="users/administrator/desktop/hrweb/confirmation.html" doc:name="Parse Template"/>
    ```

`parse-template`​ 要素の属性:

<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 30%;">
<col style="width: 70%;">
</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"><div class="content"><div class="paragraph">
<p><code>content</code>​</p>
</div></div></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"><div class="content"><div class="paragraph">
<p><code>location</code>​</p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>メッセージプロパティまたは変数から抽出された値を挿入するテンプレートとして Mule が使用するファイルの場所を定義するファイルパス。</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p><code>doc:name</code>​</p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>アプリケーションのトランスフォーマーの一意な名前を表示するためにカスタマイズします。(注意: Mule スタンドアロンでは不要)。</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p><code>target</code>​</p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>Parse Template が実行された後に ​<code>targetValue</code>​ で定義された式の結果が保存される変数の名前。</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p><code>targetValue</code></p>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>Parse Template が実行された後に評価され、結果が ​<code>target</code>​ 属性で定義された名前の変数に保存される Mule 式。</p>
</div></div></td>
</tr>
</tbody>
</table>

## [](#code_example)例

この例では、Parse Template を使用してデータを HTML に動的に読み込みます。

- [データベースクエリの結果の動的処理](#ex_1)
- [HTML 行でのデータの動的な生成](#ex_2)

### [](#ex_1)データベースクエリの結果の動的処理

この例では、データベースから従業員データを HTML テンプレートに読み込む Mule アプリケーションで Parse Template を使用します。フローは、テンプレートで構築された出力をコール元に返します。

![Parse Template を使用した Listener から Error handling へのステップが表示されているフロー](/mulesoft/current/ja-jp/mule-runtime/4.9/_images/parse-template-flow.png)

#### [](#parse-template-への入力)Parse Template への入力

`http://localhost:8081/getEmployee?id=1234`​ など、従業員の ID を識別するクエリパラメーターを含む URI でフローを開始し、Select 操作で次の入力を Parse Template に返すとします。

```html
{
  "first_name": "Melba",
  "last_name": "Roy Mouton",
  "department": "Computers",
  "job_title": "Assistant Chief of Research Programs",
  "start_date": "01/01/2021",
  "employee_type": "mathematician"
}
```

#### [](#template)テンプレートコンテンツ

テンプレートでは、名前、部門、役職、開始日、および従業員種別の各値を含む項目を入力ペイロードから選択します。

```html
<html>
	<body>
		<table>
			<thead>
				<tr>
					<th>First Name</th>
					<th>Last Name</th>
					<th>Department</th>
					<th>Job Title</th>
					<th>Start Date</th>
					<th>Employee Type</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>#[payload['first_name']]</td>
					<td>#[payload['last_name']]</td>
					<td>#[payload['department']]</td>
					<td>#[payload['job_title']]</td>
					<td>#[payload['start_date']]</td>
					<td>#[payload['employee_type']]</td>
				</tr>
			</tbody>
		</table>
	</body>
</html>
```

#### [](#configuration-xml-設定-xml)Configuration XML (設定 XML)

この例の Mule 設定 XML では、Parse Template で設定 ​`location="src/main/resources/responseHtml.template"`​ を使用してテンプレートファイルを読み込みます。

```xml
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
      xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns:db="http://www.mulesoft.org/schema/mule/db"
      xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
       http://www.mulesoft.org/schema/mule/core/current/mule.xsd
       http://www.mulesoft.org/schema/mule/db
       http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
       http://www.mulesoft.org/schema/mule/http
       http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
       http://www.mulesoft.org/schema/mule/ee/core
       http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
    <db:config name="Database_Config" doc:name="Database Config" >
        <db:my-sql-connection host="localhost" port="3306" user="user"
          password="pw" database="MySQL_Data_Source" />
    </db:config>
    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config">
        <http:listener-connection host="localhost" port="8081" />
    </http:listener-config>
    <flow name="exampleTemplateFlow1" >
        <http:listener doc:name="Listener" config-ref="HTTP_Listener_config"
          path="/getEmployee"/>
        <db:select doc:name="Select" config-ref="Database_Config">
            <ee:repeatable-file-store-iterable />
            <db:sql >SELECT * FROM Employees WHERE id=#[attributes.queryParams['id']]</db:sql>
        </db:select>
        <parse-template doc:name="Parse Template"
          location="src/main/resources/responseHtml.template"/>
    </flow>
</mule>
```

#### [](#テンプレートの出力)テンプレートの出力

この​[例](#template)​では、テンプレートで構築された出力が返されます。出力は、選択された従業員データを含む HTML です。

```xml
<html>
  <body>
    <table>
      <thead>
        <tr>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Department</th>
          <th>Job Title</th>
          <th>Start Date</th>
          <th>Employee Type</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>"Melba"</td>
          <td>"Roy Mouton"</td>
          <td>"Computers"</td>
          <td>"Assistant Chief of Research Programs"</td>
          <td>"01/01/2021"</td>
          <td>"mathematician"</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>
```

### [](#ex_2)HTML 行でのデータの動的な生成

この例では、オブジェクトの配列として表される従業員レコードで Parse Template を使用します。

![Listener、Set Payload、Parse Template、Logger が含まれるフロー](/mulesoft/current/ja-jp/mule-runtime/4.9/_images/parse-template-flow2.png)

#### [](#parse-template-への入力-2)Parse Template への入力

Parse Template コンポーネントは Set Payload から次のペイロードを受け取るとします。

```html
{
	Employee: [

			{
				"first_name" : "Melba",
				"last_name" : "Roy Mouton",
				"department" : "Computers",
				"job_title" : "Assistant Chief of Research Programs",
				"start_date" : "01/01/2021",
				"employee_type" : "mathematician"

			},
			{
				"first_name" : "Annie",
				"last_name" : "Easley",
				"department" : "Software Development",
				"job_title" : "Technical Lead",
				"start_date" : "06/02/2020",
				"employee_type" : "Rocket Scientist"

			}

	]
}
```

#### [](#template2)テンプレートコンテンツ

次のテンプレートでは、DataWeave スクリプトを使用して入力配列を反復処理し、配列内のオブジェクトごとに HTML 行を作成します。

```html
<html>
	<body>
		<table>
			<thead>
				<tr>
					<th>First Name</th>
					<th>Last Name</th>
					<th>Department</th>
					<th>Job Title</th>
					<th>Start Date</th>
					<th>Employee Type</th>
				</tr>
			</thead>
			<tbody>
#[%dw 2.0
output application/java
---
payload.*Employee[0] map ((item) ->
	"<tr><td><span>" ++ (item.first_name default "") ++ "</span></td><td><span>"
	++ (item.last_name default "") ++ "</span></td><td><span>"
	++ (item.department default "") ++ "</span></td><td><span>"
	++ (item.job_title default "") ++ "</span></td><td><span>"
	++ (item.start_date default "") ++ "</span></td><td><span>"
	++ (item.employee_type default "") ++ "</span></td></tr>")
	joinBy ""]
			</tbody>
		</table>
	</body>
</html>
```

#### [](#configuration-xml-設定-xml-2)Configuration XML (設定 XML)

Mule 設定 XML では、Parse Template で設定 ​`location="/path/to/my.template"`​ を使用してテンプレートファイルを読み込みます。

```xml
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
      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:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="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://www.mulesoft.org/schema/mule/ee/core
      http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.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="exampleTemplateFlow" >
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/parseit"/>
		<set-payload doc:name="Set Payload" value='#[{
	Employee: [

			{
				"first_name" : "Melba",
				"last_name" : "Roy Mouton",
				"department" : "Computers",
				"job_title" : "Assistant Chief of Research Programs",
				"start_date" : "01/01/2021",
				"employee_type" : "mathematician"

			},
			{
				"first_name" : "Annie",
				"last_name" : "Easley",
				"department" : "Software Development",
				"job_title" : "Technical Lead",
				"start_date" : "06/02/2020",
				"employee_type" : "Rocket Scientist"

			}

	]
}]'/>
		<parse-template doc:name="Parse Template" location="/path/to/my.template">
		</parse-template>
		<logger level="INFO" doc:name="Logger" message="#[payload]"/>
	</flow>
</mule>
```

#### [](#テンプレートの出力-2)テンプレートの出力

この​[例](#template2)​では、テンプレートで構築された出力が返されます。出力は、動的に作成された行と、配列内のオブジェクトのデータを含む HTML です。

```xml
<html>
  <body>
    <table>
      <thead>
        <tr>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Department</th>
          <th>Job Title</th>
          <th>Start Date</th>
          <th>Employee Type</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><span>Melba</span></td>
          <td><span>Roy Mouton</span></td>
          <td><span>Computers</span></td>
          <td><span>Assistant Chief of Research Programs</span></td>
          <td><span>01/01/2021</span></td>
          <td><span>mathematician</span></td>
        </tr>
        <tr>
          <td><span>Annie</span></td>
          <td><span>Easley</span></td>
          <td><span>Software Development</span></td>
          <td><span>Technical Lead</span></td>
          <td><span>06/02/2020</span></td>
          <td><span>Rocket Scientist</span></td>
        </tr>
      </tbody>
    </table>
  </body>
</html>
```

## [](#特殊文字)特殊文字

Parse Template は、テンプレート内での式の使用や、これらの式でのリテラルの使用をサポートします。テンプレートは、​`\#[`​ といったシーケンスや引用符など、一部の特殊文字を認識します。これらの文字をエスケープすることで通常の文字として扱うことができます。

- `#[an-expression]`​:
    
    テンプレート内では ​`#[]`​ を使用して式を指定します。たとえば、テンプレートの HTML 要素内で ​`<td>#[upper("mulesoft")]</td>`​ という式を指定すると ​`<td>MULESOFT</td>`​ に解決されます。空白の式 (​`#[]`​) はエラーとなりますが、​`#['']`​ や ​`#[""]`​ などの空白文字列は有効な式です。
- サブ式:
    
    引用符で囲まれたサブ式を解決するには、サブ式を別の ​`#[]`​ で囲みます。たとえば、次のテンプレートスニペットでは、4 つの要素で同じサブ式 (​`upper("world")`​) が引用符の内部または外部に指定されています。最初の 2 つは同じ結果を返します。最後の 2 つは、サブ式が引用符内に指定されていますが、別の ​`#[]`​ で囲まれていないため、​`upper("world")`​ を処理せずにそのまま返します。
    
    Parse Template スニペット:
    
    ```XML
    <td>#['hello #[upper("world")]']</td>
    <td>#['hello ' ++ upper("world")]</td>
    <td>#['hello upper("world")']</td>
    <td>#['hello ++ upper("world")']</td>
    ```
    
    出力される値:
    
    ```XML
    <td>hello WORLD</td>
    <td>hello WORLD</td>
    <td>hello upper("world")</td>
    <td>hello ++ upper("world")</td>
    ```
- エスケープ文字 (​`\`​):
    
    Parse Template は、文字シーケンスの ​`#[`​ で式の開始を示します。この文字シーケンスを式として解釈せずにリテラル文字として扱うには、​`\`​ でエスケープします。たとえば、​`<td>#[</td>`​ は ​`<td>#[</td>`​ を返します。
    
    また、式の文字列に含まれる特殊文字を通常の文字として扱いたい場合があります。式に含まれる文字列内の特殊文字をエスケープするには、特殊文字の前に ​`\`​ を付けます。特殊文字の例としては、​`#[`​ のシーケンス、引用符 (​`'`​ または ​`"`​)、アポストロフィ (​`'`​)、​`$`​ などがあります。​`\#`​ や ​`[`​ は、文字列内でこれらの 2 文字が (​`\#`​ が ​`[`​ の前に来て) 連続していない限り、エスケープする必要はありません。
    
    Parse Template スニペット:
    
    ```XML
    <td>\#[</td>
    <td>#['abcd\#[-1234' ++ now() as String ++ '.log']</td>
    <td>'abc'def'</td>
    <td>#['abc\'def']</td>
    <td>"xyz"xyz"</td>
    <td>#["xyz\"xyz"]</td>
    <td>#["abc\$DEF\#ghi\[JKL]"]</td>
    ```
    
    出力される値:
    
    ```XML
    <td>#[</td>
    <td>abcd#[-12342020-07-06T17:20:10.683-07:00.log</td>
    <td>'abc'def'</td>
    <td>abc'def</td>
    <td>"xyz"xyz"</td>
    <td>xyz"xyz</td>
    <td>abc$DEF#ghi[JKL]</td>
    ```
    
    詳細は、​[「特殊文字のエスケープ」](https://docs.mulesoft.com/dataweave/latest/dataweave-language-introduction#escape-special-characters)​を参照してください。

## [](#関連情報)関連情報

[Set Variable トランスフォーマー](https://docs.mulesoft.com/jp/mule-runtime/4.9/variable-transformer-reference)