Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove unwanted Space and returns using XSLT

Tags:

xslt

I want to remove Space and returns in between the elements only for the particular child element (i.e) for 'math' as mentioned below XML. I tried <xsl:strip-space elements="m:math"/>. But it removes space for all the elements. I am in need to retain the space after <style> element and space before <b> element.

Input XML:

<?xml version="1.0"?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
    <style>This is style</style> 
    <math display="block"> 
        <munder> 
            <mi mathvariant="normal">BASE</mi>
            <mi mathvariant="normal">under</mi> 
        </munder> 
        <mo>=</mo> 
        <munder> 
            <mrow> 
                <munder> 
                    <mi mathvariant="normal">BASE</mi> 
                    <mi mathvariant="normal">under</mi> 
                </munder> 
            </mrow> 
            <mphantom>
                <mi mathvariant="normal">under</mi>
            </mphantom> 
        </munder> 
    </math>
    <b>This is bold</b>
</chapter>

XSLT tried:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:mml="http://www.w3.org/1998/Math/MathML">
    <xsl:output method="xml" encoding="UTF-8" indent="no"/>
    <xsl:strip-space elements="*"/>

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

Required Output:

<?xml version='1.0' encoding='UTF-8'?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML"><style>This is style</style> <math display="block"><munder><mi mathvariant="normal">BASE</mi><mi mathvariant="normal">under</mi></munder><mo>=</mo><munder><mrow><munder><mi mathvariant="normal">BASE</mi><mi mathvariant="normal">under</mi></munder></mrow><mphantom><mi mathvariant="normal">under</mi></mphantom></munder></math> <b>This is bold</b></chapter>
like image 721
siva2012 Avatar asked Nov 28 '25 12:11

siva2012


1 Answers

EDIT: Though this answer isn't valid for this question I'm keeping it for reference since I have explained significance of each templates in linearize XML code.. And posting the relevant answer as new answer..


You don't need strip-space, this should do..

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="no" omit-xml-declaration="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:value-of select="normalize-space()" />
    </xsl:template>
</xsl:stylesheet>

Explanation:

<xsl:output indent="no" omit-xml-declaration="yes"/>

indent = 'no' takes care of removing addition space between two nodes .. omit-xml-declaration removes xml declaration from output XML. This is optional in your case..

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

Copies nodes as is.. except without indentation ..

<xsl:template match="text()">
    <xsl:value-of select="normalize-space()" />
</xsl:template>

Coverts unwanted whitespace characters to single-space char .. example:

<node>

    there is lots of     space
</node>

output will be like this:

<node>there is lots of space</node>
like image 134
InfantPro'Aravind' Avatar answered Dec 02 '25 05:12

InfantPro'Aravind'



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!