XSLT xsl: normalize-space () function not working

I 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 XML output:

 <?xml version="1.0" encoding="IBM437"?> <Tickets> <Ticket LogNumber="454564"> <CaseNumber>FGV-9999-88888888 </CaseNumber> </Ticket> </Tickets> 

I use

 <xsl:template match="/text"> <xsl:value-of select='normalize-space()'/> </xsl:template> 

like the MSDN article, everyone else on the network says, but cannot remove trailing spaces from CaseNumber Part3. Is there something I'm doing wrong?

+4
source share
4 answers

Edit <xsl:template match="/text">
to: xsl:template match="text()">

And use xsl:apply-templates instead of xsl:value-of .
To do this, 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> 

What will generate the following output:

 <Ticket LogNumber="454564"><CaseNumber>FGV-9999-88888888</CaseNumber></Ticket> 
+4
source

One way to do this is to use normalize-space directly in <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> 
+6
source

To normalize a copy of XML document Y, you can implement the following template:

  <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:ns1="http://logiasoftware.fi/2010/CanonicalDataModel" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" indent="yes"/> <xsl:template name="NormalizeSpace"> <xsl:element name="{name()}"> <xsl:choose> <xsl:when test="*"> <xsl:for-each select="*"> <xsl:call-template name="NormalizeSpace"/> </xsl:for-each> </xsl:when> <xsl:otherwise> <xsl:value-of select="normalize-space()"/> </xsl:otherwise> </xsl:choose> </xsl:element> </xsl:template> <xsl:template match="/"> <xsl:for-each select="*"> <xsl:call-template name="NormalizeSpace"/> </xsl:for-each> </xsl:template> </xsl:stylesheet> 
0
source

I thought normalize-space did not work because I was still getting line breaks in long lines. Turns out my problem is with my xsl: output configuration. My configuration was:

 <xsl:output encoding="UTF-8" method="html" omit-xml-declaration="yes"/> 

I needed to change the output method:

 <xsl:output encoding="UTF-8" method="text" omit-xml-declaration="yes"/> 
0
source

Source: https://habr.com/ru/post/1483726/


All Articles