hitp83
hitp83

Reputation: 1

Transform XML to XML using XSLT transforming attributes on target XML

I have source XML that looks like this -

Currently XML looks as follows:

<FIXML xmlns="http://www.fixprotocol.org/FIXML-4-4" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Order ID="337228967" ID2="2867239" >
    <Instrmt ID="764635" Src="101" CFI="" SecTyp="Swap" SubTyp="Multi-currency IRS" >
    </Instrmt>
    <Stip Typ="TEXT" Val="ASSETALL" />
    <OrdQty Qty="250000" />
  </Order>
</FIXML>

After transformation I want it to look like this -

<FIXML xmlns="http://www.fixprotocol.org/FIXML-4-4" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Order ID="337228967" ID2="2867239" >
    <Instrmt ID="764635" Src="101" CFI="" SecTyp="Swap" SubTyp="Interest Rate Swap" >
    </Instrmt>
    <Stip Typ="TEXT" Val="ASSETALL" />
    <OrdQty Qty="250000" />
  </Order>
</FIXML>

Basically, I am looking to replace SubTyp text when SubTyp = "Multi-currency IRS" replace to SubTyp = "Interest Rate Swap". If SubType <> "Multi-currency IRS", return current value.

I am trying to us the following code but only see output same as input. I am not seeing the value replacement

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:f="fixprotocol.org/FIXML-4-4">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="f:Instrmt/@SubTyp">
        <xsl:choose>
        <xsl:when test="f:Instrmt/@SubTyp='Multi-currency IRS'">
            <xsl:text>Interest Rate Swap</xsl:text>
        </xsl:when>
    </xsl:choose>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

New to XSLT. Any help would be much appreciated.

Upvotes: 0

Views: 42

Answers (1)

Sebastien
Sebastien

Reputation: 2714

Here's how it can be done :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:f="http://www.fixprotocol.org/FIXML-4-4">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="f:Instrmt/@SubTyp[.='Multi-currency IRS']">
        <xsl:attribute name="SubTyp">Interest Rate Swap</xsl:attribute>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

See it working here : https://xsltfiddle.liberty-development.net/ehVZvvU

Upvotes: 1

Related Questions