Reputation: 521
I want output with spaces however I want to keep <xsl:strip-space elements="*"/>
as well in the xslt. Presently, out by below XSLT is "onetwofour" that is without spaces. If I remove <xsl:strip-space elements="*"/>
tag output will be as expected that is "one two four" (note two spaces between word two & four one extra space is due to subitem3)
Note: "subitem3" tag has one space, which should be retain in output.
Is there any way i can keep <xsl:strip-space elements="*"/>
tag and have spaces also (so output should be like this =>"one two four").
If that is not possible due to inclusion of xsl:strip-space on top at least space for subitem3 can it be retain (so expected output in this case can be "onetwo four" notice space before word 'four' due to subitem3)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:template match="item">
<xsl:for-each select="child::node()">
<xsl:choose>
<xsl:when test="name() = 'some_tag'">
<!-- Do nothing for now -->
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="descendant-or-self::text()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
XML:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<subitem1>one</subitem1> <subitem2>two</subitem2> <subitem3> </subitem3> <subitem4>four</subitem4>
</item>
</items>
present output: onetwofour
Expected Output: one two four OR onetwo four
Upvotes: 0
Views: 493
Reputation: 117140
If you want to retain a text node that contains only whitespace characters, then either do not use:
<xsl:strip-space elements="*"/>
or override it by:
<xsl:preserve-space elements="subitem3"/>
Alternatively, you could insert your own spaces when writing to the output tree - for example:
<xsl:template match="item">
<xsl:for-each select="*">
<xsl:value-of select="."/>
<xsl:if test="not(string())">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
Upvotes: 1