Newbie: How to avoid newline after template processing?

J

J Bondo

Hi again,

I'm having some issues with my stylesheet that converts from DocBook
XML to plist XML.

I need to do some simple HTML translation. For example, I'd like this
input:

<para>This is a <glossterm>term</glossterm>, and this is not.</para>

to become:

<p>This is a <dfn>term</dfn>, and this is not.</p>

With my stylesheet, however, it becomes:

<p>This is a
<dfn>term</dfn>
, and this is not.

The problem is the newline after the </dfn>. When rendering the HTML,
it causes whitespace between the term and the comma.

My style sheet looks like this:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:db="http://docbook.org/ns/docbook"
exclude-result-prefixes="db"
version="1.0">

<xsl:eek:utput encoding="UTF-8" indent="yes" method="xml"
doctype-public="-//Apple//DTD PLIST 1.0//EN"
doctype-system="http://www.apple.com/DTDs/PropertyList-1.0.dtd"/>

<xsl:strip-space elements="*"/>

<xsl:template match="/">
<plist version="1.0">
<dict>
<xsl:apply-templates select="db:book/db:info"/>
<key>Chapters</key>
<array>
<xsl:apply-templates select="db:book/db:chapter"/>
</array>
</dict>
</plist>
</xsl:template>
....
<xsl:template match="db:para">
<p><xsl:apply-templates/></p>
</xsl:template>
....
<xsl:template match="db:glossterm">
<dfn><xsl:value-of select="."/></dfn>
</xsl:template>
....
<xsl:template match="db:*">
</xsl:template>

</xsl:stylesheet>

Thank you for your time,
Joachim
 
M

Martin Honnen

J said:
I'm having some issues with my stylesheet that converts from DocBook
XML to plist XML.

I need to do some simple HTML translation. For example, I'd like this
input:

<para>This is a <glossterm>term</glossterm>, and this is not.</para>

to become:

<p>This is a <dfn>term</dfn>, and this is not.</p>

With my stylesheet, however, it becomes:

<p>This is a
<dfn>term</dfn>
, and this is not.

That looks odd. Which XSLT processor do you use?
The problem is the newline after the </dfn>. When rendering the HTML,
it causes whitespace between the term and the comma.

My style sheet looks like this:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:db="http://docbook.org/ns/docbook"
exclude-result-prefixes="db"
version="1.0">

<xsl:eek:utput encoding="UTF-8" indent="yes" method="xml"
doctype-public="-//Apple//DTD PLIST 1.0//EN"
doctype-system="http://www.apple.com/DTDs/PropertyList-1.0.dtd"/>

You could try with
indent="no"
obviously, at least to check whether the unwanted white space is
inserted by the processor in an attempt to indent.
 
J

J Bondo

Hi Martin,

Thanks for your quick reply. See below...

That looks odd. Which XSLT processor do you use?

xsltproc (libxml 20616, libxslt 10112 and libexslt 810) on Mac OS X
10.5.6.
You could try with
    indent="no"
obviously, at least to check whether the unwanted white space is
inserted by the processor in an attempt to indent.

I tried that, but it only compresses the XML tags, not my embedded
HTML tags. One thing perhaps worth noting is that I output the HTML
tags as &lt;tag&gt; and not <tag>, because the HTML is stored within
XML elements.

Thanks,
Joachim
 
M

Martin Honnen

J said:
xsltproc (libxml 20616, libxslt 10112 and libexslt 810) on Mac OS X
10.5.6.

You might want to try a different one, just to compare the results.
I tried that, but it only compresses the XML tags, not my embedded
HTML tags. One thing perhaps worth noting is that I output the HTML
tags as &lt;tag&gt; and not <tag>, because the HTML is stored within
XML elements.

But then your earlier description
For example, I'd like this
input:

<para>This is a <glossterm>term</glossterm>, and this is not.</para>

to become:

<p>This is a <dfn>term</dfn>, and this is not.</p>

and the corresponding templates
<xsl:template match="db:para">
<p><xsl:apply-templates/></p>
</xsl:template>
...
<xsl:template match="db:glossterm">
<dfn><xsl:value-of select="."/></dfn>
</xsl:template>

don't match what you describe in your second post. I don't see any
embedded, escaped HTML there.
 
J

J Bondo

But then your earlier description





and the corresponding templates


don't match what you describe in your second post. I don't see any
embedded, escaped HTML there.

Sorry, I tried to keep things as simple as possible. The input is:

<para>This is a <glossterm>term</glossterm>, and this is not.</para>

The template is:

<xsl:template match="db:para">
&lt;p&gt;<xsl:apply-templates/>&lt;/p&gt;
</xsl:template>
...
<xsl:template match="db:glossterm">
&lt;dfn&gt;<xsl:value-of select="."/>&lt;/dfn&gt;
</xsl:template>

Output is:

&lt;p&gt;This is a
&lt;dfn&gt;term&lt;/dfn&gt;
, and this is not.&lt;/p&gt;

Both input and output validate as valid XML.

Joachim
 
P

P. Lepin

J said:
Sorry, I tried to keep things as simple as possible. The input is:

<para>This is a <glossterm>term</glossterm>, and this is not.</para>

The template is:

<xsl:template match="db:para">
&lt;p&gt;<xsl:apply-templates/>&lt;/p&gt;
</xsl:template>
...
<xsl:template match="db:glossterm">
&lt;dfn&gt;<xsl:value-of select="."/>&lt;/dfn&gt;
</xsl:template>

Use <xsl:text> to explicitly denote the text nodes you're
creating. As it is, you're creating nodes containing the desired
text, plus whitespace and linebreaks.
 
J

J Bondo

Use <xsl:text> to explicitly denote the text nodes you're
creating. As it is, you're creating nodes containing the desired
text, plus whitespace and linebreaks

That probably explains why I couldn't find any solution to this on the
Internet. It's simply too straightforward. By enclosing my tags like
this, <xsl:text>&lt;tag&gt;</xsl:text>, it works as expected.

Thank you!

Joachim
 
J

Joe Kesselman

J said:
That probably explains why I couldn't find any solution to this on the
Internet. It's simply too straightforward. By enclosing my tags like
this, <xsl:text>&lt;tag&gt;</xsl:text>, it works as expected.

Basically, the rule in XML stylesheets is that pure whitespace is
discarded... but if it's next to literal text, it's considered part of
that text and kept. As noted, <xsl:text/> can be used as a way of making
that more explicit, as can the xml:space attribute on the enclosing
element. You can also use the xsl:strip-space and xsl:preserve-space
directive to indicate how whitespace in source documents should be
handled. See http://www.w3.org/TR/xslt#strip
 

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,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top