user3766698
user3766698

Reputation: 1

Line Break XSL doesn't work

I have an XML like this:

<c id="de-4" level="file">
  <did>
    <unitid label="Cotes">1 DNG 5</unitid>
    <unittitle label="my title">some text1 <lb />some text2<lb />some text3.</unittitle>
    <unitdate label="Date" normal="2014">2014</unitdate>
  </did>

And I want to use and convert LB tag -> BR for HTML. I use an XSL stylesheet to do this:

<xsl:template name="cree_cote">
    <td align='left' valign='top' class="titres">
        <xsl:call-template name="_noeud">
            <xsl:with-param name="noeud" select="did/unittitle"/>
        </xsl:call-template>
    </td>
</xsl:template>

<xsl:template match="lb">
    <br />
</xsl:template>

<!--  le contenu d'un noeud -->
<xsl:template name="_noeud">
    <xsl:param name="noeud"/>
    <xsl:for-each select="$noeud">
        <xsl:text> </xsl:text>
    <xsl:value-of select="text()"/>
        <xsl:text> </xsl:text>
        <xsl:if test="$avec_label">
            <xsl:if test="@label">
                <span class='ead_label'>[<xsl:value-of select="@label"/>]</span>
            </xsl:if>
        </xsl:if>
    </xsl:for-each>
    <xsl:apply-templates select="$noeud/*"/>
</xsl:template>

But after converting, I have this:

<td valign="top" align="left" class="titres">some text1 some text2some text3.<br><br></td>

I don't understand why my <BR> are in the end of text.

Upvotes: 0

Views: 234

Answers (2)

Martin Honnen
Martin Honnen

Reputation: 167401

Change

<xsl:template name="cree_cote">
    <td align='left' valign='top' class="titres">
        <xsl:call-template name="_noeud">
            <xsl:with-param name="noeud" select="did/unittitle"/>
        </xsl:call-template>
    </td>
</xsl:template>

to

<xsl:template name="cree_cote">
    <td align='left' valign='top' class="titres">
        <xsl:apply-templates select="did/unittitle"/>
    </td>
</xsl:template>

then change

<xsl:template name="_noeud">
    <xsl:param name="noeud"/>
    <xsl:for-each select="$noeud">
        <xsl:text> </xsl:text>
    <xsl:value-of select="text()"/>
        <xsl:text> </xsl:text>
        <xsl:if test="$avec_label">
            <xsl:if test="@label">
                <span class='ead_label'>[<xsl:value-of select="@label"/>]</span>
            </xsl:if>
        </xsl:if>
    </xsl:for-each>
    <xsl:apply-templates select="$noeud/*"/>
</xsl:template>

to

<xsl:template match="did/unittitle">
  <xsl:text> </xsl:text>
  <xsl:apply-templates/>
  <xsl:text> </xsl:text>
  <xsl:if test="$avec_label and @label">
    <span class="ead_label">[xsl:value-of select="@label"/>]</span>
  </xsl:if>
</xsl:template>

And of course keep your template for lb elements.

Upvotes: 0

JLRishe
JLRishe

Reputation: 101652

Since we don't have access to your whole XSLT, it's hard to give you the cleanest recommendation, but the reason that your lbs are being output after the text is because you are outputting the text() before applying templates to the child nodes!

Please give this a try:

<!--  le contenu d'un noeud -->
<xsl:template name="_noeud">
    <xsl:param name="noeud"/>
    <xsl:for-each select="$noeud">
        <xsl:text> </xsl:text>
        <xsl:apply-templates />
        <xsl:text> </xsl:text>
        <xsl:if test="$avec_label and @label">
            <span class='ead_label'>[<xsl:value-of select="@label"/>]</span>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

Upvotes: 1

Related Questions