Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

xml - Trying to add line breaks inside a template in XSLT

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...