Seoirse
Seoirse

Reputation: 47

Where is the extra space coming from? (XSLT)

I'm generating hyperlinks with XSLT and I keep getting extra spaces at the end of the linked words.

My XML looks like this:

The last word needs to be a <url id="1">link</url>.

The link is concatenated, using the @id. Here's my XSLT:

<xsl:template match="//url">
    <a href="../mainsite.html{@id}"><xsl:copy-of select="."/></a>
</xsl:template>

For some reason, this generates a link on the word 'link', but it adds a space after it, even though there is no space between the url tags.

If I switch out the xsl:copy-of for a plain string the problem goes away. eg:

<xsl:template match="//url">
    <a href="../mainsite.html{@id}">string</a>
</xsl:template>

Where on earth is the extra space coming from? It's driving me mad, because any link that's followed by punctuation looks screwy. Where should I be looking at to track down the problem?

Thank you so much to anyone who can help.

Upvotes: 3

Views: 897

Answers (3)

michael.hor257k
michael.hor257k

Reputation: 117140

<xsl:template match="//url">
    <a href="../mainsite.html{@id}"><xsl:copy-of select="."/></a>
</xsl:template>

For some reason, this generates a link on the word 'link', but it adds a space after it

The problem is that you are using:

<xsl:copy-of select="."/>

where you should be using:

<xsl:value-of select="."/> 

And since your stylesheet is evidently set to indent the output, you end up with:

The last word needs to be a <a href="../mainsite.html1">
      <url id="1">link</url>
   </a>

The browser then ignores the non-HTML tags and renders the line breaks as spaces.

Upvotes: 1

SoaringMoon
SoaringMoon

Reputation: 31

The normalize-space property will remove leading and trailing spaces.

<xsl:value-of select="normalize-space(.)"/>

Upvotes: 3

MichaelJ
MichaelJ

Reputation: 39

It could be adding a line break at the end of the value. Normal behavior for xsl:copy-of is to add an ending line break which could present itself as an extra space.

Upvotes: 2

Related Questions