Formatting to CSV TEXT

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
The XSL file I am modifying creates a CSV file where the last two fields on each line can be empty
The second last field contains attributes (name/value pairs) where the name and value are separated by a space and the attributes are separated by | . If the name is description then only the value is output. The last field is an optional comment which
is just another attribute pair where the name of the attribute is COMMENT or ,
Example: the record for Bill Smith has FNAME and LNAME attributes and an AGE attribute and a DESCRIPTION attribute, plus a comment about Bill
e.g. <constant set of csv fields>,FNAME BILL | LNAME SMITH | AGE 32 | Bill is a nice guy,This is a comment about Bill
Here is the basic procedure for the last two fields
<ol>
Output a commaSkip though all attributesIf this is not the first attribute output a separator | If the attribute name is comment or , ignore it because this is a comment which goes lastIf the attribute value is empty ignore itIf the non-ignored attribute name = description output only <value>If the non-ignored attribute name != description output <name><space><value>Skip through all attributes againIf the attribute name is comment or , and value is not empty then output <comma><value></ol>
So this works for the most part but with the following problems:
<ol>
if the first attribute pair has a blank value then it is correctly ignored but you end up starting with the separator |
if you have a bunch of empty-value attributes the line ends with a comma</ol>
In a regular programming language I would use a flag to tell me a comma is needed and another flag to tell me if a separator is needed but with XSL I cannot work with flags.
<pre class="prettyprint <!-- do all attributes but not comments -->
<xsl:for-each select="Feature
<xsl:value-of select=","/>
<xsl:variable name="FeatName" select="@Name"/>
<xsl:for-each select="Attribute
<xsl:if test="string-length(Value) &gt; 0
<xsl:if test="Name != , and translate(Name,comment,COMMENT) != COMMENT
<!-- Include the predefined separator | if not first attribute -->
<xsl:if test="position() &gt; 1
<xsl:value-of select="string( | )"/>
</xsl:if>
<xsl:if test="translate(Name,description,DESCRIPTION) != DESCRIPTION
<xsl:value-of select="Name"/>
<xsl:value-of select="string( )"/>
</xsl:if>
<xsl:value-of select="Value"/>
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:for-each>

<!-- last do the comments -->
<xsl:for-each select="Feature
<xsl:variable name="FeatName" select="@Name"/>
<xsl:for-each select="Attribute
<xsl:if test="string-length(Value) &gt; 0
<xsl:if test="Name = , or translate(Name,comment,COMMENT) = COMMENT
<xsl:value-of select=","/>
<xsl:value-of select="Value"/>
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:for-each>[/code]
<br/>


<br/>

View the full article
 
Back
Top