Statistics
| Revision:

root / tmp / org.txm.core / res / org / txm / xml / xsl / tei / profiles / default / csv / from.xsl @ 187

History | View | Annotate | Download (4.8 kB)

1
<?xml version="1.0"?>
2

    
3
<xsl:stylesheet version="2.0"
4
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
5
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
6
	xmlns:tei="http://www.tei-c.org/ns/1.0"
7
	xmlns:jc="http://james.blushingbunny.net/ns.html"
8
	exclude-result-prefixes="xs tei jc">
9
	<xsl:param name="input-encoding" select="'UTF-8'" as="xs:string"/>
10
	<xsl:output indent="yes" encoding="UTF-8"/>
11
	<xsl:param name="input-uri" select="'filename.csv'"/>
12

    
13
  <doc xmlns="http://www.oxygenxml.com/ns/doc/xsl" scope="stylesheet" type="stylesheet">
14
      <desc>
15
         <p>This software is dual-licensed:
16

    
17
1. Distributed under a Creative Commons Attribution-ShareAlike 3.0
18
Unported License http://creativecommons.org/licenses/by-sa/3.0/ 
19

    
20
2. http://www.opensource.org/licenses/BSD-2-Clause
21
		
22
All rights reserved.
23

    
24
Redistribution and use in source and binary forms, with or without
25
modification, are permitted provided that the following conditions are
26
met:
27

    
28
* Redistributions of source code must retain the above copyright
29
notice, this list of conditions and the following disclaimer.
30

    
31
* Redistributions in binary form must reproduce the above copyright
32
notice, this list of conditions and the following disclaimer in the
33
documentation and/or other materials provided with the distribution.
34

    
35
This software is provided by the copyright holders and contributors
36
"as is" and any express or implied warranties, including, but not
37
limited to, the implied warranties of merchantability and fitness for
38
a particular purpose are disclaimed. In no event shall the copyright
39
holder or contributors be liable for any direct, indirect, incidental,
40
special, exemplary, or consequential damages (including, but not
41
limited to, procurement of substitute goods or services; loss of use,
42
data, or profits; or business interruption) however caused and on any
43
theory of liability, whether in contract, strict liability, or tort
44
(including negligence or otherwise) arising in any way out of the use
45
of this software, even if advised of the possibility of such damage.
46
</p>
47
         <p>Author: James Cummings</p>
48
         <p>Id: $Id: from.xsl 9646 2011-11-05 23:39:08Z rahtz $</p>
49
         <p>Copyright: 2010, TEI Consortium</p>
50
<p>
51
Usage:  saxon -it main styesheet.xsl input-uri=filename.csv
52

    
53
<ul>
54
<li> It assumes cells are comma separated and 
55
rows are on individual lines...technically a linebreak 
56
is allowed to occur inside a quoted CSV field, but in 
57
this stylesheet it will be treated as two separate lines.</li>
58
<li>If surrounded in double-quotes, it will remove 
59
them.</li>
60
<li>If there are extra or final commas, these will be changed 
61
into empty <gi>cell/</gi> elements </li>
62
<li> It assumes things are in UTF-8 but this can be 
63
changed with the input-encoding parameter.  
64
All output is converted to UTF-8.</li>
65
</ul>
66
</p>
67
      </desc>
68
   </doc>
69

    
70
   <xsl:function name="jc:splitCSV" as="xs:string+">
71
     <xsl:param name="str" as="xs:string"/>
72
     <xsl:analyze-string select="concat($str, ',')"
73
			 regex="((&quot;[^&quot;]*&quot;)+|[^,]*),">
74
       <xsl:matching-substring>
75
	 <xsl:sequence
76
	     select="replace(regex-group(1), &quot;^&quot;&quot;|&quot;&quot;$|(&quot;&quot;)&quot;&quot;&quot;, &quot;$1&quot;)"
77
	     />
78
       </xsl:matching-substring>
79
     </xsl:analyze-string>
80
   </xsl:function>
81
   
82
   <xsl:template match="/" name="main">
83
     <xsl:choose>
84
       <xsl:when test="unparsed-text-available($input-uri, $input-encoding)">
85
	 <xsl:variable name="csv"
86
		       select="unparsed-text($input-uri, $input-encoding)"/>
87
	 <xsl:variable name="lines" select="tokenize($csv, '&#xa;')"
88
		       as="xs:string+"/>
89
	 <TEI xmlns="http://www.tei-c.org/ns/1.0">
90
	   <teiHeader>
91
	     <fileDesc>
92
	       <titleStmt>
93
		 <title>A TEI file automatically converted from CSV</title>
94
	       </titleStmt>
95
	       <publicationStmt>
96
		 <p>No publication statement</p>
97
	       </publicationStmt>
98
	       <sourceDesc>
99
		 <p>A TEI file automatically converted from a CSV file.</p>
100
		 
101
	       </sourceDesc>
102
	     </fileDesc>
103
	   </teiHeader>
104
	   <text>
105
	     <body>
106
	       <table>
107
		 <xsl:for-each select="$lines">
108
		   <row>
109
		     <xsl:variable name="lineItems" select="jc:splitCSV(.)"
110
				   as="xs:string+"/>
111
		     
112
		     <xsl:for-each select="$lineItems">
113
		       <xsl:variable name="pos" select="position()"/>
114
		       <cell n="{$pos}">
115
			 <xsl:value-of select="$lineItems[$pos]"/>
116
		       </cell>
117
		     </xsl:for-each>
118
		   </row>
119
		 </xsl:for-each>
120
	       </table>
121
	     </body>
122
	   </text>
123
	 </TEI>
124
       </xsl:when>
125
       <xsl:otherwise>
126
	 <xsl:message terminate="yes">
127
	   <xsl:text>Cannot find the input csv file: </xsl:text>
128
	   <xsl:value-of select="$input-uri"/>
129
	 </xsl:message>
130
       </xsl:otherwise>
131
     </xsl:choose>
132
   </xsl:template>
133
</xsl:stylesheet>