XSLTマップを作成して、キー・フィールドによって関連付けられたインスタンスとの様々なソース(入力ペイロード)をループ処理できます。
ソース間の1:0..nおよび1:1の関係の例
次のビジネス・ユニットおよび従業員の例が用意されています:
- 各ビジネス・ユニットは、0..n従業員(1:0..n関係)を持つことができます。
- ビジネス・ユニットと1対1の相関関係を持つG/L勘定科目ソース。
これらを組み合せるXSLTマップを作成できます。
この例のソース(入力ペイロード)は次のとおりです:
$BusinessUnits<company>
<bu>
<id>SD</id> <name>Software Development</name>
<accounbtid>i9</accountid>
</bu>
<bu>
<id>BS</id> <name>Sales</name>
<accounbtid>i1</accountid>
</bu>
<bu>
<id>MD</id> <name>Marketing</name>
<accounbtid>i2</accountid>
</bu>
</company>
$Employees<people>
<emp> <buid>SD</buid> <name>Joe Smith</name> </emp>
<emp> <buid>SD</buid> <name>Mike Jones</name> </emp>
<emp> <buid>BS</buid> <name>Dave Johnson</name> </emp>
</people>
$GLAccounts
<gl>
<account> <id>i1</id> <number>001.345</number> </account>
<account> <id>i2</id> <number>001.477</number> </account>
<account> <id>i9</id> <number>001.223</number> </account>
</gl>
$BusinessUnitsと$Employeesの間のリンクは、ビジネス・ユニットIDです。 ヘッダーは$BusinessUnitで、詳細は$Employeesです。 GL勘定科目とビジネス・ユニットのリンクは、勘定科目IDです。
次の出力が必要です:<xxx>
<yyy>
<BU id='SD'>Software Development</BU>
<empName>Joe Smith</empName>
<accNumber>001.223</accNumber>
</yyy>
<yyy>
<BU id='SD'>Software Development</BU>
<empName>Mike Jones</empName>
<accNumber>001.223</accNumber>
</yyy>
<yyy>
<BU id='BS'>Sales</BU>
<empName>Dave Johnson</empName>
<accNumber>001.345</accNumber>
</yyy>
</xxx>
ソリューション
ソースのインスタンス(レコード)に1対1の相関関係がある場合は、述語を使用できます。
インスタンスに1:0..n相関がある場合、xsl:for-each-groupを使用すると、ソースの過剰解析が回避されるため、述語を使用するよりもパフォーマンスが向上します。
XSLTコンテンツは次のとおりです:
<?xml version = '1.0' encoding = 'UTF-8'?>
<xsl:stylesheet version="2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="BusinessUnits" />
<xsl:param name="Employees" />
<xsl:param name="GLAccounts"/>
<xsl:template match="/" >
<xxx>
<xsl:for-each-group select="$Employees/people/employee" group-by="buid">
<!-- this section will be executed only once per 'buid' -->
<!-- Store the Business Unit Record in a variable -->
<xsl:variable name="BURecord">
<xsl:copy-of select="$BusinessUnits/company/bu[id = fn:current-grouping-key()]"/>
</xsl:variable>
<!-- Store the GL Account Record in a variable -->
<xsl:variable name="GLAccountRecord">
<xsl:copy-of select="$GLAccounts/gl/account[id = $BURecord/bu/accountid]" />
</xsl:variable>
<!-- end: executed only once per 'buid' -->
<xsl:for-each select="current-group()">
<!-- iterates the employees within the current 'buid' -->
<yyy>
<BU id="{./buid}">
<xsl:value-of select="$BURecord/bu/name" />
</BU>
<empName>
<xsl:value-of select="./name" />
</empName>
<accNumber>
<xsl:value-of select="$GLAccountRecord/account/number"/>
</accNumber>
</yyy>
</xsl:for-each>
</xsl:for-each-group>
</xxx>
</xsl:template>
</xsl:stylesheet>
サマリー
- 1対1の関係がある場合、XSLTではグループを作成するためにデータをソートする必要がないため、
<xsl:for-each-group>のかわりに述語を使用する方が高速です。
- 1:0..n関係がある場合、
<xsl:for-each-group>を使用する方が述語を使用するよりも高速に実行されます。 これは、前述の例の述語では、すべての従業員ごとにビジネス・ユニット・ソースおよびGL勘定科目ソース全体が解析されるためです。