Search code examples
xsltline-breaksvalue-of

XSLT <xsl: value-of> generates extra line-breaks


In XSLT, using , it generates a line break before the rendered value and another one after it. Here comes an example:

<xsl:when test="name(.) = 'Item'">
     "<xsl:value-of select="./Item/Data[last()]/text()"/>"
</xsl:when>

And the rendered result is:


                                                   "
                                             09/07/2012
"

As you can see, it puts two line breaks before and after the result value, while the desired result is:

"09/07/2012"

The original input is :

Here comes the original input, sorry for that.

                                      <Item>
                                         <Item>
                                            <Data>105</Data>
                                            <Data>09/07/2012</Data>
                                         </Item>
                                      </Item>

I'm executing this XSLT within an Oracle Server Bus

Any help will be appreciated.


Solution

  • The extra space is could also be coming from the selected text. Use normalize-space() to remove this.

    <xsl:value-of select="normalize-space(./Item/Data[last()]/text())"/>
    

    Edit Overnuts is correct in using <xsl:text> around the quotes, otherwise the Xslt processor will preserve the newline before the opening / after the closing quotes. However, I still can't see why a newline could get in between the quotes and your xsl:value-of?

    I've tried the following

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:template match="/xml" xml:space="default">
            <xsl:apply-templates select="*" />
        </xsl:template>
    
        <xsl:template match="*" xml:space="default">
            <xsl:choose>
                <xsl:when test="name(.) = 'Item'">
                    <xsl:text>"</xsl:text>
                    <xsl:value-of select="normalize-space(./Item/Data[last()]/text())"/>
                    <xsl:text>"</xsl:text>
                </xsl:when>
            </xsl:choose>
        </xsl:template>
    
    </xsl:stylesheet>
    

    When run with this XML:

    <xml>
        <Item>
            <Item>
                <Data>105</Data>
                <Data>09/07/2012</Data>
            </Item>
        </Item>
    </xml>
    

    Produces "09/07/2012"