In the below XSLT I'm checking the following
- If element is null then replace then replace it with the value of another element.
- If attribute of that element is null then replace it with constant value.
1 is working, however 2 is not.
For 2 I have tried two things:
First, using the xsl:if condition didn't work.
It is adding a new node with the same node name instead of inserting a value to the attribute.
Second I have tried to use the template. That didn't work either. It is completely eliminating the node and adding the attribute to the parent with the value.
Also, is it possible to do it differently or a better way.
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>
input
<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>
output
<ns0:Testing>
<ns0:Cedent>
<ns0:Party>
<ns0:Id Agency="Legacy">292320710</ns0:Id>
<ns0:Name>Canada</ns0:Name>
</ns0:Party>
</ns0:Cedent>
</ns0:Testing>