javax.xml.transform.Transformer 正在删除所需的空白(xml 到文本转换)
我正在尝试使用 javax.xml.transform 将 XML 转换为文本。 xsltproc 将正确地将我的 XML 转换为格式正确的文本,而以下代码生成的输出几乎删除了所有空格:
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final InputStream is = getClass().getResourceAsStream( xslResourceName );
final StreamSource xsltSrc = new StreamSource( is );
final Transformer transformer = tFactory.newTransformer( xsltSrc );
final Source src = new StreamSource( new StringReader( xmlData ) );
final Result res = new StreamResult( out );
transformer.setOutputProperty( "method", "text" );
transformer.setOutputProperty( "omit-xml-declaration", "yes" );
transformer.transform( src, res );
return out.toString();
XSLT 有意添加空格,使用如下标签:
<xsl:value-of select="substring(concat(concat($frontpadding,$cellvalue),$blank),1,$width)"/>
对于更大的示例,源 xml 可能具有
<reportheader display="true">
<name>Hours01</name>
<date>2011-04-14</date>
<description>Hours Report</description>
<pagewidth>130</pagewidth>
</reportheader>
: xsl 具有:
<xsl:template match="reportheader">
<xsl:if test="@display='true'">
<xsl:variable name="col1width" select="12"/>
<xsl:variable name="datewidth" select="10"/>
<xsl:variable name="col2width" select="$pagewidth - $col1width - $datewidth"/>
<xsl:copy-of select="substring(concat(name,$blank),1,$col1width)"/>
<xsl:copy-of select="substring(concat(description,$blank),1,$col2width)"/>
<xsl:copy-of select="substring(concat(date,$blank),1,$datewidth)"/>
<xsl:text>
</xsl:text>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:template>
xsltproc 输出为:
Hours01 Hours Report 2011-04-14
javax.xml.transformer.Transformer 输出为:
Hours01Hours Report2011-04-14
I'm trying to convert XML to text, using javax.xml.transform. xsltproc will correctly transform my XML to properly formatted text, while the following code produces output with almost all whitespace removed:
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final InputStream is = getClass().getResourceAsStream( xslResourceName );
final StreamSource xsltSrc = new StreamSource( is );
final Transformer transformer = tFactory.newTransformer( xsltSrc );
final Source src = new StreamSource( new StringReader( xmlData ) );
final Result res = new StreamResult( out );
transformer.setOutputProperty( "method", "text" );
transformer.setOutputProperty( "omit-xml-declaration", "yes" );
transformer.transform( src, res );
return out.toString();
The spaces are intentionally being added by the XSLT, using tags such as:
<xsl:value-of select="substring(concat(concat($frontpadding,$cellvalue),$blank),1,$width)"/>
For a larger example, the source xml might have:
<reportheader display="true">
<name>Hours01</name>
<date>2011-04-14</date>
<description>Hours Report</description>
<pagewidth>130</pagewidth>
</reportheader>
The xsl has:
<xsl:template match="reportheader">
<xsl:if test="@display='true'">
<xsl:variable name="col1width" select="12"/>
<xsl:variable name="datewidth" select="10"/>
<xsl:variable name="col2width" select="$pagewidth - $col1width - $datewidth"/>
<xsl:copy-of select="substring(concat(name,$blank),1,$col1width)"/>
<xsl:copy-of select="substring(concat(description,$blank),1,$col2width)"/>
<xsl:copy-of select="substring(concat(date,$blank),1,$datewidth)"/>
<xsl:text>
</xsl:text>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:template>
The xsltproc output is:
Hours01 Hours Report 2011-04-14
And the javax.xml.transformer.Transformer output is:
Hours01Hours Report2011-04-14
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是如何定义$blank的?当我这样做时,
我会得到与你相同的结果。但是,以下产生了您想要的结果
How did you define $blank? When I do
I get the same results as you. However, the following produced the results you desire
尝试在 xslt 中使用 xml 字符作为空格。
或者使用文本标签..
我希望这有帮助。
Try the xml character for space in your xslt.
Or use text tag..
I hope this helps.