XSL and Whitespace

T

Tjerk Wolterink

Hello all,

how does xsl handle white space?
I know you can set domething like this
for nice indentation:

<xsl:eek:utput method="xhtml" indent="yes"/>

But know i have xsl code like this:

<a>
<xsl:attribute name="href">
<xsl:value-of select="$absolute_url"/>/index.php?page=<xsl:value-of
select="@page"/>
<xsl:for-each select="page:var">
&amp;<xsl:value-of select="page:name"/>=<xsl:value-of
select="page:value"/>
</xsl:for-each>
</xsl:attribute>
</a>


But that should transform to someting like this:

<a href="http://localhost/index.php?page=pages/medewerker.page.xml&id=42">
</a>

Not to someting like this:

<a
href="http://localhost/index.php?page=pages/medewerker.page.xml &amp; id =42">
</a>


How do is solve my problem??
And why is &amp not recognised? How should i define entities?
Note, i have an xsd but i do not use it to validate documents, and the
xml documents do not say wich scheme they are. So how can i define &amp;
=& without xsd?!?!
 
J

Joris Gillis

<xsl:attribute name="href">
<xsl:value-of select="$absolute_url"/>/index.php?page=<xsl:value-of
select="@page"/>
<xsl:for-each select="page:var">
&amp;<xsl:value-of select="page:name"/>=<xsl:value-of
select="page:value"/>
</xsl:for-each>
</xsl:attribute>
</a>


that should transform to someting like this:

<a href="http://localhost/index.php?page=pages/medewerker.page.xml&id=42">
</a>

Not to someting like this:

<a
href="http://localhost/index.php?page=pages/medewerker.page.xml &amp; id =42">
</a>


How do is solve my problem??

Hi,

Use something like this:
<xsl:variable name="href" >
/index.php?page=<xsl:value-of select="@page"/>
<xsl:for-each select="page:var">
&amp;<xsl:value-of select="page:name"/>=<xsl:value-of select="page:value"/>
</xsl:for-each>
</xsl:variable>
<a href="{normalize-space($href)}">
<xsl:apply-templates select="node()[not(self::page:var)]" mode="ignore"/>
And why is &amp; not recognised? How should i define entities?
Actually it is being recognised as the entity for '&'. But, when serializing the output, the processor escapes the '&' symbol back to '&amp;'. In the past I also try to deal with this, but I haven't found any solution yet. Even not with DTD's or XSD's. The XSLT 1.0 clearly states that it is illegal to disable output escaping in output nodes other than text nodes. (http://www.w3.org/TR/xslt.html#disable-output-escaping). XSLT 2.0 might bring a solution...

regards,
 
T

Tjerk Wolterink

Joris said:
<a>
<xsl:attribute name="href">
<xsl:value-of select="$absolute_url"/>/index.php?page=<xsl:value-of
select="@page"/>
<xsl:for-each select="page:var">
&amp;<xsl:value-of select="page:name"/>=<xsl:value-of
select="page:value"/>
</xsl:for-each>
</xsl:attribute>
</a>


that should transform to someting like this:

<a
href="http://localhost/index.php?page=pages/medewerker.page.xml&id=42">
</a>

Not to someting like this:

<a
href="http://localhost/index.php?page=pages/medewerker.page.xml &amp; id =42">

</a>


How do is solve my problem??


Hi,

Use something like this:
<xsl:variable name="href" >
/index.php?page=<xsl:value-of select="@page"/>
<xsl:for-each select="page:var">
&amp;<xsl:value-of select="page:name"/>=<xsl:value-of
select="page:value"/>
</xsl:for-each>
</xsl:variable>
<a href="{normalize-space($href)}">
<xsl:apply-templates select="node()[not(self::page:var)]" mode="ignore"/>
And why is &amp; not recognised? How should i define entities?

