XSL文本输出-修剪空行和前导空格



我有一个XSLT,它看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="no" encoding="utf-8" media-type="text/plain" />
    <xsl:template match="/SOME/NODE">
        <xsl:if test="./BLAH[foo]">
<xsl:value-of select="concat(@id, ',' , ./BLAH/bar/@id, ',' , ./blorb/text())"/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

输出看起来像这样(它将是一个CSV文件):

1,2,34456,2290.5,一些文本365,16,soasdkjasdkasdf9,43,更多文本

我需要的是将其转化为:

1,2,34456,2290.5,一些文本365,16,soasdkjasdkasdf9,43,更多文本

主要问题是空行(来自与IF条件不匹配的节点)和缩进。是否有任何方法可以删除空行并修剪缩进,同时保留非空行后的换行符?

我尝试过使用<xsl:strip-space elements="*"/>,但输出看起来像这样:

1,2,3,4456,22,90,5,部分文本,365,16,soasdkjasdjkasdf,9,43,更多文本

这不起作用,因为我需要每行有3个值。

根据要求,输入的(高度简化的)样本:

<SOME>
    <NODE>
        <BLAH id="1">
            <foo>The Foo</foo>
            <bar id="2" />
            <blorb> some text </blorb>
        </BLAH>
    </NODE>
    <NODE>
        <BLAH id="3">
            <bar id="4" />
            <blorb>some text that shouldn't be in output because there's no foo here</blorb>
        </BLAH>
    </NODE>
    <NODE>
        <BLAH id="5">
            <foo>another Foo</foo>
            <bar id="6" />
            <blorb>some other text</blorb>
        </BLAH>
    </NODE>
</SOME>

我建议您这样处理:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"  encoding="utf-8" />
<xsl:template match="/SOME">
    <xsl:for-each select="NODE/BLAH[foo]">
        <xsl:value-of select="@id"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="bar/@id"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="blorb"/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

最新更新