[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: reading a .xsv file in xslt
Hi Andrew,
I am not sure that I understand well your point on QNames and normalize-space, as if the header line is missing from the csv, picking it up to try to derive element or attribute names from it is bound to generate invalid QName errors, for example, with an amount or dollar value. The way I currently understand it, one would have to test and determine if line 1 provides headers or not, figuring options if it is not. I would, for example, use something like <xsl:function name="fn:get-headers" as="xs:string+"> <xsl:param name="param"/> <xsl:param name="line1"/> <xsl:param name="line2"/> <xsl:variable name="headerline" select="if (string($param)) then $param else if (every $x in fn:get-tokens($line1) satisfies ($x castable as xs:QName)) then $line1 else ''"/> <xsl:variable name="headers" select="fn:get-tokens($headerline)"/> <xsl:for-each select="1 to max((count(fn:get-tokens($line2)), count($headers)))"> <xsl:variable name="pos" select="position()"/> <xsl:value-of select="if ($headers[$pos] castable as xs:QName) then $headers[$pos] else concat('col', string($pos))"/> </xsl:for-each> </xsl:function> to get the headers, covering most cases of missing, or partly missing, or not missing column headers, with the option of providing a set of column header names as a parameter, assuming an invocation, from your example code, like <xsl:variable name="names" select="fn:get-headers($headers, $lines[1], $lines[2])" as="xs:string+"/> where this $headers is a parameter to the main "csv2xml" template, which could be empty or a string similar to the expected csv header line. The fact that there may not be a header line in the csv file, also implies that the line <xsl:for-each select="$lines[position() > 1]"> may have to be changed to something like <xsl:for-each select="$lines[position() > every $x in $line[1] satisfies ($x castable as xs:QName)]"> for example. '
' indeed displays as space in html but wouldn't '\r?\n' be more portable? As a note on extending your example, the name for <root> and <row> could be parametrized and I think that I would move <root> further outside the nested code and allow $csvpath to be a space-delimited name list, for example, to easily support csv file merge into the tree, by simply looping over the tokenized file paths. Your code offers a good basic design and I especially like your regex token grabber.
On 3 February 2010 04:11, ac <ac@xxxxxxxxxxxxx> wrote:
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|