Question

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.

Was it helpful?

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"

OTHER TIPS

I think you can try:

everything on a single line (quick and dirty):

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

or use tags like this (best practice):

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

Possibly the original XML soure contains these newlines (indentation), try something like:

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

Perhaps an implementation specific bug?

Using xsltproc all the above work as expected, although the expected results of bare newline+whitespace+quote+date+quote+newline+space is for the /external/ white-space to be copied as well. All the other examples produce the same 13 bytes, including a trailing newline.

Using libxml 20706, libxslt 10124 and libexslt 813 xsltproc was compiled against libxml 20701, libxslt 10124 and libexslt 813 libxslt 10124 was compiled against libxml 20701 libexslt 813 was compiled against libxml 20701

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top