0

我有一个很长的字符串,我想用 xslt 显示它。xslt 代码片段是这样的:

<xsl:template match="report:rmessagewithbutton">
    <h2>Running and terminated requests</h2>
    <table border="1">
        <tr>
            <th>Name</th>
            <th>Button</th>
        </tr>
        <tr>
            <td>
                <!-- <xsl:for-each select="rmessagewithbutton"> + -->
                <message>
                    <xsl:copy-of select="." />
                </message>
                <!-- </xsl:for-each> -->
            </td>
            <td>
                <input type="submit" value="Stop Request" />
            </td>
        </tr>
    </table>

我通过以下方法用java生成xml:

public static void generateOutputWithButton(final ContentHandler contentHandler,  final String msg)  throws SAXException{
    contentHandler.startElement(REPORT_NS, "rmessagewithbutton", "report:rmessagewithbutton",  SAXHelper.NO_ATTR);
    generateIWithButton(contentHandler, msg);
    contentHandler.endElement(REPORT_NS, "rmessagewithbutton", "report:rmessagewithbutton");        
}
public static void generateIWithButton(final ContentHandler contentHandler, final String text) throws SAXException {    
    contentHandler.startElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TEXT_ELEMENT, FormsConstants.I18N_PREFIX_COLON + I18nTransformer.I18N_TEXT_ELEMENT, SAXHelper.NO_ATTR);
    SAXHelper.toSAX(contentHandler, text);
    contentHandler.endElement(I18nTransformer.I18N_NAMESPACE_URI, I18nTransformer.I18N_TEXT_ELEMENT, FormsConstants.I18N_PREFIX_COLON + I18nTransformer.I18N_TEXT_ELEMENT);
}

这显示了一个非常长的字符串。如果我希望字符串逐行显示,那么我必须在一个循环内调用 generateOutputWithButton() 方法表单。像这样的东西:

for (String l : StringUtils.split(message, "\n")) {
ReportGeneratorHelper.generateOutputWithButton(contentHandler, l);
 }

然而,该解决方案具有不希望的副作用。它多次显示所有内容,整个表格。所以我需要从Java代码中删除循环,并将其放入xsl。但我不熟悉语法,我不确定我应该迭代哪个..任何建议都值得赞赏。

4

1 回答 1

0

解决方案:一个for循环可以保留在java代码中,但只适用于子元素。

public static void generateOutputWithButton(final ContentHandler contentHandler,  final String msg)  throws SAXException{

        contentHandler.startElement(REPORT_NS, "rmessagewithbutton", "report:rmessagewithbutton",  SAXHelper.NO_ATTR);
         for (String l : StringUtils.split(msg, "\n")) {
                contentHandler.startElement(REPORT_NS, "subelement", "report:subelement",  SAXHelper.NO_ATTR);
                generateIWithButton(contentHandler, l);
                contentHandler.endElement(REPORT_NS, "subelement", "report:subelement");        
         }
        contentHandler.endElement(REPORT_NS, "rmessagewithbutton", "report:rmessagewithbutton");        
    }



<xsl:template match="report:rmessagewithbutton">
    <h2>Running and terminated requests</h2>
    <table border="1">
        <tr>
            <th>Name</th>
            <th>Button</th>
        </tr>
        <xsl:apply-templates select="report:subelement"/>
    </table>

<xsl:template match="report:subelement">
        <tr>
            <td>
                <message>
                    <xsl:copy-of select="." />
                </message>
                </td>
            <td>
                <input type="submit" value="Stop Request" />
            </td>
        </tr>
</xsl:template>
于 2013-05-17T15:31:05.327 回答