change output order

Discussion in 'XML' started by FlickChick, Jan 20, 2005.

  1. FlickChick

    FlickChick Guest

    I don't usually post questions, but 2 days of googleing and frustration
    have broken my resistance.

    I have some scary looking xml that looks something like this:

    <?xml version="1.0"?>
    <foo xmlns='urn:xxx:yyy'>
    <stuff type="why">
    <morestuff type="do">
    <evenmore>
    <stuff_overload type="this">
    <finally name="{last}">
    <value>Doe</value>
    </finally>
    <finally name="{first}">
    <value>John</value>
    </finally>
    <finally name="{number}">
    <value>14</value>
    </finally>
    </stuff_overload>
    </evenmore>
    </morestuff>
    </stuff>
    <stuff type="why">
    <morestuff type="do">
    <evenmore>
    <stuff_overload type="this">
    <finally name="{last}">
    <value>Smith</value>
    </finally>
    <finally name="{first}">
    <value>Jane</value>
    </finally>
    <finally name="{number}">
    <value>15</value>
    </finally>
    </stuff_overload>
    </evenmore>
    </morestuff>
    </stuff>
    </foo>


    Which I would like to transform into this:
    John,Doe,14
    Jane,Smith,15

    So I wrote this:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xdm="urn:xxx:yyy">
    <xsl:eek:utput method="text" indent="yes"/>
    <xsl:strip-space elements="xdm:foo xdm:stuff xdm:morestuff xdm:evenmore
    xdm:stuff_overload"/>

    <xsl:template match="xdm:finally">
    <xsl:apply-templates select="xdm:finally[@name='{first}']"/>
    <xsl:apply-templates select="xdm:finally[@name='{last}']"/>
    <xsl:apply-templates select="xdm:finally[@name='{number}']"/>
    </xsl:template>

    <xsl:template match="xdm:finally[@name='{first}']"><xsl:value-of
    select="xdm:value"/>
    </xsl:template>

    <xsl:template match="xdm:finally[@name='{last}']">,<xsl:value-of
    select="xdm:value"/>
    </xsl:template>

    <xsl:template match="xdm:finally[@name='{number}']">,<xsl:value-of
    select="xdm:value"/><xsl:text>
    </xsl:text>
    </xsl:template>

    </xsl:stylesheet>

    But my output looks like:
    ,DoeJohn,14
    ,SmithJane,15

    I've also tried nesting the templates, and about 100 other things. Any
    ideas on what I'm doing wrong would be a great help.

    -tia
    FlickChick, Jan 20, 2005
    #1
    1. Advertising


  2. > But my output looks like:



    Your template
    xsl:template match="xdm:finally">

    is never called as each of your finally element has a name attribute and
    is matched with higher priority by one of your other templates, so the
    finally elements are processed in the order they appear in the source.

    If it was called it would produce no result as it iis
    <xsl:template match="xdm:finally">
    <xsl:apply-templates select="xdm:finally[@name='{first}']"/>
    <xsl:apply-templates select="xdm:finally[@name='{last}']"/>
    <xsl:apply-templates select="xdm:finally[@name='{number}']"/>
    </xsl:template>


    so it just applies templates to xdm:finally children of the current
    xdm:finally element but there are no such children.

    You want to match on teh parent of teh finally elements so change the
    above to

    <xsl:template match="xdm:stuff_overload">
    <xsl:apply-templates select="xdm:finally[@name='{first}']"/>
    <xsl:apply-templates select="xdm:finally[@name='{last}']"/>
    <xsl:apply-templates select="xdm:finally[@name='{number}']"/>
    </xsl:template>

    David
    David Carlisle, Jan 20, 2005
    #2
    1. Advertising

  3. FlickChick

    FlickChick Guest

    Wow. Thank you. That did it!
    FlickChick, Jan 20, 2005
    #3
  4. FlickChick wrote:

    > I don't usually post questions, but 2 days of googleing and frustration
    > have broken my resistance.


    Who told you to use XSLT for such a simple task ?
    There are tools which are easier to handle.
    The follwing script in XMLgawk works, I tested it.

    BEGIN { XMLMODE=1; OFS="," }

    XMLCHARDATA {
    data = $0
    }

    XMLSTARTELEM == "finally" {
    name = XMLATTR["name"]
    }

    XMLENDELEM == "value" {
    if (name == "{last}")
    last = data
    if (name == "{first}")
    first = data
    if (name == "{number}")
    number = data
    }

    XMLENDELEM == "stuff_overload" {
    print first, last, number
    }

    > I've also tried nesting the templates, and about 100 other things. Any
    > ideas on what I'm doing wrong would be a great help.


    If you need XSL output, XSLT might be the right tool.
    But you were asking for simple ASCII output. For such
    simple tasks there are much simpler ways of processing
    XML data.

    If your only tool is XSLT, everything looks like a template.
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=, Jan 20, 2005
    #4
  5. Jürgen Kahrs <> writes:

    > FlickChick wrote:
    >
    > > I don't usually post questions, but 2 days of googleing and frustration
    > > have broken my resistance.

    >
    > Who told you to use XSLT for such a simple task ?


    It doesn't seem an unreasonable suggestion. The XSLT is shorter and
    arguably simpler that the XMLgawk that you posted. (It's hard for me to
    say if it really is simpler as I read XSLT a lot better than I read AWK)
    It is certainly true that the XSLT is likely to be more portable. It
    is hard to find a mchine without at least one XSLT engine installed these
    days (anything with IE or mozilla or netscape browsers for example)
    but XMLgawk is I would guess rather less available, although no doubt it
    may be easily downloaded/installed.

    David
    David Carlisle, Jan 21, 2005
    #5
  6. David Carlisle wrote:

    > It is certainly true that the XSLT is likely to be more portable. It
    > is hard to find a mchine without at least one XSLT engine installed these
    > days (anything with IE or mozilla or netscape browsers for example)


    Really ? Is there an XSL processor inside the browser ?
    I always thought that the XML processing is usually
    done on the server side.

    > but XMLgawk is I would guess rather less available, although no doubt it
    > may be easily downloaded/installed.


    That's true, XMLgawk is only experimental.
    But there are other tools with larger support
    for handling XML files from the command line
    (xmlstarlet, IIRC).
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=, Jan 21, 2005
    #6
  7. Really ? Is there an XSL processor inside the browser ?
    I always thought that the XML processing is usually
    done on the server side.

    yes mozilla family browsers have xslt built in, and IE6 comes
    automatically set up to use msxml if served an xml file, and msxml
    includes an xslt engine. Not to mention the various java and python
    and ... implementations.

    David
    David Carlisle, Jan 21, 2005
    #7
  8. David Carlisle wrote:

    > yes mozilla family browsers have xslt built in, and IE6 comes
    > automatically set up to use msxml if served an xml file, and msxml
    > includes an xslt engine. Not to mention the various java and python


    Interesting.

    But the original question started with a command-line
    application which should have produced this output:

    > Which I would like to transform into this:
    > John,Doe,14
    > Jane,Smith,15


    No mention of a browser here.
    Anyway, the user has to decide which tool he wants.
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=, Jan 21, 2005
    #8

  9. > No mention of a browser here.


    Quite. You can call msxml from the command line (you have to build
    transformiix (mozilla xslt) from source if you want it stand alone) but
    my comment was not really to the original poster (I'd already replied to
    the XSLT question) but rather to your suggestion that XSLT was a bad
    choice for this kind of transformation. I only mentioned the browser
    implementations as they give an indication of just how widely installed
    XSLT is.

    > Anyway, the user has to decide which tool he wants.

    agreed.

    David
    David Carlisle, Jan 21, 2005
    #9
  10. David Carlisle wrote:
    > Really ? Is there an XSL processor inside the browser ?
    > I always thought that the XML processing is usually
    > done on the server side.
    >
    > yes mozilla family browsers have xslt built in, and IE6 comes
    > automatically set up to use msxml if served an xml file, and msxml


    Have a look at this XSLT bug:

    http://lkml.org/lkml/2005/1/22/

    XSLT error: XML parser error 4: not well-formed (invalid token)
    <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="_#_TEMPLATE_#_"?><list><title>22 Jan

    At least this example looks like the XSLT is
    still running on the server side. I guess that
    browsers with client side XSLT processing are
    still a minority.

    I know that this question has nothing to do with
    the original news thread, but I found your comment
    about client side XSLT processing quite interesting.
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=, Jan 22, 2005
    #10
  11. <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="_#_TEMPLATE_#_"?><list><title>22 Jan

    At least this example looks like the XSLT is
    still running on the server side. I guess that

    No xml-stylesheet PI (almost) always refers to client side xslt.
    Try view source, you'll see the client has the XML source not the
    transform.

    > I guess that
    > browsers with client side XSLT processing are
    > still a minority.


    I think Opera and safari (for example) are important browsers without
    XSLT, but I think that you will find that Internet explorer and
    mozilla/netscape/firefox between them account for a large percentage of
    desktop browser usage (well in excess of 90%) this is hardly a minority.


    > Have a look at this XSLT bug:
    > http://lkml.org/lkml/2005/1/22/


    Not sure what you mean here that page (I looked in firefox and IE)
    doesn't have any XML or client side transformation. It may as you say be
    a server side transform but I can't of course see that from the outside.

    David
    David Carlisle, Jan 23, 2005
    #11
  12. David Carlisle wrote:

    > No xml-stylesheet PI (almost) always refers to client side xslt.
    > Try view source, you'll see the client has the XML source not the
    > transform.


    If PIs are used, I guess that client side XSLT
    is not standardized (is it ?).

    >>Have a look at this XSLT bug:
    >>http://lkml.org/lkml/2005/1/22/

    >
    >
    > Not sure what you mean here that page (I looked in firefox and IE)
    > doesn't have any XML or client side transformation. It may as you say be
    > a server side transform but I can't of course see that from the outside.


    This is a server side XSLT. They obviously have fixed the server.
    Therefore, the error message I saw yesterday does not occur anymore.
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=, Jan 24, 2005
    #12

  13. > If PIs are used, I guess that client side XSLT
    > is not standardized (is it ?).


    The PI in the fragment you quoted is the format of a W3C recommendation
    and supported by all Browsers with XSLt (as well as many command line
    xslt prograns such as saxon)

    David
    David Carlisle, Jan 24, 2005
    #13
  14. David Carlisle wrote:

    > The PI in the fragment you quoted is the format of a W3C recommendation
    > and supported by all Browsers with XSLt (as well as many command line
    > xslt prograns such as saxon)


    Agreed, I admit that I wrote my question in
    a stupid was.

    What is not clear to me is, who decides if
    the XSLT is to be executed on the client side
    or the server side ?

    Thanks for your patience.
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=, Jan 24, 2005
    #14
    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. Soren Kuula
    Replies:
    2
    Views:
    495
    Soren Kuula
    Feb 1, 2004
  2. Replies:
    8
    Views:
    4,530
    Bjorn Abelli
    May 8, 2006
  3. Peter Møllerud
    Replies:
    2
    Views:
    946
    Stefan Behnel
    Sep 9, 2006
  4. cspoh
    Replies:
    0
    Views:
    244
    cspoh
    Jul 31, 2003
  5. Stephan Kämper
    Replies:
    2
    Views:
    232
    Stephan Kämper
    Jan 18, 2004
Loading...

Share This Page