Actually it is being recognised as the entity for '&'. But, when
serializing the output, the processor escapes the '&' symbol back to
'&amp;'. In the past I also try to deal with this, but I haven't found
any solution yet. Even not with DTD's or XSD's. The XSLT 1.0 clearly
states that it is illegal to disable output escaping in output nodes
other than text nodes.
(http://www.w3.org/TR/xslt.html#disable-output-escaping). XSLT 2.0
might bring a solution...

regards,

Hey joris,

You know alot about xml xsl and xpath,
but where does your knowledge come from?

from a book?
from the internet?

I've learned xml xpath and xsl from the internet from sites like
w3schools and from the w3c specifications.

But some problems i cannot solve.

So do have advice for me to learn more about xsl and xpath?
Url or book?

Anyways, thanks for your help.
 
J

Joris Gillis

So do have advice for me to learn more about xsl and xpath?
Url or book?

I found 'inside XSLT' quite useful. But I cannot compare as I never read another book on the topic. And the book might be outdated now.

A site where all XSL and Xpath tricks are bundled would be very handy, but I never found such site.
 
M

Marrow

Hi Tjerk,

The reason you are seeing whitespace in the attribute output is because you
are using implied output text. As soon as you use implied output text -
that implied output text will also include any whitespace (inc. tabs, CRs
etc.).

You can overcome this by using explicit output text (and it's best to get
into the habit of using explicit output text), e.g.

<a>
<xsl:attribute name="href">
<xsl:value-of select="$absolute_url"/>
<xsl:text>/index.php?page=</xsl:text>
<xsl:value-of select="@page"/>
<xsl:for-each select="page:var">
<xsl:text>&amp;</xsl:text>
<xsl:value-of select="page:name"/>
<xsl:text>=</xsl:text>
<xsl:value-of select="page:value"/>
</xsl:for-each>
</xsl:attribute>
</a>

The ampersands are being correctly recognised and are being URL encoded
according to the HTML/XHTML specs.

HTH
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator
 
J

Joris Gillis

The reason you are seeing whitespace in the attribute output is because you
are using implied output text. As soon as you use implied output text -
that implied output text will also include any whitespace (inc. tabs, CRs
etc.).

You can overcome this by using explicit output text (and it's best to get
into the habit of using explicit output text), e.g.
The ampersands are being correctly recognised and are being URL encoded
according to the HTML/XHTML specs.

Hi,

Setting the output method to 'text' may solves the ampersand and the superfluous wittespaces, but the 'a' element and it's attribute will not be outputted. (http://www.w3.org/TR/xslt.html#section-Text-Output-Method)
I don't think it makes sense to output 'text' if you're actually dealing with 'html'. Moreover you'd have to have to strinyfy all element and attributes throughout the stylesheet, a really bad idea if you ask me.
If this 'hack' really is the only way around it, don't you agree that this is a shortcomming in the XSLT1.0 Recommendation?

regards,
 
M

Marrow

Hi,
Hi,

Setting the output method to 'text' may solves the ampersand and the
superfluous wittespaces, but the 'a' element and it's attribute will not be
outputted. (http://www.w3.org/TR/xslt.html#section-Text-Output-Method)
I don't think it makes sense to output 'text' if you're actually dealing
with 'html'. Moreover you'd have to have to strinyfy all element and
attributes throughout the stylesheet, a really bad idea if you ask me.
If this 'hack' really is the only way around it, don't you agree that this
is a shortcomming in the XSLT1.0 Recommendation?

I did not mention the output method at all! - merely pointed to the correct
and safe way of outputting text nodes using the <xsl:text> instruction.
This is no hack whatsoever, and there is not shortcomming in the W3 rec
regarding this issue. It is known by all seasoned XSLT programmers that the
first step towards taking firm control of whitespace (in whatever output
mode) is to avoid using implicit text node output and opt for using explicit
text node output using the <xsl:text> instruction. The <xsl:text>
instruction is there for a reason - one of which being so that explicit
output text nodes can be differentiated from whitespace text nodes in the
stylesheet that are merely there to 'prettify' the stylesheet for
readability (which might otherwise be assumed by the XSLT transformation as
text nodes for output).

I can only suggest you re-read my original posting again ;) If you read it
carefully you will notice there is no mention of the output mode and gives
clear example code on using the <xsl:text> instruction.

Cheers
Marrow

Joris Gillis said:
Hi,

Setting the output method to 'text' may solves the ampersand and the
superfluous wittespaces, but the 'a' element and it's attribute will not be
outputted. (http://www.w3.org/TR/xslt.html#section-Text-Output-Method)
I don't think it makes sense to output 'text' if you're actually dealing
with 'html'. Moreover you'd have to have to strinyfy all element and
attributes throughout the stylesheet, a really bad idea if you ask me.
If this 'hack' really is the only way around it, don't you agree that this
is a shortcomming in the XSLT1.0 Recommendation?
 
J

Joris Gillis

I can only suggest you re-read my original posting again ;) If you read it
carefully you will notice there is no mention of the output mode and gives
clear example code on using the <xsl:text> instruction.

Sorry about that, I shouldn't have sent such hastely reaction without reading your mail carefully. You're absolutely right about the 'xsl:text' element.

regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top