Connecting Tech Pros Worldwide Help | Site Map

XSL and Whitespace

Tjerk Wolterink
Guest
 
Posts: n/a
#1: Jul 20 '05
Hello all,

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

<xsl:output 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%0a%09%09%09%09&amp;%0a%09%09%0 9%09%09%09%09%09%09id%0a%09%09%09%09%09%09%09%09=4 2">
</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?!?!
Joris Gillis
Guest
 
Posts: n/a
#2: Jul 20 '05

re: XSL and Whitespace


> <a>[color=blue]
> <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%0a%09%09%09%09&amp;%0a%09%09%0 9%09%09%09%09%09%09id%0a%09%09%09%09%09%09%09%09=4 2">
> </a>
>
>
> How do is solve my problem??[/color]

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"/>
</a>
[color=blue]
> And why is &amp; not recognised? How should i define entities?[/color]
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,

--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Tjerk Wolterink
Guest
 
Posts: n/a
#3: Jul 20 '05

re: XSL and Whitespace


Joris Gillis wrote:[color=blue][color=green]
>> <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%0a%09%09%09%09&amp;%0a%09%09%0 9%09%09%09%09%09%09id%0a%09%09%09%09%09%09%09%09=4 2">
>>
>> </a>
>>
>>
>> How do is solve my problem??[/color]
>
>
> 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"/>
> </a>
>[color=green]
>> And why is &amp; not recognised? How should i define entities?[/color]
>
> 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,
>[/color]

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.
Joris Gillis
Guest
 
Posts: n/a
#4: Jul 20 '05

re: XSL and Whitespace


> So do have advice for me to learn more about xsl and xpath?[color=blue]
> Url or book?[/color]

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.

--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Martin Honnen
Guest
 
Posts: n/a
#5: Jul 20 '05

re: XSL and Whitespace




Tjerk Wolterink wrote:

[color=blue]
> 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?[/color]

Many problems are solved, check the FAQ at
http://www.dpawson.co.uk/xsl/index.html
There is also
http://www.jenitennison.com/xslt/

--

Martin Honnen
http://JavaScript.FAQTs.com/
Marrow
Guest
 
Posts: n/a
#6: Jul 20 '05

re: XSL and Whitespace


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



"Tjerk Wolterink" <tjerk@wolterinkwebdesign.com> wrote in message
news:418387f6$0$76543$b83b6cc0@news.wanadoo.nl...[color=blue]
> Hello all,
>
> how does xsl handle white space?
> I know you can set domething like this
> for nice indentation:
>
> <xsl:output 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
>[/color]
href="http://localhost/index.php?page=pages/medewerker.page.xml%0a%09%09%09%
09&amp;%0a%09%09%09%09%09%09%09%09%09id%0a%09%09%0 9%09%09%09%09%09=42">[color=blue]
> </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?!?![/color]


Joris Gillis
Guest
 
Posts: n/a
#7: Jul 20 '05

re: XSL and Whitespace


> The reason you are seeing whitespace in the attribute output is because you[color=blue]
> 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.[/color]

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#secti...-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,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Marrow
Guest
 
Posts: n/a
#8: Jul 20 '05

re: XSL and Whitespace


Hi,
[color=blue]
> Hi,
>
> Setting the output method to 'text' may solves the ampersand and the[/color]
superfluous wittespaces, but the 'a' element and it's attribute will not be
outputted. (http://www.w3.org/TR/xslt.html#secti...-Output-Method)[color=blue]
> I don't think it makes sense to output 'text' if you're actually dealing[/color]
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.[color=blue]
> If this 'hack' really is the only way around it, don't you agree that this[/color]
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" <roac@pandora.be> wrote in message
news:opsgr3mvnfyf9v9r@news.pandora.be...[color=blue][color=green]
> > The reason you are seeing whitespace in the attribute output is because[/color][/color]
you[color=blue][color=green]
> > are using implied output text. As soon as you use implied output text -
> > that implied output text will also include any whitespace (inc. tabs,[/color][/color]
CRs[color=blue][color=green]
> > etc.).
> >
> > You can overcome this by using explicit output text (and it's best to[/color][/color]
get[color=blue][color=green]
> > 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.[/color]
>
> Hi,
>
> Setting the output method to 'text' may solves the ampersand and the[/color]
superfluous wittespaces, but the 'a' element and it's attribute will not be
outputted. (http://www.w3.org/TR/xslt.html#secti...-Output-Method)[color=blue]
> I don't think it makes sense to output 'text' if you're actually dealing[/color]
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.[color=blue]
> If this 'hack' really is the only way around it, don't you agree that this[/color]
is a shortcomming in the XSLT1.0 Recommendation?[color=blue]
>
> regards,
> --
> Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
> Ceterum censeo XML omnibus esse utendum[/color]


Joris Gillis
Guest
 
Posts: n/a
#9: Jul 20 '05

re: XSL and Whitespace


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

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,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Closed Thread