Trying to add line breaks inside a template in XSL

2019-09-21 11:00发布

I have an XSLT file that is rendering an HTML page, I am trying to split the string by using ; as the delimiter and then adding line breaks every EVEN break. I have my code below, it doesn't seem to work, the line breaks don't appear:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<td width="50%" nowrap="nowrap">
 <xsl:call-template name="split-parent" />
</td>

....

 <xsl:template match="STUDENT_DETAILS/PARENT" name ="split-parent">
    <xsl:variable name="splitParentsVar">
        <xsl:call-template name="add-line-breaks">
            <xsl:with-param name="stringToBreak" select="STUDENT_DETAILS/PARENT"/>
            <xsl:with-param name="isEven" select="0"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="$splitParentsVar"/>
</xsl:template>
<xsl:template name="add-line-breaks">
    <xsl:param name="stringToBreak"/>
    <xsl:param name ="isEven" />
    <xsl:if test ="$isEven='1'">
        <xsl:value-of select="concat($stringToBreak,'&#xa;')"/>
        <xsl:if test="substring-after($stringToBreak,';')!=''">
            <xsl:call-template name="add-line-breaks">
                <xsl:with-param name="stringToBreak" select="substring-after($stringToBreak,';')"/>
                <xsl:with-param name="isEven" select="0"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:if>
    <xsl:if test="$isEven='0'">
        <xsl:value-of select="$stringToBreak"/>
        <xsl:if test="substring-after($stringToBreak,';')!=''">
            <xsl:call-template name="add-line-breaks">
                <xsl:with-param name="stringToBreak" select="substring-after($stringToBreak,';')"/>
                <xsl:with-param name="isEven" select="1"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:if>
</xsl:template>
....

An example of the input would be:

George Aaron; Susan Lee Aaron; Richard Elliot Aaron; Albert Smith; Carry Johnson

output would be something like:

George Aaron; Susan Lee Aaron;
Richard Elliot Aaron; Albert Smith;
Carry Johnson

The input XML looks something like this:

<NewDataSet>
    <REPORT_OPTIONS>
        <RANK_RUN>12/04/2013</RANK_RUN>
        <PRNT_WGHT_AVGE>False<PRNT_WGHT_RANK>
    </REPORT_OPTIONS>
    <STUDENT_DETAILS>
        <STUD_PK>1590</STUD_PK>
        <STUD_NAME>Robert SMith</STUD_NAME>
        <PARENT>jubju Aaron; Susan Lee Aaron; Richard Elliot Aaron; Carl Smith</PARENT>
    </STUDENT_DETAILS>
</NewDataSet>

I want to modify the <PARENT> tag so that every two parents there is a line break that will be rendered in HTML (Whatever the best way to do this is).

标签: xml xslt
2条回答
闹够了就滚
2楼-- · 2019-09-21 11:47

Its an incomplete Input and Output XML. But the primary issue is that HTML and XHTML doesn't adhere to line breaks.

This code in XHTML:

<span>Hello


World

!</span>

Would be rendered as:

Hello World !

To insert a line break in HTML, you'll need to output a <br/> tag where you want line breaks in the output page.

So you'd need to change this line:

<xsl:value-of select="concat($stringToBreak,'&#xa;')"/>

to this:

<xsl:value-of select="$stringToBreak"/><br/>
查看更多
Emotional °昔
3楼-- · 2019-09-21 11:57

Carried over from:
hInserting line-break into XML so that it appears after XSL rendering in VB.NET

You are (again) making it very difficult to help you, because there is no context - like where does the table begin, for example. Anyway, try fitting these two templates into your stylesheet:

<xsl:template match="STUDENT_DETAILS/PARENT">
    <td>
        <xsl:call-template name="break_even">
            <xsl:with-param name="string" select="."/>
            <xsl:with-param name="delimiter" select="';'"/>
        </xsl:call-template>
    </td>
</xsl:template>


<xsl:template name="break_even">
    <xsl:param name="string"/>
    <xsl:param name="delimiter" select="','"/>

    <xsl:choose>
        <xsl:when test="
        contains($string, $delimiter) 
        and 
        contains(substring-after($string, $delimiter), $delimiter)">
            <xsl:value-of select="concat(
            substring-before($string, $delimiter), $delimiter, 
            substring-before(substring-after($string, $delimiter), $delimiter), $delimiter)"/>
            <br/>
            <xsl:call-template name="break_even">
                <xsl:with-param name="string" select="substring-after(substring-after($string, $delimiter),$delimiter)" />
                <xsl:with-param name="delimiter" select="$delimiter" />
            </xsl:call-template>
        </xsl:when>

        <xsl:otherwise>
            <xsl:value-of select="$string"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
查看更多
登录 后发表回答