strings
この一般的な変換により、要素と、生成する必要がある対応する要素名との間の個別の (別のドキュメント内または外部パラメーターとして渡されたものであっても) マッピングが可能になります。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my" exclude-result-prefixes="my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<my:mapping>
<map key="1" value="PolicyNumber"/>
<map key="2" value="CommissionFactorMultiplier"/>
<map key="3" value="PremiumValue"/>
<map key="4" value="SalesmanCommissionValue"/>
<map key="5" value="ManagerCommissionValue"/>
<map key="7" value="GL_COR"/>
<map key="8" value="GL_OPO"/>
</my:mapping>
<xsl:variable name="vMaps" select="document('')/*/my:mapping/*"/>
<xsl:template match="values">
<CalculationOutput>
<xsl:apply-templates/>
</CalculationOutput>
</xsl:template>
<xsl:template match="strings">
<xsl:if test="position()=$vMaps/@key">
<xsl:variable name="vPos" select="position()"/>
<xsl:element name="{$vMaps[@key = $vPos]/@value}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
この変換が提供された XML ドキュメントに適用されると、次のようになります。
<PricingResultsV6>
<subItems>
<SubItem>
<profiles>
<ProfileValues>
<values>
<strings>800210</strings>
<strings>THC</strings>
<strings>10.0</strings>
<strings>20.0</strings>
<strings>30.0</strings>
<strings>40.0</strings>
<strings>550.0</strings>
<strings>640.0</strings>
</values>
</ProfileValues>
</profiles>
</SubItem>
</subItems>
</PricingResultsV6>
必要な正しい結果が生成されます。
<CalculationOutput>
<PolicyNumber>800210</PolicyNumber>
<CommissionFactorMultiplier>THC</CommissionFactorMultiplier>
<PremiumValue>10.0</PremiumValue>
<SalesmanCommissionValue>20.0</SalesmanCommissionValue>
<ManagerCommissionValue>30.0</ManagerCommissionValue>
<GL_COR>550.0</GL_COR>
<GL_OPO>640.0</GL_OPO>
</CalculationOutput>
Ⅱ.このソリューションは、キーを使用して非常に効率的にすることができます:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my" exclude-result-prefixes="my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<my:mapping>
<map key="1" value="PolicyNumber"/>
<map key="2" value="CommissionFactorMultiplier"/>
<map key="3" value="PremiumValue"/>
<map key="4" value="SalesmanCommissionValue"/>
<map key="5" value="ManagerCommissionValue"/>
<map key="7" value="GL_COR"/>
<map key="8" value="GL_OPO"/>
</my:mapping>
<xsl:variable name="vMaps" select="document('')/*/my:mapping/*"/>
<xsl:key name="kValueByKey" match="@value" use="../@key"/>
<xsl:template match="values">
<CalculationOutput>
<xsl:apply-templates/>
</CalculationOutput>
</xsl:template>
<xsl:template match="strings">
<xsl:if test="position()=$vMaps/@key">
<xsl:variable name="vPos" select="position()"/>
<xsl:variable name="vCur" select="."/>
<xsl:for-each select="$vMaps/..">
<xsl:element name="{key('kValueByKey', $vPos)}">
<xsl:value-of select="$vCur"/>
</xsl:element>
</xsl:for-each>
</xsl:if>
</xsl:template>
</xsl:stylesheet>