Reputation: 89
I am trying to transform an xml document into another document to get a comma dlimited string. However, when I transform I see a lot of spaces.
I am not sure what is causing this and what should I do to fix this issue -
<File>
<row>
<wid>WID0001</wid>
<xtn>Leave Record</xtn>
<status>In Process</status>
<eid>E001</eid>
<amt>5000</amt>
</row>
<row>
<wid>WID0003</wid>
<xtn>Leave Record</xtn>
<status>In Process</status>
<eid>E005</eid>
<amt>3000</amt>
</row>
<row>
<wid>WID0010</wid>
<xtn>Leave Record</xtn>
<status>In Process</status>
<eid>E010</eid>
<amt>7000</amt>
</row>
<row>
<wid>WID0007</wid>
<xtn>Leave Record</xtn>
<status>In Process</status>
<eid>E010</eid>
<amt>0000</amt>
</row>
<row>
<wid>WID0050</wid>
<xtn>Leave Record</xtn>
<status>In Process</status>
<eid>E025</eid>
<amt>750000</amt>
</row>
</File>
And the XSLT -
<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:this="http://abogusurl.com">
<xsl:output indent="yes" method="xml" />
<xsl:template match="/File">
<widlist>
<xsl:apply-templates />
</widlist>
</xsl:template>
<xsl:template match="row">
<xsl:value-of select="wid" />
<xsl:value-of select="','" />
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0"?>
<widlist x xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:this="http://abogusurl.com">
WID0001,
WID0003,
WID0010,
WID0007,
WID0050,
</widlist>
How do I eliminate the spaces?
Upvotes: 3
Views: 908
Reputation: 66781
Your XML has text()
nodes between the row
elements. The built-in template rules preserve them in the output.
You can suppress those text()
nodes by adding an empty template that matches those whitespace-only text nodes:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/File">
<widlist>
<xsl:apply-templates/>
</widlist>
</xsl:template>
<xsl:template match="row">
<xsl:value-of select="wid"/>
<xsl:value-of select="','"/>
</xsl:template>
<!--suppress whitespace-only text nodes-->
<xsl:template match="text()[not(normalize-space())]"/>
</xsl:stylesheet>
You could also have the empty template match File/text()
:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/File">
<widlist>
<xsl:apply-templates/>
</widlist>
</xsl:template>
<xsl:template match="row">
<xsl:value-of select="wid"/>
<xsl:value-of select="','"/>
</xsl:template>
<!--suppress (whitespace) text nodes that are children of File-->
<xsl:template match="File/text()"/>
</xsl:stylesheet>
Or you can exclude those File/text()
nodes by only applying templates to the row
elements in the File template:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/File">
<widlist>
<xsl:apply-templates select="row"/>
</widlist>
</xsl:template>
<xsl:template match="row">
<xsl:value-of select="wid"/>
<xsl:value-of select="','"/>
</xsl:template>
</xsl:stylesheet>
All of these solutions remove the extra whitespace and linebreaks and produce the following output:
<widlist>WID0001,WID0003,WID0010,WID0007,WID0050,</widlist>
This implementation is currently producing a trailing comma in the widlist
.
You can fix that by changing the row template:
<xsl:template match="row">
<xsl:value-of select="wid"/>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:template>
If you are using XSLT 2.0 and want to produce a comma separated list, you could shorten your stylesheet to:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/File">
<widlist>
<xsl:value-of select="row/wid" separator=","/>
</widlist>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2
Reputation: 88286
So I think adding <xsl:strip-space elements="*"/>
will do what you want.
Upvotes: 2