我有一个看起来像这样的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,3 4,456,22 90,5,some text 365,16,soasdkjasdjkasdf 9,43,more text
我需要的是将其转化为:
1,2,3 4,456,22 90,5,some text 365,16,soasdkjasdjkasdf 9,43,more text
主要问题是空白行(来自与IF条件不匹配的节点)和缩进。有没有办法删除空行并修剪缩进,同时保留非空行后的换行符?
我尝试使用<xsl:strip-space elements="*"/>
,但输出如下:
1,2,3,4,456,22,90,5,some text,365,16,soasdkjasdjkasdf,9,43,more text
哪个不起作用,因为我需要在每一行上有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>
答案 0 :(得分:1)
我建议你这样做:
<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> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>