1. I would like to structure my XSLT a little better where I would be using multiple commands.
The design of the source XML is pretty bad, this would be a lot better if the elements were like:
<PayerPartyReference leg="one"/>
Since all the LegOne/LegTwo elements seem to be basically the same, however you using XSLT 2.0 so there's a solution to that using regex, I'd do something like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="/*">
<body>
<insertions>
<xsl:apply-templates select="Trades_Output"/>
</insertions>
</body>
</xsl:template>
<xsl:template match="Trades_Output">
<trade>
<tradeHeader>
<xsl:copy-of select="tradeId | tradeDate"/>
</tradeHeader>
<product>
<swap>
<!-- this will generate two groups for the elements starting with legOne/legTwo -->
<xsl:for-each-group select="*[matches(name(),'(legOne|legTwo)')]"
group-by="substring(name(),1,6)">
<swapStream>
<!--
this will apply to legOnePayerPartyReference and legOneReceiverPartyReference
when in group legOne and respectively when in group legTwo
-->
<xsl:apply-templates select="current-group()[ends-with(name(),'PartyReference')]"/>
<calculationPeriodDates>
<xsl:apply-templates select="current-group()[ends-with(name(),'Date')]"/>
</calculationPeriodDates>
</swapStream>
</xsl:for-each-group>
</swap>
</product>
</trade>
</xsl:template>
<xsl:template match="*[matches(name(),'(legOne|legTwo)PayerPartyReference')]">
<payerPartyReference href="{.}"/>
</xsl:template>
<xsl:template match="*[matches(name(),'(legOne|legTwo)ReceiverPartyReference')]">
<receiverPartyReference href="{.}"/>
</xsl:template>
<xsl:template match="*[matches(name(),'(legOne|legTwo)StartDate')]">
<effectiveDate>
<unadjustedDate>
<xsl:value-of select="."/>
</unadjustedDate>
<dateAdjustments>
<businessDayConvention>MODFOLLOWING</businessDayConvention>
</dateAdjustments>
</effectiveDate>
</xsl:template>
<xsl:template match="*[matches(name(),'(legOne|legTwo)EndDate')]">
<terminationDate>
<unadjustedDate>
<xsl:value-of select="."/>
</unadjustedDate>
<dateAdjustments>
<businessDayConvention>MODFOLLOWING</businessDayConvention>
</dateAdjustments>
</terminationDate>
</xsl:template>
</xsl:stylesheet>
It's unclear to me were most of the stuff in your desired Output comes from, I'm guessing lot's of it is fixed...however this should get you started.
I'm also going to process several <tradeType>
nodes (i.e. <tradeType>IRS</tradeType>
, as well as <tradeType>CDS</tradeType>
), and would need my output to be be slightly different based on that <tradeType>
value.
This totally depends on what you mean by slightly so maybe provide some more info here on what would be different for each tradeType... the simplest solutin would be to just have one template per tradeType
<xsl:template match="Trades_Output[tradeType='IRS']">
respectively
<xsl:template match="Trades_Output[tradeType='CDS']">
and then have different structures in there...