XML to HTML with XSL, using attributes?

D

Digital Puer

I am trying to use XSL to parse XML into HTML, but
I'm having a hard time with parsing XML attributes.

My XML looks like this:

<td> data </td>
<td width="10"> data </td>

I want the resulting HTML to look exactly like the above!

My XSL looks like this:
----
<xsl:template match="td">
<td>
<xsl:attribute name="width">
<xsl:value-of select="@width"/>
</xsl:attribute>
<xsl:apply-templates/>
</td>
</xsl:template>
----

However, this produces:

<td width=""> data </td>
<td width="10"> data </td>


Can someone answer the following questions:
1. How can the 'width=""' be removed if there is no 'width' attribute'?

2. can someone comment if the above template is the right way to go
to process XML attributes, in general? I'm a newbie. thanks.
 
D

Dimitre Novatchev

Nope, After much staring, I can't work it out! What is this doing?
It appears to match any attribute or node() - or possibly any element with
an attribute or node()? I'm not sure.

It then shallow copies whatever it's matched (suggesting the match does
actually match elements), then applies templates to, er, whatever it
matches itself. That doesn't make sense, even to me, and I wrote it!

I give up! Can you explain this in newbie terms?

In such case the best advice is to read a good XSLT book -- e.g. Jeni
Tenisson's "Beginning XSLT".

Read also: http://www.w3.org/TR/xslt#copying

This template copies every node and its attributes, then applies itself
(note that we don't have any other competing template) on the children of
this node -- and so on recursively, until no node matched has any children.
Thus the complete tree starting from the root node to the leaves is copied.


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 
S

Stuart A Yeates

Derek said:
It appears to match any attribute or node() - or possibly any element with
an attribute or node()? I'm not sure.

It then shallow copies whatever it's matched (suggesting the match does
actually match elements), then applies templates to, er, whatever it
matches itself. That doesn't make sense, even to me, and I wrote it!

I give up! Can you explain this in newbie terms?

You're right, it matches attribute or node then it makes a shallow copy
of the attribute or node and recursively calls itself on all
attributes or nodes in the current context (i.e. any attributes or nodes
that are children original matched attribute or node).

stuart
 
D

Derek Fountain

<xsl:stylesheet version="1.0"
You're right, it matches attribute or node then it makes a shallow copy
of the attribute or node and recursively calls itself on all
attributes or nodes in the current context (i.e. any attributes or nodes
that are children original matched attribute or node).

OK, a bit of experimentation with an XPath evaluator shows what it matches,
and I can understand what is going on. :eek:)

Why use this instead of <xsl:copy-of>? That does a deep copy of an element,
including all its attributes and nodes, doesn't it?
 
D

Derek Fountain

Because one can override this very general tempalate with templates
specific just for a given element or attribute and thus solve such tasks

Ah, yes, OK. That makes sense.
Once again, this is basic stuff that is best studied with a good book.
Asking isolated questions in a newsgroup will not provide the systematic
knowledge that one will acquire from such a book just in one month's time.

I'm doing both. I've been working through O'Reilly's XSLT book for a few
days now, and, despite the typos, it's teaching me a lot. However, real
world examples, like those found in this NG, add a depth and challenge to
understanding that you just can't get from a book.

For me, taking a newbie's question from here and working with the book, the
XSLT/XPath specs and some test code, to find out the answer for myself
produces a much better understanding than just working through the book. Of
course, it does mean that if I can't work it out, and I don't understand
the answers posted in the NG, I have to ask for line by line clarification.
I suppose that could annoy some people - I must sound like a little kid
going "why? why? whhhhhhy?" :eek:)
 
D

Dimitre Novatchev

Once again, this is basic stuff that is best studied with a good book.
time.

I'm doing both. I've been working through O'Reilly's XSLT book for a few
days now, and, despite the typos, it's teaching me a lot. However, real
world examples, like those found in this NG, add a depth and challenge to
understanding that you just can't get from a book.

If you mean Sal Mangano's book -- yes, it is an excellent book. However it
is not intended for beginners. I would rather recommend Jeni Tenissons
"beginning XSLT".


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 

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

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top