I want to remove spaces from all attributes in my xmls using xslt. I used strip-space
, but that removes the spaces from nodes.
My input xml is:
<OrderList>
<Order OrderDate="26-July" OrderNo="ORDER 12345"
CustomertName="JOHN DOE" OrderKey="ORDKEY12345">
<ShipAddress AddressLine="ABC Colony" FirstName="John" LastName="Doe "/>
</Order>
</OrderList>
and the xsl I used to get rid of the spaces in the attributes like CustomertName="JOHN DOE"
is:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="/">
<xsl:apply-templates/>
<OrderList>
<xsl:for-each select="OrderList/Order">
<xsl:element name="Order">
<xsl:copy-of select="@OrderDate"/>
<xsl:copy-of select="@OrderNo"/>
<xsl:copy-of select="@CustomertName"/>
<!-- ShipAddress begins -->
<xsl:element name="ShipAddress">
<xsl:copy-of select="ShipAddress/@AddressLine"/>
<xsl:copy-of select="ShipAddress/@FirstName"/>
<xsl:copy-of select="ShipAddress/@LastName"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</OrderList>
</xsl:template>
</xsl:stylesheet>
But this leaves the input xml as it was. I want to remove the spaces from the attribute values at all levels.
You can use the translate function, like so, although it would make sense to refactor this with a call template:
<xsl:attribute name="OrderDate">
<xsl:value-of select="translate(@OrderDate, ' ','')"/>
</xsl:attribute>
<xsl:attribute name="OrderNo">
<xsl:value-of select="translate(@CustomertName, ' ','')"/>
</xsl:attribute>
<xsl:attribute name="CustomertName">
<xsl:value-of select="translate(@CustomertName, ' ','')"/>
</xsl:attribute>