---
schema_version: "1"
id: ab18c576623cce064835389d9ca2d3e9f1120315908159d56e02520a07db2573
title: XML Module を使用した XSLT 変換
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/xml-module/latest/xml-xslt.md
canonical_url: https://docs.mulesoft.com/jp/xml-module/latest/xml-xslt
source_site: docs.mulesoft.com
content_type: documentation
content_hash: ca02d69e1d49b36c901097c324e731fcc582822e99b84e66e6524cd16404ffb2
description: MuleSoft Documentation Site
release: "1.4"
latest: true
---
# XML Module を使用した XSLT 変換

`<xml-module:xslt-transform>`​ 要素は入力 XML ドキュメントを受け入れ、XSL スタイルシートを使用して変換できます。

MuleSoft は XSLT 標準をサポートしていますが、XML ドキュメントを抽出して変換するツールとして DataWeave をお勧めします。

XML 入力ドキュメントの例を次に示します。

```xml
<?xml version="1.0" encoding="UTF-8"?>
<cities>
    <city name="milan"  country="italy"   pop="5"/>
    <city name="paris"  country="france"  pop="7"/>
    <city name="munich" country="germany" pop="4"/>
    <city name="lyon"   country="france"  pop="2"/>
    <city name="venice" country="italy"   pop="1"/>
</cities>
```

次のように XML 入力ドキュメントを変換できます。

```xml
<xml-module:xslt-transform>
    <xml-module:xslt><![CDATA[
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

        <xsl:template match="/">
            <table>
            <tr>
                <th>Country</th>
                <th>City List</th>
                <th>Population</th>
            </tr>
            <xsl:for-each-group select="cities/city" group-by="@country">
                <tr>
                    <td>
                        <xsl:value-of select="@country"/>
                    </td>
                    <td>
                        <xsl:value-of select="current-group()/@name" separator=", "/>
                    </td>
                    <td>
                        <xsl:value-of select="sum(current-group()/@pop)"/>
                    </td>
                </tr>
            </xsl:for-each-group>
            </table>
        </xsl:template>
    </xsl:stylesheet>
]]></xml-module:xslt>
</xml-module:xslt-transform>
```

このスクリプトは、次のドキュメントを出力します。

```xml
<table>
    <tr>
        <th>Country</th>
        <th>City List</th>
        <th>Population</th>
    </tr>
    <tr>
        <td>italy</td>
        <td>milan, venice</td>
        <td>6</td>
    </tr>
    <tr>
        <td>france</td>
        <td>paris, lyon</td>
        <td>9</td>
    </tr>
    <tr>
        <td>germany</td>
        <td>munich</td>
        <td>4</td>
    </tr>
</table>
```

この操作はデフォルトでメッセージペイロードレベルで入力ドキュメントを検索しますが、独自の入力を指定できます。たとえば、都市がペイロード内の JSON 配列であるとします。その場合、最初に JSON を XML に変換し、次に XSL 変換を実行することもできます。

```xml
<xml-module:xslt-transform>
    <xml-module:content><![CDATA[
                %dw 2.0
                input payload application/json encoding='UTF-8'
                output application/xml encoding='UTF-8'
                ---
                payload.cities
    ]]></xml-module:content>
    <xml-module:xslt><![CDATA[
        // THE XSLT
    ]]></xml-module:xslt>
</xml-module:xslt-transform>
```

## [](#xsl-スタイルシートの個別のファイルへの外部化)XSL スタイルシートの個別のファイルへの外部化

XSLT スクリプトを Mule アプリケーションに組み込まずに済むように、次のように、ファイルを使用してモジュールに渡すことができます。

```xml
<xml-module:xslt-transform>
    <xml-module:xslt>${file::xslt/cities.xslt}</xml-module:xslt>
</xml-module:xslt-transform>
```

より複雑なユースケースでは、使用するスクリプトが外的な条件に応じて異なる場合もあります。たとえば、使用する変換が ​`userId`​ に応じて異なるマルチテナントのインテグレーションを考えてみます。

```xml
<flow name="multitenantExample">
    <http:listener path="transform" allowedMethods="POST" config-ref="httpListener" /> // 1
    <file:read path="#['xslt/$(attributes.queryParam.userId).xslt']" target="xslt" />
    <xml-module:xslt-transform>
        <xml-module:xslt>#[vars.xslt]</xml-module:xslt>
    </xml-module:xslt-transform>
</flow>
```

1. フロー例は HTTP 要求によってトリガーされます。
2. この例は、クエリパラメーターとして提供される ​`userId`​ 値と、正しい XSL が含まれるファイルを読み取って変数に保存するために使用される File Connector に基づきます。
3. この例では次に、​`Read`​ 操作を使用して取得された XSL シートを参照する式を使用して変換を実行します。

HTTP Connector と File Connector は単に例として使用しており、同様のユースケースを実行するために必要なわけではありません。

## [](#コンテキストプロパティの使用)コンテキストプロパティの使用

XML Module は XSL 言語と同じように、変換にパラメーターを渡すことができるコンテキストプロパティを提供します。次の例では、​`<xml-module:context-properties/>`​ 要素に引数マップを作成する DataWeave 式が含まれています。

```xml
<xml-module:xslt-transform>
    <xml-module:xslt><![CDATA[
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
        <xsl:output method="xml"/>

        <xsl:param name="vtn" /> (1)
        <xsl:template match="/parameter">
            <param><xsl:value-of select="$vtn"/></param> (2)
        </xsl:template>
    </xsl:stylesheet>
]]></xml-module:xslt>
    <xml-module:context-properties>#[{'vtn' : 'some value'}] (3)
    </xml-module:context-properties>
</xml-module:xslt-transform>
```

1. XSL スタイルシートは ​`vtn`​ と呼ばれる ​`param`​ を宣言します。
2. スタイルシートは ​`$`​ プレフィックスを使用して ​`param`​ の割り当てられた値を参照します。
3. この例では、​`context-properties`​ パラメーターを使用して値を内部に渡します。

## [](#出力-mime-タイプ)出力 MIME タイプ

XSLT の最も一般的なユースケースは新しい XML ドキュメントの出力です。ただし、XSLT は JSON、CSV、またはテキストや数値などの他の形式でドキュメントを生成することもできます。つまり、生成された値では特定の MIME タイプが割り当てられません。​`outputMimeType`​ パラメーターを使用して MIME タイプを指定する必要があります。

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

[XML Module](https://docs.mulesoft.com/jp/xml-module/latest/)