[xsl] Re: Re-arranging an XML file

Subject: [xsl] Re: Re-arranging an XML file
From: XSL-List Owner <xsl-list-owner@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 22 Jan 2009 11:12:18 -0500
Mike --

You will improve the chances that you will receive prompt and helpful responses by:

  - asking specific enough questions that answers are possible. Describe
    what you are trying to do, how you approached it, what happened, and
    why you don't like the result you got

  - supplying small samples of files that illustrate your question or
    problem. For example, an XML file, an XSLT stylesheet, the result
    you got, and the result you want - all in miniature (and remembering
    that everything posted to XSL-List is archived in public)

-- Tommie

Hi Wendell et. al,

I've inserted the following in my XSL file, but it's being ignored.

<xsl:template match="LastChangedBy">
  <xsl:text>User: </xsl:text>
  <xsl:text>, </xsl:text>

Any ideas as to why? What I'm trying to do is to insert parts from
both Artists and Albums into my output file, and to control their
order of appearance.

Many thanks,

Mike in SA

Date: Wed, 21 Jan 2009 10:17:16 -0500
 To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
 From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
 Subject: Re: [xsl] Re: Re-arranging an XML file


At 06:22 AM 1/21/2009, you wrote:
As I mentioned in an earlier post: I'm new to XSL (I've got about 3
hours experience) and am still trying to get my head around it, I have
an XSL file that outputs in CSV format, but when I tried applying it
to the script you sent (below) everything falls down.

Welcome to XSL!

 This is indeed a long way from COBOL. You are probably best off
 thinking of it as a different kind of thing entirely. XSLT is to
 COBOL as automotive engineering is to wheelwrighting. Don't take this
 as a slight against COBOL: automobiles need wheels too. (Also, wheels
 are useful for many things besides automobiles.) It's just that
 automotive engineering assumes that the principles of wheel design
 are well understood, and indeed that there is an entire industry (an
 entire *tire* industry) we can rely on to give us wheels for our
 automobiles any time we need them.

  There are a few
things I don't understand:
- how can I output in CSV format?

CSV is a special kind of plain text output. In XSLT, when you ask
"how can I output X"?, you need to keep in mind that any kind of
output presumes two things, the logical and the "physical" (keeping
in mind that as long as we're talking about bits on a disk,
"physical" is itself questionable). That is, it's a question both
about the organization of the output, and the physical form the output takes.

 If you're talking about a CSV file on your disk, that's a physical
 form. To manage this side of the question, XSLT relies on external
 programs, called serializers (since they take tree-organized
 XSLT/XPath strucures and sequence their information into streams of
 characters written to files). Most XSLT processors come with one or
 more serializers. You need a serializer to write plain text, since it
 won't have to write any tagging (XML, HTML or what have you) based on
 the structures it sees in your XSLT results.

Commonly this can be done from your stylesheet using a top-level element:

<xsl:output method="text"/>

 which tells the XSLT system to take care of it ("serialize this a
 plain text, please").

 ... and then it's only a matter of getting your commas (or other
 delimiters) in the right place in your CSV (and managing any issues
 related to that, such as escaping commas in your data. Since this has
 to do with the organization or arrangement of your data (comma
 characters being used to organize the data internally), this job will
 be done by your XSLT proper.

- how do I remove the "<AddedAlbums> tag?
- how can I include (for example) "LastChangedBy" labeling it
something else? (eg "User")

These are really basic questions. Would you mind us referring you to
> some of the excellent available literature to explain it? You might
 appreciate one of the starter-level books by Jeni Tennison (she has
 volumes to cover both XSLT 1.0 and 2.0).

An example would be appreciated.

Here's an example:

 <xsl:template match="AddedAlbums">
   <xsl:text>, </xsl:text>

 A stylesheet containing this template, assuming no other templates
 interfere with it, will result in output in which the "AddedAlbums"
 tags are removed, the data between those tags (the contents of the
 AddedAlbums elements) are processed (they will probably come through
 depending on what other templates are doing), and a comma is added
 after every bit of content that had been an 'AddedAlbums' element.

 Notice that there is a *big* difference between removing AddedAlbums
 *tags* and removing the entire element (which in the XML includes its
 contents as well as its tags). You asked about removing the tags, so
 I'm assuming the content is still wanted.


 <xsl:template match="LastChangedBy">
   <xsl:text>User: </xsl:text>
   <xsl:text>, </xsl:text>

 This does almost the same thing, removing the LastChangedBy tags,
 allowing the content to be processed, but prefixing it with the text
 "User: ", while also appending a comma.

 I hope this helps. But I really think you need to dig into a good
 book. The way XSLT works is just not something you could guess based
 on what was possible twenty years ago....


 Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
 Mulberry Technologies, Inc.                http://www.mulberrytech.com
 17 West Jefferson Street                    Direct Phone: 301/315-9635
 Suite 207                                          Phone: 301/315-9631
 Rockville, MD  20850                                 Fax: 301/315-8285
   Mulberry Technologies: A Consultancy Specializing in SGML and XML


B. Tommie Usdin                        mailto:btusdin@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                           Phone: 301/315-9631
Suite 207                                    Direct Line: 301/315-9634
Rockville, MD  20850                                 Fax: 301/315-8285
  Mulberry Technologies: A Consultancy Specializing in XML and SGML

Current Thread