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. 5 2001
> 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
Derek Fountain wrote: Dimitre Novatchev wrote:As you're re-creating exactly the source xml document, it's much simpler to use the identity template:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template>
</xsl:stylesheet>
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
>>><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template>
</xsl:stylesheet>
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. :o)
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?
> 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?" :o)
> > 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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: nanookfan |
last post by:
Hi all,
I'm having a bizarre problem converting XML files to HTML using an
XSLT. The problem is only occuring in my Netscape 7.0 browser. What
makes it more bizarre is that it is only...
|
by: kmunderwood |
last post by:
I am having trouble excluding select xml out to HTML using xsl
I want to ignore some xml and turn others red
I can not find the right way to both:
1. Only show the <tag> that want to, and...
|
by: Randall Parker |
last post by:
I'm wondering if there are general rules for the use of HTML attributes with ASP tags?
For example, one can set an id for an asp:DataGrid and it will show up as the id for
the resulting HTML...
|
by: sarada7 |
last post by:
Hi,
Is there a way to encode/decode HTML using C++??
Thanks,
Sarada.
|
by: kewalmehra |
last post by:
Hi All,
I have a requirement of saving Excel files as HTML using C#. I have
managed to write code for saving as HTLM file. however this seems to be
not working in the once perticular case .
...
|
by: Kewal |
last post by:
Hi All,
I have a requirement of saving Excel files as HTML using C#. I have
managed to write code for saving as HTLM file. however this seems to be
not working in the once perticular case .
...
|
by: antony78 |
last post by:
Hi,
Could you please tell me how to retrieve the values in the select tag of HTML using JSP.
eg:
<select name="", id="",>
<option value="1">1</option>
</select>.
I want to retrieve the...
|
by: roop1 |
last post by:
Hello gits,
I found this snippet that you offered to another person and I was wondering if it wouldn't work for me as well:
<script type="text/javascript">
function...
|
by: Izhaki |
last post by:
Hi,
I'm creating a system where my XML includes HTML tags (<h1></h1>) in addition to other XML elements (<book></book>).
I would like to render the HTML tags back to HTML using XSL. Considering...
|
by: Bill Downing |
last post by:
I need help finishing a visual basic program that converts SVG to HTML using LINQ. The input in svg.xml should produce the output in target.html
Here is my svg.xml code:
<?xml version="1.0"...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| | |