有没有一种方法可以附加到文本文件中的当前项目而不是写入 XSLT 中的换行符?
Is there a way to append to current item in a text file rather than writing to a newline in XSLT?
我有一些正在生成 wiki 标记的 XSLT:
||Host / SIM||||User CPU - Avg||User CPU - 95th% tile||System CPU - Avg||System CPU - 95th% tile||
|dub-001544-vm01
|23.88
|60.87
|9.83
|13.8
上面的标记生成了一个 table,但是它的格式应该是这样的:
||Host / SIM||||User CPU - Avg||User CPU - 95th% tile||System CPU - Avg||System CPU - 95th% tile||
|dub-001544-vm01 |23.88 |60.87 |9.83 |13.8
所有单元格条目应位于同一行。生成第一个单元格项 (dub-001544-vm01) 的 XSLT 是:
<xsl:template name="populateHostSim">
<xsl:param name="action" />
<xsl:param name="mode" />
<xsl:param name="currentHost" />
<xsl:param name="currentResult" />
<xsl:message>In populateHostSim, with mode: <xsl:value-of select="$mode"/></xsl:message>
<xsl:message>In populateHostSim, with current host: <xsl:value-of select="$currentHost"/></xsl:message>
<xsl:choose>
<xsl:when test="/summary/results[@count eq $currentResult]">
<xsl:if test="$action='header'">
<xsl:choose>
<xsl:when test="$mode='html'">
<th>
<b>Host / SIM</b>
</th>
</xsl:when>
<xsl:otherwise>
<xsl:text>||Host / SIM||</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
<xsl:if test="$currentHost">
<xsl:choose>
<xsl:when test="$mode='html'">
<td>
<xsl:call-template name="populateSimId">
<xsl:with-param name="currentSimResult" select="$currentResult" />
<xsl:with-param name="currentSimHost" select="$currentHost" />
</xsl:call-template>
<b>
<xsl:value-of select="$currentHost" />
</b>
</td>
</xsl:when>
<xsl:otherwise>
TD: |<xsl:value-of select="$currentHost" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:when>
</xsl:choose>
</xsl:template>
那么实际数字(|23.88 |60.87 |9.83 |13.8)是用不同的模板完成的:
<xsl:template name="cpu">
<xsl:param name="action" />
<xsl:param name="mode" />
<xsl:param name="currentHost" />
<xsl:param name="currentResult" />
<xsl:if test="$action='header'">
<xsl:call-template name="populateCpuHeader">
<xsl:with-param name="mode" select="$mode" />
</xsl:call-template>
</xsl:if>
<xsl:choose>
<xsl:when test="/summary/results[@count eq $currentResult]/server">
<xsl:variable name="PATH"
select="/summary/results[@count eq $currentResult]/server[hostname eq $currentHost]/hardwareStats" />
<xsl:for-each
select="distinct-values(/summary/results[@count eq $currentResult]/server[child::hardwareStats]/hardwareStats/cpuStats/@type)">
<xsl:variable name="type" select="." />
<xsl:variable name="avg"
select="$PATH/cpuStats[@type eq $type]/avg/text()" />
<xsl:variable name="percentile"
select="$PATH/cpuStats[@type eq $type]/percentile/text()" />
<xsl:if test="$action='data'">
<xsl:choose>
<xsl:when test="$mode='html'">
<td>
<xsl:value-of select="$avg" />
</td>
<td>
<xsl:value-of select="$percentile" />
</td>
</xsl:when>
<xsl:otherwise>
| <xsl:value-of select="$avg" />
| <xsl:value-of select="$percentile" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:if test="$action='data'">
<xsl:call-template name="fillCpuTableCellWithEmptyTag" />
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
目前我在单元格条目前面插入 'TD:',这样我可以稍后在 Java 中提取它并重新格式化,然后再将其推送到 Confluence:
List<String> lines = Files.readAllLines(Paths.get("summary.markup"), Charset.defaultCharset());
StringBuilder b = new StringBuilder();
StringBuilder rowBuilder = new StringBuilder();
for(int i=0; i < lines.size(); i++) {
System.out.println("Line item: " + lines.get(i));
if(lines.get(i).trim().startsWith("TD:")) {
String item = Arrays.asList(lines.get(i).split(":")).get(1).trim();
System.out.println("Item:" + item);
rowBuilder.append(item);
} else {
b.append(String.format("%s%s", lines.get(i), "\r\n"));
}
}
rowBuilder.append("|");
b.append(String.format("%s%s",rowBuilder, "\r\n"));
page.setContent(b.toString());
随着 table 变得更加复杂,这将变得混乱。如果我能用 XSLT 做就更好了。
一如既往地非常感谢您的想法:)
如果您使用
编写 XSLT 代码
<xsl:otherwise>
TD: |<xsl:value-of select="$currentHost" />
</xsl:otherwise>
或
<xsl:otherwise>
| <xsl:value-of select="$avg" />
| <xsl:value-of select="$percentile" />
</xsl:otherwise>
然后是空格和换行符,例如
|
将被输出。您可以通过显式使用 xsl:text
来更好地控制文本,如
<xsl:otherwise>
<xsl:text>TD: |</xsl:text>
<xsl:value-of select="$currentHost"/>
</xsl:otherwise>
作为替代方案,当您使用 XSLT 2.0 时 value-of
允许输出序列,您可以尝试例如<xsl:value-of select="'|', $avg, '|', $percentile"/>
.
最后,在 XSLT 1.0 和 2.0 中您都可以使用 concat
,例如<xsl:value-of select="concat('TD: |', $currentHost)"/>
.
我有一些正在生成 wiki 标记的 XSLT:
||Host / SIM||||User CPU - Avg||User CPU - 95th% tile||System CPU - Avg||System CPU - 95th% tile||
|dub-001544-vm01
|23.88
|60.87
|9.83
|13.8
上面的标记生成了一个 table,但是它的格式应该是这样的:
||Host / SIM||||User CPU - Avg||User CPU - 95th% tile||System CPU - Avg||System CPU - 95th% tile||
|dub-001544-vm01 |23.88 |60.87 |9.83 |13.8
所有单元格条目应位于同一行。生成第一个单元格项 (dub-001544-vm01) 的 XSLT 是:
<xsl:template name="populateHostSim">
<xsl:param name="action" />
<xsl:param name="mode" />
<xsl:param name="currentHost" />
<xsl:param name="currentResult" />
<xsl:message>In populateHostSim, with mode: <xsl:value-of select="$mode"/></xsl:message>
<xsl:message>In populateHostSim, with current host: <xsl:value-of select="$currentHost"/></xsl:message>
<xsl:choose>
<xsl:when test="/summary/results[@count eq $currentResult]">
<xsl:if test="$action='header'">
<xsl:choose>
<xsl:when test="$mode='html'">
<th>
<b>Host / SIM</b>
</th>
</xsl:when>
<xsl:otherwise>
<xsl:text>||Host / SIM||</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
<xsl:if test="$currentHost">
<xsl:choose>
<xsl:when test="$mode='html'">
<td>
<xsl:call-template name="populateSimId">
<xsl:with-param name="currentSimResult" select="$currentResult" />
<xsl:with-param name="currentSimHost" select="$currentHost" />
</xsl:call-template>
<b>
<xsl:value-of select="$currentHost" />
</b>
</td>
</xsl:when>
<xsl:otherwise>
TD: |<xsl:value-of select="$currentHost" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:when>
</xsl:choose>
</xsl:template>
那么实际数字(|23.88 |60.87 |9.83 |13.8)是用不同的模板完成的:
<xsl:template name="cpu">
<xsl:param name="action" />
<xsl:param name="mode" />
<xsl:param name="currentHost" />
<xsl:param name="currentResult" />
<xsl:if test="$action='header'">
<xsl:call-template name="populateCpuHeader">
<xsl:with-param name="mode" select="$mode" />
</xsl:call-template>
</xsl:if>
<xsl:choose>
<xsl:when test="/summary/results[@count eq $currentResult]/server">
<xsl:variable name="PATH"
select="/summary/results[@count eq $currentResult]/server[hostname eq $currentHost]/hardwareStats" />
<xsl:for-each
select="distinct-values(/summary/results[@count eq $currentResult]/server[child::hardwareStats]/hardwareStats/cpuStats/@type)">
<xsl:variable name="type" select="." />
<xsl:variable name="avg"
select="$PATH/cpuStats[@type eq $type]/avg/text()" />
<xsl:variable name="percentile"
select="$PATH/cpuStats[@type eq $type]/percentile/text()" />
<xsl:if test="$action='data'">
<xsl:choose>
<xsl:when test="$mode='html'">
<td>
<xsl:value-of select="$avg" />
</td>
<td>
<xsl:value-of select="$percentile" />
</td>
</xsl:when>
<xsl:otherwise>
| <xsl:value-of select="$avg" />
| <xsl:value-of select="$percentile" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:if test="$action='data'">
<xsl:call-template name="fillCpuTableCellWithEmptyTag" />
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
目前我在单元格条目前面插入 'TD:',这样我可以稍后在 Java 中提取它并重新格式化,然后再将其推送到 Confluence:
List<String> lines = Files.readAllLines(Paths.get("summary.markup"), Charset.defaultCharset());
StringBuilder b = new StringBuilder();
StringBuilder rowBuilder = new StringBuilder();
for(int i=0; i < lines.size(); i++) {
System.out.println("Line item: " + lines.get(i));
if(lines.get(i).trim().startsWith("TD:")) {
String item = Arrays.asList(lines.get(i).split(":")).get(1).trim();
System.out.println("Item:" + item);
rowBuilder.append(item);
} else {
b.append(String.format("%s%s", lines.get(i), "\r\n"));
}
}
rowBuilder.append("|");
b.append(String.format("%s%s",rowBuilder, "\r\n"));
page.setContent(b.toString());
随着 table 变得更加复杂,这将变得混乱。如果我能用 XSLT 做就更好了。
一如既往地非常感谢您的想法:)
如果您使用
编写 XSLT 代码 <xsl:otherwise>
TD: |<xsl:value-of select="$currentHost" />
</xsl:otherwise>
或
<xsl:otherwise>
| <xsl:value-of select="$avg" />
| <xsl:value-of select="$percentile" />
</xsl:otherwise>
然后是空格和换行符,例如
|
将被输出。您可以通过显式使用 xsl:text
来更好地控制文本,如
<xsl:otherwise>
<xsl:text>TD: |</xsl:text>
<xsl:value-of select="$currentHost"/>
</xsl:otherwise>
作为替代方案,当您使用 XSLT 2.0 时 value-of
允许输出序列,您可以尝试例如<xsl:value-of select="'|', $avg, '|', $percentile"/>
.
最后,在 XSLT 1.0 和 2.0 中您都可以使用 concat
,例如<xsl:value-of select="concat('TD: |', $currentHost)"/>
.