Yuan Ray
Yuan Ray

Reputation: 23

How to add space between two different emphasis tags

I'm having a problem regarding spacing value between two different emphasis. The current output of the given xslt below is yet okay but there is no spacing between the two tags. Please help me with this as I dont know much about transformation. The detailed problem can be seen below.

Here is the input XML:

<caption>
    <content>Box</content>
      <number>1</number>
      <description>
        <em type="bold">Some text with scientic name: </em>
    <em type="bolditalic">fhadinistis</em>
      </description>
</caption>

The output is:

<cap>
    <text>Box</text>
    <num>1</num>
    <content>
        <b>Some text with scientic name:</b><b>
            <i>fhadinistis</i>
        </b>
    </content>
</cap>

Desired output should be:(notice that there is a space between the closing and opening bold tag)

<cap>
    <text>Box</text>
    <num>1</num>
    <content>
        <b>Some text with scientic name:</b> <b>
            <i>fhadinistis</i>
        </b>
    </content>
</cap>

My XSLT is:

<xsl:template match="em">
    <xsl:choose>
        <xsl:when test="@type='bolditalic'">
            <b>
                <it>
                    <xsl:apply-templates/>
                </it>
            </b>
        </xsl:when>
        <xsl:when test="@type='boldunderline'">
            <b>
                <ul>
                    <xsl:apply-templates/>
                </ul>
            </b>
        </xsl:when>
        <xsl:when test="@type='italicunderline'">
            <it>
                <ul>
                    <xsl:apply-templates/>
                </ul>
            </it>
        </xsl:when>                 
    </xsl:choose>
</xsl:template>

Upvotes: 2

Views: 1394

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

Just put this at the start of the template:

    <xsl:if test="preceding-sibling::*[1][self::em]">
      <xsl:text> </xsl:text>
    </xsl:if>

Upvotes: 1

Rodders
Rodders

Reputation: 2435

Wherever you need a blank space, you could try using:

<xsl:text>#x20;</xsl:text>

even

<xsl:text> </xsl:text>

should do the trick but it's easy to miss blank spaces in code, better to use the #x20; for visibility.

You could put it after the xsl:choose close tag. So even if xsl:choose = nothing and you get multiple black spaces, HTML will only display 1 (by default)

Implement it as:

<xsl:template match="em">
    <xsl:choose>
        <xsl:when test="@type='bolditalic'">
            <b>
                <it>
                    <xsl:apply-templates/>
                </it>
            </b>
        </xsl:when>
        <xsl:when test="@type='boldunderline'">
            <b>
                <ul>
                    <xsl:apply-templates/>
                </ul>
            </b>
        </xsl:when>
        <xsl:when test="@type='italicunderline'">
            <it>
                <ul>
                    <xsl:apply-templates/>
                </ul>
            </it>
        </xsl:when>                 
    </xsl:choose>
    <xsl:text>#x20;</xsl:text>
</xsl:template>

Upvotes: 0

Related Questions