Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > change output order

Reply
Thread Tools

change output order

 
 
FlickChick
Guest
Posts: n/a
 
      01-20-2005
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='urnxx: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"
xmlnssl="http://www.w3.org/1999/XSL/Transform"
xmlnsdm="urnxx:yyy">
<xslutput 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

 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      01-20-2005

> 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

 
Reply With Quote
 
 
 
 
FlickChick
Guest
Posts: n/a
 
      01-20-2005
Wow. Thank you. That did it!

 
Reply With Quote
 
=?ISO-8859-1?Q?J=FCrgen_Kahrs?=
Guest
Posts: n/a
 
      01-20-2005
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.
 
Reply With Quote
 
David Carlisle
Guest
Posts: n/a
 
      01-21-2005
Jürgen Kahrs <(E-Mail Removed)> 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
 
Reply With Quote
 
=?ISO-8859-1?Q?J=FCrgen_Kahrs?=
Guest
Posts: n/a
 
      01-21-2005
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).
 
Reply With Quote
 
David Carlisle
Guest
Posts: n/a
 
      01-21-2005
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

 
Reply With Quote
 
=?ISO-8859-1?Q?J=FCrgen_Kahrs?=
Guest
Posts: n/a
 
      01-21-2005
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.
 
Reply With Quote
 
David Carlisle
Guest
Posts: n/a
 
      01-21-2005


> 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
 
Reply With Quote
 
=?ISO-8859-1?Q?J=FCrgen_Kahrs?=
Guest
Posts: n/a
 
      01-22-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A Paradise DNS address change? What change? There was no change. Tony Neville NZ Computing 7 09-22-2006 01:02 PM
If you get an order # does it mean the order is accepted? =?Utf-8?B?U3RldmUxMDc3?= Windows 64bit 3 05-12-2005 11:46 PM
Traversion order cf. output order in XSL Soren Kuula XML 2 02-01-2004 09:10 AM
In which order are files looked for when loaded/requierd - and what'sthe order of suffixes? Stephan Kämper Ruby 2 01-18-2004 02:07 PM
How to Display DropDownList with preserved order (custom order) cspoh ASP .Net Web Controls 0 07-31-2003 09:19 AM



Advertisments