Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT xsl:normalize-space() function not working

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?

like image 326
Josh McKearin Avatar asked May 31 '13 06:05

Josh McKearin


People also ask

What does normalize space do in XSLT?

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.

What is normalize 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.

How do I remove spaces in XSLT?

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.

What is namespace in XSLT?

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.


2 Answers

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> 
like image 150
Tomalak Avatar answered Oct 10 '22 23:10

Tomalak


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>
like image 21
hr_117 Avatar answered Oct 10 '22 22:10

hr_117