XSL and java.lang.transformer

A

Angus Parvis

Hi,

I've already posted this in comp.lang.java.programmer, but it doesn't
seem like the ppl there can help. I hope you guys here know more about
it, altough my question is more java than xml related ;)

Here's the problem:

I have a XSL Stylesheet. Using the java.xml.transform.Transformer I
transform XML to HTML. My XSL contains spaces and entities like
"©".

This code for example contains a space between text1 and text2:

<xsl:value-of select="$text1"/> <xsl:value-of select="$text2"/>

But after transformation the space is gone! If there was any space
character in text1 or text2, it's still there, but the space between
those two disappered. What am I doing wrong? I don't want my text to
stick together ..

The second this is a problem with entities. Whenever I use an entity
like "&copy" or "&nbsp;", java.xml.transform.Transformer tells me, that
this entity is unknown and refuses work until i remove the entity from
the HTML code. What's the problem here? How can I use HTML entities
without troubles?

Thanks for your help,

Angus
 
D

Derek Harmon

Angus Parvis said:
seem like the ppl there can help. I hope you guys here know more about
it, altough my question is more java than xml related ;)

It may surprise you to learn your question(s) are entirely
XML-related. :)
Here's the problem: : :
This code for example contains a space between text1 and text2:

<xsl:value-of select="$text1"/> <xsl:value-of select="$text2"/>

But after transformation the space is gone!

Most whitespace between elements in the stylesheet is insignificant,
unless you direct otherwise. Two options:

1. Add xml:space="preserve" to the xsl:stylesheet element of your
stylesheet so that it resembles the following example:

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xml:space="preserve" >

This may preserve more space than you want. Whitespace in
in your stylesheet is more-often than not present to aid clarity
and readability, and normally this is undesirable in the output.

2. Use an <xsl:text> element. Whitespace in an <xsl:text> element
is significant. Here is an example of preserving 3 spaces between
two book titles:

<xsl:value-of select="//book[1]/@title" /><xsl:text> </xsl:text><xsl:value-of select="//book[2]/@title" />

: :
The second this is a problem with entities. Whenever I use an entity
like "&copy" or "&nbsp;", java.xml.transform.Transformer tells me, that
this entity is unknown and refuses work until i remove the entity

These entities are unknown to the stylesheet. Tell the stylesheet
what their definitions are in the DTD internal subset of your style-
sheet, as in the following example:

<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

Alternately, you can use the character entity reference,  ,
directly within the content of the stylesheet and dispense with the
DTD internal subset altogether.


Derek Harmon
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top