xsltproc question - wondering what I did wrong?

G

Glen Millard

Hi;

When I reformat my XML file using xsltproc and my xsl template file, I see the following anomaly: (call it what you will!)

<?xml version="1.0" encoding="iso-8859-1"?>
<row>
<date>2012-03-04 21:16:56</date>
<src>+19095703426</src>
<dst>8774658861</dst>
<duration>24</duration>
<callerid>+19095703426</callerid>
<cost>0.0106</cost>
</row><row>
<date>2012-03-06 20:58:14</date>
<src>8883091120</src>
<dst>8885216725</dst>
<duration>24</duration>
<callerid>VFAX - Received</callerid>
<cost>0.011200000000000002</cost>
</row><row>

Notice how it does not put the <row> tag on the next line, as I would wish it to.

Here is a snippet of my original XML doc:
<?xml version="1.0" encoding="utf-8"?>
<content>
<status>ok</status>
<records>
<call>
<date>2012-03-04 21:16:56</date>
<src>+19095703426</src>
<dst>8774658861</dst>
<duration>24</duration>
<callerid>+19095703426</callerid>
<disposition>ANSWERED</disposition>
<cost>0.0106</cost>
</call>
<call>
<date>2012-03-06 20:58:14</date>
<src>8883091120</src>
<dst>8885216725</dst>
<duration>24</duration>
<callerid>VFAX - Received</callerid>
<disposition></disposition>
<cost>0.011200000000000002</cost>
</call>
<call>
..
..
..
</call>
</records>
</content>



Here is my xsl stylesheet:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:eek:utput method="xml" encoding="iso-8859-1" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="status"></xsl:template>
<xsl:template match="ok"></xsl:template>
<xsl:template match="/content/records/call">
<xsl:for-each select="date">
<xsl:variable name="i" select="position()"/>
<xsl:element name="row">
<xsl:element name="date">
<xsl:value-of select="."/>
</xsl:element>
<xsl:element name="src">
<xsl:value-of select="../src[$i]"/>
</xsl:element>
<xsl:element name="dst">
<xsl:value-of select="../dst[$i]"/>
</xsl:element>
<xsl:element name="duration">
<xsl:value-of select="../duration[$i]"/>
</xsl:element>
<xsl:element name="callerid">
<xsl:value-of select="../callerid[$i]"/>
</xsl:element>
<xsl:element name="cost">
<xsl:value-of select="../cost[$i]"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

What am I doing wrong? Anyone? What would be the best way to correct this please?

Thanks so much in advance for your help!

Glen
 
M

Martin Honnen

Glen said:
What am I doing wrong? Anyone? What would be the best way to correct this please?

Your XSLT outputs a document fragment with several top level elements
(i.e. those "row" elements). xsltproc seems to have its own way of
formatting such a document fragment. If I add a template creating a root
element for the result with e.g.

<xsl:template match="/">
<rows>
<xsl:apply-templates/>
</rows>
</xsl:template>

then xsltproc outputs

<rows>
<row>
<date>2012-03-04 21:16:56</date>
<src>+19095703426</src>
<dst>8774658861</dst>
<duration>24</duration>
<callerid>+19095703426</callerid>
<cost>0.0106</cost>
</row>
<row>
<date>2012-03-06 20:58:14</date>
<src>8883091120</src>
<dst>8885216725</dst>
<duration>24</duration>
<callerid>VFAX - Received</callerid>
<cost>0.011200000000000002</cost>
</row>
</rows>

so there indentation is probably what you want.

I am not sure whether the XSLT 1.0 specification is precise and detailed
enough to prescribe how to indent a document fragment so it is hard to
tell whether xsltproc's behaviour could be called a bug. Saxon 6.5.5 is
another XSLT 1.0 processor, it manages to put each "row" on a line of
its own even when outputting a fragment.
 
J

Joe Kesselman

XSLT outputs exactly what you tell it to. Adding the break you're
looking for would add whitespace content to the output document,
potentially changing its meaning.

Try turning on indendation (using xsl:eek:utput's control for that, plus
any control needed at the XSLT processor level -- I don't use xsltproc
so I don't know its quirks). That will give the processor permission to
add whitespace before starting elements.

And/or modify your stylesheet to explicitly output the line break, using

<xsl:text>
</xsl:text>

(with the line break inside the xsl:text element) or

<xsl:text>
</xsl:text>

(same thing, but uses a numeric character escape rather than the literal
newline character).
 
J

Joe Kesselman

For what it's worth: It may be a bit dated, but Mike Kay's XSLT book is
a good reference for beginners. Looking up "whitespace" in the index
will point you to the most common issues beginners run into.

The XSLT FAQ website is also a good resource.

And of course -- my standard plug -- there are a number of good
tutorials and articles at http://developerworks.ibm.com/xml. (Yes, I
wrote an article for them. Why do you ask? <grin/>

--
Joe Kesselman,
http://www.love-song-productions.com/people/keshlam/index.html

{} ASCII Ribbon Campaign | "may'ron DaroQbe'chugh vaj bIrIQbej" --
/\ Stamp out HTML mail! | "Put down the squeezebox & nobody gets hurt."
 
G

Glen Millard

Excellent information here. I will study up some on the resources that you have provided.

I have tried both of these ideas, and they both seem to do the trick.

I thank all of you for your assistance!

Glen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top