Re: [xsl] Correcting misplaced spaces in XML documents

Subject: Re: [xsl] Correcting misplaced spaces in XML documents
From: "Graydon graydon@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 26 Mar 2023 01:52:20 -0000
On Sun, Mar 26, 2023 at 01:33:59AM -0000, Trevor Nicholls trevor@xxxxxxxxxxxxxxxxxx scripsit:
> Can anyone point me to a cleaner way of doing it?

Something like

<xsl:template match="text()[not(preceding-sibling::node())][not(following-sibling::node())]">
    <xsl:value-of select="normalize-space(.)"/>
  </xsl:template>
  <xsl:template match="text()[preceding-sibling::node()][following-sibling::node()]">
    <xsl:value-of select="concat('&#x0020;',normalize-space(.),'&#x0020;')"/>
  </xsl:template>
  <xsl:template match="text()[not(preceding-sibling::node())][following-sibling::node()]">
    <xsl:value-of select="concat(normalize-space(.),'&#x0020;')"/>
  </xsl:template>
  <xsl:template match="text()[preceding-sibling::node()][not(following-sibling::node())]">
    <xsl:value-of select="concat('&#x0020;',normalize-space(.))"/>
  </xsl:template>

May be useful.  "node()" is dangerous; PIs and comments (if present in
the source) need to be handled as though they aren't there while not
being lost, so the pattern gets more complicated.

And there might be cases where you're trying to do something specific
and different, but that can usually be moved into its own pass, either
before, to regularize the source, or after, to apply the special cases.


-- 
Graydon Saunders  | graydonish@xxxxxxxxx
^fs oferiode, pisses swa mfg.
-- Deor  ("That passed, so may this.")

Current Thread