XSLT: copy all the children of <mathml> without the m: namespace without having to create a template for each element

This is my input:

                         <mathml>
                            <m:math xmlns:m="http://www.w3.org/1998/Math/MathML">
                                <m:mrow>
                                    <m:mtext>SL depreciation expense</m:mtext>
                                    <m:mo>=</m:mo>
                                    <m:mfrac>
                                        <m:mrow>
                                            <m:mtext>cost−residual value</m:mtext>
                                        </m:mrow>
                                        <m:mrow>
                                            <m:mtext>useful life</m:mtext>
                                        </m:mrow>
                                    </m:mfrac>
                                </m:mrow>
                            </m:math>
                        </mathml>

This is expected output

            <math xmlns="http://www.w3.org/1998/Math/MathML">
            <mrow>
                <mtext>SL depreciation expense</mtext>
                <mo>=</mo>
                <mfrac>
                    <mrow>
                        <mtext>cost−residual value</mtext>
                    </mrow>
                    <mrow>
                        <mtext>useful life</mtext>
                    </mrow>
                </mfrac>
            </mrow>
        </math>

I want to copy all the children of mathml node without the m: prefix namespace without having to create a template for each element. Can you suggest any solution for this?

1 answer

  • answered 2018-05-16 05:27 msoliman

    You can use the following snippet to copy what is inside math

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

    To remove namespaces, you should create your element using xsl:element have a look to this answer that woud help you doing this.