我有一个看起来有点像这个
的xml文件 <listopt>
<name>Body Dumping Phases</name>
<alias>dump-body</alias>
<set_arg_label>phaseName</set_arg_label>
<short_desc>Dump the internal representation of each method before and after phase
<use_arg_label/>
</short_desc>
我希望获得以下输出
<listopt>
<name>Body Dumping Phases</name>
<alias>dump-body</alias>
<set_arg_label>phaseName</set_arg_label>
<short_desc>Dump the internal representation of each method before and after phase <use_arg_label/></short_desc>
我在xsl文件中使用了strip-space,它链接到xml文件。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="short_desc" />
<xsl:template match="/"> ...
但似乎没有任何事情发生。我仍然得到输出文件与输入相同。 可能是什么问题呢?谢谢!
答案 0 :(得分:2)
在您的情况下,最好对所需节点使用normalize-space
,请参阅下面的XSL:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*" />
<xsl:output method="xml"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--perform normalize-space for text in node-->
<xsl:template match="short_desc/text()">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
</xsl:stylesheet>
因此对于以下XML:
<?xml version="1.0" encoding="UTF-8"?>
<listopt>
<name>Body Dumping Phases</name>
<alias>dump-body</alias>
<set_arg_label>phaseName</set_arg_label>
<short_desc>Dump the internal representation of each method before and after phase
<use_arg_label/>
</short_desc>
</listopt>
结果将符合预期:
<?xml version="1.0" encoding="UTF-8"?>
<listopt>
<name>Body Dumping Phases</name>
<alias>dump-body</alias>
<set_arg_label>phaseName</set_arg_label>
<short_desc>Dump the internal representation of each method before and after phase<use_arg_label/></short_desc>
</listopt>