xsltproc question - wondering what I did wrong?

Discussion in 'XML' started by Glen Millard, Apr 13, 2012.

  1. Glen Millard

    Glen Millard Guest

    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
    Glen Millard, Apr 13, 2012
    #1
    1. Advertising

  2. Glen Millard wrote:

    > 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.


    --

    Martin Honnen --- MVP Data Platform Development
    http://msmvps.com/blogs/martin_honnen/
    Martin Honnen, Apr 14, 2012
    #2
    1. Advertising

  3. 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).
    Joe Kesselman, Apr 14, 2012
    #3
  4. 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."
    Joe Kesselman, Apr 14, 2012
    #4
  5. Glen Millard

    Glen Millard Guest

    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

    On Saturday, April 14, 2012 3:34:52 PM UTC-4, Joe Kesselman wrote:
    > 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."
    Glen Millard, Apr 15, 2012
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yzzzzz
    Replies:
    0
    Views:
    713
    yzzzzz
    Nov 12, 2003
  2. Brett

    xsltproc and Entities

    Brett, Feb 27, 2004, in forum: XML
    Replies:
    1
    Views:
    908
    Alain Ketterlin
    Mar 1, 2004
  3. Loudin
    Replies:
    1
    Views:
    761
    David Carlisle
    Jul 1, 2005
  4. Daniel Waite
    Replies:
    2
    Views:
    226
    Daniel Waite
    May 2, 2008
  5. Glen Millard
    Replies:
    10
    Views:
    1,527
    Peter Flynn
    Mar 12, 2012
Loading...

Share This Page