Home > Enterprise >  Add new element below existing element using xslt
Add new element below existing element using xslt

Time:01-09

I have an element Name "Dispute" and want to add new element name "Records" below the element. Eg: The current XML is in this format using ssis technology

            <root>
            <Grade></Grade>
            <Fuel>
                <Quantity/>
                <Additional/>
                <Dispute>0</Dispute>
            </Fuel>
            <Fuel>
                <Quantity/>
                <Additional/>
                <Dispute>1</Dispute>
            </Fuel>
            </root>
            
            
            Need to add new element under dispute.

            <root>
            <Grade></Grade>
            <Fuel>
                <record>1</record>
                <Quantity/>
                <Additional/>
                <Dispute>0</Dispute>
                
            </Fuel>
            <Fuel>
               <record>2</record>
                <Quantity/>
                <Additional/>
                <Dispute>1</Dispute>

            </Fuel>
            </root>

XSLT did not work to add record element with attribute value 1 and 2

            <?xml version="1.0" encoding="UTF-8"?>
            <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
               <!-- Identity transform -->
               <xsl:template match="@* | node()">
                  <xsl:copy>
                     <xsl:apply-templates select="@* | node()"/>
                  </xsl:copy>
               </xsl:template>

               <xsl:template match="Dispute">
               <Dispute>0</Dispute>
               copy-of select="."/>
                  <record>1</record>
               </xsl:template>

               <xsl:template match="Dispute">
               <Dispute>1</Dispute>
               copy-of select="."/>
                  <record>2</record>
               </xsl:template>
            </xsl:stylesheet>

CodePudding user response:

Try something like:

XSLT 1.0

<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:strip-space elements="*"/>

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

<xsl:template match="Dispute">
    <xsl:copy-of select="."/>
    <record>
        <xsl:number count="Fuel"/>
    </record>
</xsl:template>

</xsl:stylesheet>

You did not specify the exact logic for populating the added record element. The above example will use sequential numbers.


Added:

I want record element just below the fuel element

You could restate that as "just before the Quantity element" and simply replace the 2nd template with:

<xsl:template match="Quantity">
    <record>
        <xsl:number count="Fuel"/>
    </record>
    <xsl:copy-of select="."/>
</xsl:template>

Or you could change the strategy and use:

<xsl:template match="Fuel">
    <xsl:copy>
        <record>
            <xsl:number/>
        </record>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>
  •  Tags:  
  • Related