Search code examples
xsltnormalize-space


 is shown by XSLT


I want print attribute and put some values to it.

Input :

<figure id="fig_1">
 <dis>text</dis>
</figure>

my output:

<image ref="fig_1"
        comment="text the&#xA;                                    "/>

Tried code :

<xsl:template match="dis[parent::figure]">
    <xsl:variable name="fig_name" select="parent::fig/@id"/>
    <image ref="{$fig_name}">
        <xsl:attribute name="comment">
            <xsl:value-of select="text()"/>
        </xsl:attribute>
    </tps:image>
</xsl:template>

I want to remove all &#xA;. How can I do it.


Solution

  • use normalize-space() function to remove unnecessary white space.

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />        
        <xsl:template match="long-desc[parent::fig]">
            <xsl:variable name="fig_name" select="parent::fig/@id"/>
            <image ref="{$fig_name}">
                <xsl:attribute name="comment">
                    <xsl:value-of select="normalize-space(text())"/>
                </xsl:attribute>
            </image>
        </xsl:template>
    </xsl:stylesheet>