Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML + XSL -> Problem getting lines to break 1

Status
Not open for further replies.

MakeItSo

Programmer
Oct 21, 2003
3,316
DE
Hello friends,

this one should be fairly easy - yet I can't get it to work properly. I'm still quite a noob with XML.

The XML might look a bit overly split up to you. This comes from how it was produced. I generate it from my old Word documents. basically simply extract the text, identify the heading and the image in it. All set up equally...

This is what my XML looks like:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="poetry.xsl" ?>
<DOC2XML>
    <article>Aktivisten</article>
    <image>Aktivisten.jpg</image>
    <sentence>
        <text>Das ist ein Aktivist.[b]<br/>[/b]</text>
    </sentence>
    <sentence>
        <text>Sieht komisch aus. </text>
    </sentence>
...
...
Now, these "<br/>" are the crucial part. I have inserted them in places, where I need a line break. All other text not containing such breaks shall be concatenated.
So far, it already works halfway, the page is displayed just as defined in the css.
But the lines won't break.

This is how I tried to solve it in the css. This is my complete css:
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:template match="DOC2XML">
	<HTML>
  		<HEAD>
  			<LINK REL="stylesheet" TYPE="text/css" HREF="poetry.css"/>
  		</HEAD>
	</HTML>
	<xsl:apply-templates/>
</xsl:template>
<xsl:template match="article">
<h2><xsl:value-of select="."/></h2>
</xsl:template>
<xsl:template match="image">
<p><img src="{.}"/></p>
</xsl:template>
<xsl:template match="sentence">
  <xsl:apply-templates/>
</xsl:template>
<xsl:template match="text">
   <xsl:value-of select="."/>
</xsl:template>
[b]<xsl:template match="br">
  <br/>
</xsl:template>[/b]
</xsl:stylesheet>

Why don't the lines break?
Am I doing something basically wrong or am I just missing something tiny but vital?

Thanks a lot!

Cheers,
Andy

[blue]Help us, join us, participate
IAHRA - International Alliance of Human Rights Advocates[/blue]
 
The following two changes may make it easier to control your output.
Code:
<xsl:template match="text[b][COLOR=red white]()[/color][/b]">
   <xsl:value-of select="[b][COLOR=blue white]normalize-space(.)[/color][/b]"/>
</xsl:template>

The [COLOR=red white]red[/color] change is required to match properly the text nodes.

The [COLOR=blue white]blue[/color] change removes the (perhaps accidental) whitespace at the beginning and end of the text node string. It may be easier to reintroduce whitespace starting from the normalized value.

Tom Morrison
 
Main problem here is that the text node is of mixed content model. To match semantic markup, you need some extra care.
[tt]
<xsl:template match="text">
[red]<xsl:apply-templates />[/red]
</xsl:template>
[blue]<xsl:template match="text/br">
<br/>
</xsl:template>
<xsl:template match="text/text()">
<xsl:value-of select="normalize-space()" />
</xsl:template>[/blue]
[/tt]
Further notes:
[1] You can restrict templates to match further under text node if you want more control.
[2] I put reference to text in text/br and text/text() because I only want to make the matches of br and text() to be much more focus and restrictive so that there is more control.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top