Search code examples
xmlstringxsltreplacefpml

XML replacement with XSL


Hi I am trying to convert an XML file of FpML 4 to FpML 5.

The only thing I have to change is the FpML header Here follows an example:

input file FpML 4

     <FpML version="4-0" xsi:type="DataDocument" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.fpml.org/2003/FpML-4-0 ../fpml-main-4-0.xsd" xmlns="http://www.fpml.org/2003/FpML-4-0">
            <trade>...</trade>
            <party id="partyA">...</party>
            <party id="partyB">...</party>
     </FpML>

Now the resulting file should look like:

     <dataDocument xmlns="http://www.fpml.org/FpML-5/confirmation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" fpmlVersion="5-0" xsi:schemaLocation="http://www.fpml.org/FpML-5/confirmation ../../fpml-main-5-0.xsd">
            <trade>...</trade>
            <party id="partyA">...</party>
            <party id="partyB">...</party>
     </dataDocument>

I tried with XSL tutorials around and nothing really helped. Any ideas anyone would be welcome.

@Update:

For now just to see it's working I tried this XSL

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

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

<xsl:template match="FpML">
  <xsl:element name="test">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

Thanks


Solution

  • This stylesheet:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:fpml4="http://www.fpml.org/2003/FpML-4-0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://www.fpml.org/FpML-5/confirmation"
     exclude-result-prefixes="fpml4">
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="fpml4:FpML">
            <dataDocument fpmlVersion="5-0"
                          xsi:schemaLocation=
             "http://www.fpml.org/FpML-5/confirmation ../../fpml-main-5-0.xsd">
                <xsl:apply-templates select="node()"/>
            </dataDocument>
        </xsl:template>
        <xsl:template match="fpml4:*">
            <xsl:element name="{local-name()}">
                <xsl:apply-templates select="node()|@*"/>
            </xsl:element>
        </xsl:template>
    </xsl:stylesheet>
    

    Output:

    <dataDocument fpmlVersion="5-0" 
     xsi:schemaLocation="http://www.fpml.org/FpML-5/confirmation ../../fpml-main-5-0.xsd" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://www.fpml.org/FpML-5/confirmation">
        <trade>...</trade>
        <party id="partyA">...</party>
        <party id="partyB">...</party>
    </dataDocument>
    

    Edit: Better with a default namespace...