Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Can XSLT or XML strip characters

Firstly, I'd like to say I love XML. There, it's done.

Secondly, I'm building an app to convert a csv file format to another text format. The way I do it is to convert the csv to xml, then apply a stylesheet to the xml to generate 2 different reports, a sumamry and detailed (basically).

My problem is that the original csv file has a "/" at each end-row. These cause "non-well-formed" XML pages if the preceding field is empty. I am asking if I should use an (unknown to me) feature of XSLT to strip this character, or should I do it earlier - use VB (all I know) to StreamWrite the file without the annoying end characters which mess-up XML?

Does anyone have any expertience with this as to which is best/simpler/neater or all 3?
[777 byte] By [felix2468] at [2007-11-11 6:49:20]
# 1 Re: Can XSLT or XML strip characters
That's OK I used the string.Replace method of VB to fix up the csv file. Now I'm faced with getting an xslt to choose only the numbers out of an element whose value is alphanumeric...I'm sure there'll be an answer
felix2468 at 2007-11-11 23:28:54 >
# 2 Re: Can XSLT or XML strip characters
You should be able to alter this example, which strips non-numerical examples using a regular expression, to do what you need. http://www.dpawson.co.uk/xsl/rev2/regex2.html#d14432e301
arjonesiii at 2007-11-11 23:29:54 >
# 3 Re: Can XSLT or XML strip characters
Thanks for that arjonesiii
Funny u should mention that website, i came across another section of it in looking for answers to another problem.

I came up with the (lengthier) solution

<xsl:when test="translate(Narration, '1234567890&.-','')">
<xsl:value-of select="substring(translate(Narration, '1234567890&.-',''),1,32)"/>
</xsl:when>

that worked for me, except i had trouble removing the apostrophes out of the strings - i tried ' method but it fails, so i will give your method a try. (I tried quoting quotes and all sorts of variations but no luck)

Do you know of a site where i can find all the "useful" functions like translate, replace etc etc. I know there's www.w3.org but I'm looking for something in realspeak.

Guy
felix2468 at 2007-11-11 23:30:46 >
# 4 Re: Can XSLT or XML strip characters
With regard ti the "replace" expression, i had a read of the regex papers, and the way I see it, it is to be used in XSLT 2.0

XSLT is still a "work in draft" isn't it? I cannot make the suggested code work :
<xsl:template match="csv_data_records/record [Header=16]">
<xsl:variable name="in" select="Narration"/>
<xsl:value-of select="replace($in,'[^0-9]','')"/>

because xslt "Could not find function: replace", regardless of whether i make the stylesheet version 2.0 or 1.0. Maybe my namespace needs to be changed? I am not so hot on these topics just yet.

So is there a way to implement the 2.0 functions already?
felix2468 at 2007-11-11 23:31:52 >
# 5 Re: Can XSLT or XML strip characters
Maybe it's my processor? I'm using the xalan one, as the msxml rips out some whitespace I need kept in text files. I'll try updating the xalan processor to one that can handle 2.0
felix2468 at 2007-11-11 23:32:57 >