change output order

F

FlickChick

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
 
D

David Carlisle

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
 
?

=?ISO-8859-1?Q?J=FCrgen_Kahrs?=

FlickChick said:
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.
 
D

David Carlisle

Jürgen Kahrs said:
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
 
?

=?ISO-8859-1?Q?J=FCrgen_Kahrs?=

David said:
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).
 
D

David Carlisle

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
 
?

=?ISO-8859-1?Q?J=FCrgen_Kahrs?=

David said:
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.
 
D

David Carlisle

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
 
?

=?ISO-8859-1?Q?J=FCrgen_Kahrs?=

David said:
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.
 
D

David Carlisle

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

=?ISO-8859-1?Q?J=FCrgen_Kahrs?=

David said:
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 ?).
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.
 
D

David Carlisle

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
 
?

=?ISO-8859-1?Q?J=FCrgen_Kahrs?=

David said:
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.
 

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,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top