我有一个非常长的String,我希望用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);
}
显示一个非常长的字符串。如果我想逐行显示String,那么我必须在一个循环中调用generateOutputWithButton()方法表单。 像这样:
for (String l : StringUtils.split(message, "\n")) {
ReportGeneratorHelper.generateOutputWithButton(contentHandler, l);
}
然而,该解决方案具有不希望的副作用。它会多次显示所有内容,整个表格。所以我需要从Java代码中删除循环,并将其放到xsl中。但我不熟悉语法,我不知道我应该在哪个迭代...
任何建议都表示赞赏。
答案 0 :(得分: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>