Database Reference
In-Depth Information
<!-- Process fields in each row -->
<xsl:template
match=
"row"
>
<xsl:apply-templates
select=
"field"
/>
</xsl:template>
<!-- Display text content of each field -->
<xsl:template
match=
"field"
>
<xsl:value-of
select=
"."
/>
<xsl:choose
>
<xsl:when
test=
"position() != last()"
>
<xsl:text
>
,
</xsl:text>
<!-- comma after all but last field -->
</xsl:when>
<xsl:otherwise
>
<xsl:value-of
select=
"' '"
/>
<!-- newline after last field -->
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Use the transform like this:
%
mysql -X -e "SELECT * FROM limbs WHERE legs=0" cookbook \
| xsltproc mysql-xml.xsl -
Query: SELECT * FROM limbs WHERE legs=0
Result set:
squid, 0, 10
fish, 0, 0
phonograph, 0, 1
The
-H
,
--html
-X
, and
--xml
options produce output only for statements that generate
a result set, not for statements such as
INSERT
or
UPDATE
.
To write your own programs that generate XML from query results, see
Recipe 11.9
. To
write web scripts that generate HTML from query results, see
Chapter 18
.
Suppressing column headings in query output
Tab-delimited format is convenient for generating datafiles for import into other pro‐
grams. However, the first row of output for each query lists the column headings by
default, which may not always be what you want. Suppose that a program named
sum‐
marize
produces descriptive statistics for a column of numbers. If you produce output
from
mysql
to be used with this program, a column header row would throw off the
results because
summarize
would treat it as data. To create output that contains only
data values, suppress the header row with the
--skip-column-names
option:
%
mysql --skip-column-names -e "SELECT arms FROM limbs" cookbook | summarize
Specifying the “silent” option (
-s
or
--silent
) twice achieves the same effect:
%
mysql -ss -e "SELECT arms FROM limbs" cookbook | summarize