I have followed the MSDN documentation verbatim here to no avail.
An example of my XML:
<Ticket xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<LogNo>454564</LogNo>
<CaseNumber>
<Part1>FGV</Part1>
<Part2>9999</Part2>
<Part3>88888888 </Part3>
</CaseNumber>
</Ticket>
An example of my XSLT:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*" />
<xsl:template match="/text">
<xsl:value-of select='normalize-space()'/>
</xsl:template>
<xsl:template match="Ticket">
<Ticket><xsl:attribute name="LogNumber"><xsl:value-of select="LogNo"/></xsl:attribute>
<CaseNumber><xsl:value-of select="CaseNumber/Part1"/>-<xsl:value-of select="CaseNumber/Part2"/>-<xsl:value-of select="CaseNumber/Part3"/></CaseNumber>
</Ticket>
</xsl:template>
</xsl:stylesheet>
My output XML:
<?xml version="1.0" encoding="IBM437"?>
<Tickets>
<Ticket LogNumber="454564">
<CaseNumber>FGV-9999-88888888 </CaseNumber>
</Ticket>
</Tickets>
I am using
<xsl:template match="/text">
<xsl:value-of select='normalize-space()'/>
</xsl:template>
as the MSDN article and everyone else on the net says, but can't seem to remove the trailing whitespace from CaseNumber Part3. Is there something I am doing wrong?
The normalize-space() function It does three things: It removes all leading spaces. It removes all trailing spaces. It replaces any group of consecutive whitespace characters with a single space.
The normalize-space function strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string.
XSLT <xsl:strip-space> The <xsl:strip-space> element is used to define the elements for which white space should be removed. Note: Preserving white space is the default setting, so using the <xsl:preserve-space> element is only necessary if the <xsl:strip-space> element is used.
The namespace name http://www.w3.org/1999/XSL/Transform is used primarily to identify elements which serve as declarations or instructions in the XSLT language.
One way to do this is using normalize-space
directly in the <xsl:value-of>
:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
<xsl:template match="Ticket">
<Ticket LogNumber="{LogNo}">
<xsl:apply-templates select="CaseNumber" />
</Ticket>
</xsl:template>
<xsl:template match="CaseNumber">
<xsl:copy>
<xsl:value-of select="concat(
normalize-space(Part1), '-',
normalize-space(Part2), '-',
normalize-space(Part3)
)" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Change <xsl:template match="/text">
to: xsl:template match="text()">
And use xsl:apply-templates
instead of xsl:value-of
.
Therefor try:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*" />
<xsl:template match="text()">
<xsl:value-of select='normalize-space()'/>
</xsl:template>
<xsl:template match="Ticket">
<Ticket>
<xsl:attribute name="LogNumber">
<xsl:value-of select="LogNo"/>
</xsl:attribute>
<CaseNumber>
<xsl:apply-templates select="CaseNumber/Part1"/>-<xsl:apply-templates select="CaseNumber/Part2"/>-<xsl:apply-templates select="CaseNumber/Part3"/>
</CaseNumber>
</Ticket>
</xsl:template>
</xsl:stylesheet>
Which will generate the following output:
<Ticket LogNumber="454564"><CaseNumber>FGV-9999-88888888</CaseNumber></Ticket>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With