以下の XSLT では、次のことを確認しています
- 要素が null の場合は、それを別の要素の値に置き換えます。
- その要素の属性が null の場合は、定数値に置き換えます。
1 は機能しますが、2 は機能しません。
2については、次の2つのことを試しました:
まず、xsl:if
条件を使用してもうまくいきませんでした。属性に値を挿入する代わりに、同じノード名で新しいノードを追加しています。
次に、テンプレートを使用しようとしました。それもうまくいきませんでした。ノードを完全に削除し、値を持つ親に属性を追加します。
また、別の方法またはより良い方法で行うことは可能ですか。
XSLT
<xsl:template match="//ns0:Cedent/ns0:Party/ns0:Id">
<xsl:if test="//ns0:Cedent/ns0:Party/ns0:Id = ''">
<xsl:copy>
<xsl:value-of select="//ns0:Broker/ns0:Party/ns0:Id"/>
</xsl:copy>
</xsl:if>
<!--<xsl:if test="//ns0:Cedent/ns0:Party/ns0:Id[@Agency = '']">
<xsl:copy>
<xsl:attribute name="Agency">Legacy</xsl:attribute>
<xsl:value-of select="'Legacy'"/>
</xsl:copy>
</xsl:if>-->
</xsl:template>
<xsl:template match="//ns0:Cedent/ns0:Party/ns0:Id[@Agency = '']">
<xsl:attribute name="Agency">Legacy</xsl:attribute>
</xsl:template>
入力
<ns0:Testing>
<ns0:Cedent>
<ns0:Party>
<ns0:Id Agency=""></ns0:Id>
<ns0:Name>Canada</ns0:Name>
</ns0:Party>
</ns0:Cedent>
<ns0:Broker>
<ns0:Party>
<ns0:Id Agency="Legacy">292320710</ns0:Id>
<ns0:Name>Spain</ns0:Name>
</ns0:Party>
</ns0:Broker>
</ns0:Testing>
出力
<ns0:Testing>
<ns0:Cedent>
<ns0:Party>
<ns0:Id Agency="Legacy">292320710</ns0:Id>
<ns0:Name>Canada</ns0:Name>
</ns0:Party>
</ns0:Cedent>
</ns0:Testing>