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:
If you just want to removes spaces from the values of attributes, you could just create a template to match any attribute, and then use the translate function.
<xsl:template match="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="translate(., ' ', '')" />
</xsl:attribute>
</xsl:template>
And if you want to filter out some attributes, you can create templates to match, and then ignore them. (XSLT will match the more specific template first)
<xsl:template match="Order/@OrderKey" />
You can also simplify you code somewhat by making use of the identity transform to cope existing nodes. Here is the full XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="translate(., ' ', '')" />
</xsl:attribute>
</xsl:template>
<xsl:template match="Order/@OrderKey" />
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When applied to your sample XML, the following is ouptut
<OrderList>
<Order OrderDate="26-July" OrderNo="ORDER12345" CustomertName="JOHNDOE">
<ShipAddress AddressLine="ABCColony" FirstName="John" LastName="Doe"></ShipAddress>
</Order>
</OrderList>
The advantage of this approach is that it will work with any XML document.
If you wanted to do this only for specific elements, try this XSLT instead which explicitly matches the attributes you want (and discards all others)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Order/@OrderDate|Order/@OrderNo|Order/@CustomertName|ShipAddress/@AddressLine|ShipAddress/@FirstName|ShipAddress/@LastName">
<xsl:attribute name="{name()}">
<xsl:value-of select="translate(., ' ', '')"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="@*"/>
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
EDIT: If you wanted to remove only leading and trailing spaces, then 'normalize-space' is your friend.
<xsl:value-of select="normalize-space(.)" />
Do note this will remove excess spaces within the attribute though (that is to say double-spaces become single spaces between words).
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>