Home > Back-end >  xsl:value-of with a sequence of text nodes: no separator
xsl:value-of with a sequence of text nodes: no separator

Time:08-27

I wonder why I am not getting a separator when using xsl:value-of with a sequence of text nodes.

Here's a simple way to reproduce the issue:

XML:

<input>alpha<br/>bravo<br/>charlie</input>

XSLT:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

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

</xsl:stylesheet>

Expected output:

<?xml version="1.0" encoding="UTF-8"?>
<output>alpha bravo charlie</output>

Actual output:

<?xml version="1.0" encoding="UTF-8"?>
<output>alphabravocharlie</output>

I know that the processor recognizes that the selection is a sequence of 3 text nodes, because <xsl:value-of select="count(text())"/> returns 3 and <xsl:value-of select="text()[2]"/> returns bravo. But for some reason it does not insert a separator between the individual values.

When I use the xsl:value instruction with a sequence of elements, or even with a sequence of strings, I do get the expected separator. It's only with a sequence of text nodes that the behavior is different.

Could this be a quirk of the Saxon processor (I am using Saxon 10.6 HE)?

CodePudding user response:

Saxon 10 is an XSLT 3 processor, not an XSLT 2 processor. But the version doesn't matter, but let's look at the spec (https://www.w3.org/TR/xslt-30/#value-of): "The way in which the value is constructed is specified in 5.7.2 Constructing Simple Content.".

https://www.w3.org/TR/xslt-30/#constructing-simple-content: "Adjacent text nodes in the sequence are merged into a single text node".

So this is not a quirk of the processor, just the way the xsl:value-of element is defined.

You could use e.g. <xsl:value-of select="text()/string()"/> to achieve the result you want, or <xsl:value-of select="data(text())"/> is another.

  • Related