xsl-list
[Top] [All Lists]

Re: Malformed (??) XML and XML 2 SQL XSLT transformation

2003-11-14 08:32:58
Hi Charles,

The lines are longer than I would like, but were necessary in order
to eliminate unwanted whitespace in the output.

Use <xsl:text> around text in order to make the stylesheet look nice
without passing whitespace through into the output. For example,
rather than:

 <xsl:template match="Orders" mode="columns">INSERT INTO orders(<xsl:for-each 
select="Customer/Template/Fields">"<xsl:value-of select="@FieldName" 
/>"<xsl:if test="position() != 
last()">,</xsl:if></xsl:for-each>)</xsl:template>

use:

<xsl:template match="Orders" mode="columns">
  <xsl:text>INSERT INTO orders(</xsl:text>
  <xsl:for-each select="Customer/Template/Fields">
    <xsl:text>"</xsl:text>
    <xsl:value-of select="@FieldName" />
    <xsl:text>"</xsl:text>
    <xsl:if test="position() != last()">,</xsl:if>
  </xsl:for-each>
  <xsl:text>)</xsl:text>
</xsl:template>

This works because whitespace-only text nodes in the tree generated
from the stylesheet document automatically get stripped, unless they
appear within <xsl:text> elements. So <xsl:text> effectively delimits
the text that you're interested in from the whitespace that you don't
want.

You can also use the concat() function in an <xsl:value-of> to reduce
the verbosity and help manage whitespace:

<xsl:template match="Orders" mode="columns">
  <xsl:text>INSERT INTO orders(</xsl:text>
  <xsl:for-each select="Customer/Template/Fields">
    <xsl:value-of select="concat('&quot;', @FieldName, '&quot;')" />
    <xsl:if test="position() != last()">,</xsl:if>
  </xsl:for-each>
  <xsl:text>)</xsl:text>
</xsl:template>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list