Re: [xsl] Transforming field when parsing XML file with xsltproc

Subject: Re: [xsl] Transforming field when parsing XML file with xsltproc
From: "C. M. Sperberg-McQueen cmsmcq@xxxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 3 Jul 2022 14:40:00 -0000
"H agents@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> writes:

> Unfortunately one of the fields of the XML records occasionally
> contains LF characters which normally should not be part of the
> field. Can xsltproc be used to replace the LF in this single field
> with SPACE?

If the element 'this-single-field' never has child elements, then
something along the lines of

    <xsl:template match="this-single-field">
        <xsl:variable name="safe-value"
                      select="translate(string(.), '&#x0A;', ' ')"/>
                      
        ... do what you need to do ...
        ... and write $safe-value out where you need it ...
    </

If this-single-field may have child elements, then you need to call
translate() on each text node within it.  So instead of handling it all
at once as shown above, you'll need to handle it text-node by text-node:

    <xsl:template match="text()[ancestor::this-single-field]">
        <xsl:value-of select="translate(., '&#xA;', ' ')"/>
    </

If the line breaks in the input turn out to be important (e.g. people
using them to separate paragraphs or items in lists -- it does happen),
you might want to translate LF to "\n" instead of " ", and let
downstream processes use the \n or not as they choose.

Good luck.

Michael Sperberg-McQueen



-- 
C. M. Sperberg-McQueen
Black Mesa Technologies LLC
http://blackmesatech.com

Current Thread