Why doesn't this XSLT work?

S

Sebastien B.

I've trying to modify an XHTML document, copying everything but replacing
the divs with alternate content (simplified stylesheet below), but they
never get replaced

According to the O'Reilly book I have and the 5-6 examples I've seen, this
is correct; so what am I missing?

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="html" indent="yes" />

<xsl:template match="div">
<div style="border: 1px solid red">
blah blah blah
</div>
</xsl:template>

<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="comment()|processing-instruction()">
<xsl:copy />
</xsl:template>

</xsl:stylesheet>
 
J

Joris Gillis

I've trying to modify an XHTML document, copying everything but replacing
the divs with alternate content (simplified stylesheet below), but they
never get replaced

According to the O'Reilly book I have and the 5-6 examples I've seen, this
is correct; so what am I missing?
Hi,

That's a very frustrating bug.
The problem is the default namespace in your XHTML. (probably "http://www.w3.org/1999/xhtml")
You have to include that namspace in the xsl and add a prefix to the elements that should be matched.
For example:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml">

<xsl:eek:utput method="xml" indent="yes"/>

<xsl:template match="xhtml:div">
<div style="border: 1px solid red">
blah blah blah
</div>
</xsl:template>

<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="comment()|processing-instruction()">
<xsl:copy />
</xsl:template>

</xsl:stylesheet>


regards,
 
J

Joris Gillis

S

Sebastien B.

Hi,
That's a very frustrating bug.

Now when you say that, do you mean a bug in my template, or a bug in the
implementation (ie. would I be better off using a 2.x template instead)?
 
J

Joris Gillis

That's a very frustrating bug.
Now when you say that, do you mean a bug in my template, or a bug in the
implementation

No no, I just wanted to say it's frustrating for someone learning XSLT to suddenly come across a stylesheet that refuses to work because of a detail you're overlooking. I once spent 4 hours looking for the cause of a similar bug.
(ie. would I be better off using a 2.x template instead)?
2.0 does not yet exist, so you can't use it. Besides the specific problem here (namespace issue) is quite fundamental, I don't think there will be major changes in XSLT2.0 concerning namespaces of the source document (I could be wrong though).

regards,
 
D

David Carlisle

Joris Gillis said:
No no, I just wanted to say it's frustrating for someone learning XSLT
to suddenly come across a stylesheet that refuses to work because of a
detail you're overlooking. I once spent 4 hours looking for the cause
of a similar bug.

2.0 does not yet exist, so you can't use it.


well it exists in working draft stage and there exist fairly complete
implementations so you can use it (although being a draft it may be
advisable not to use for production code yet) see saxon 8.x
Besides the specific problem here (namespace issue) is quite
fundamental, I don't think there will be major changes in XSLT2.0
concerning namespaces of the source document (I could be wrong
though).

The main change in this area is that you can set a default namespace for
element names in XPath expressions, so that you don't have to explictly
prefix element names in XPath and XSLT patterns in order to refer to
elements in a namespace.



David
 
J

Joris Gillis

(ie. would I be better off using a 2.x template instead)?
well it exists in working draft stage and there exist fairly complete
implementations so you can use it (although being a draft it may be
advisable not to use for production code yet) see saxon 8.x

Yes, but theoretically, it can always be recalled and redesigned from scratch.
The main change in this area is that you can set a default namespace for
element names in XPath expressions, so that you don't have to explictly
prefix element names in XPath and XSLT patterns in order to refer to
elements in a namespace.

That's indeed quite interesting...
Up until now, I never took a good look at XSLT2.0 , maybe I should start learning it while it is still in working draft stage. Do you have any idea about the timescale (when will the recommendation be finished?) or is that unpredictable?

regards,
 
D

David Carlisle

Do you have any idea about the timescale (when will the recommendation
be finished?) or is that unpredictable?

Late next year they reckon, XSLT has been very stable with very few
changes between drafts, mainly XSLT2 has been playing catch up while
Xquery got developed (as they both depend on XPath2, all three need to
be finalised together). Xquery has now been through "last call" so it is
also getting fairly stable. I don't expect any large changes now, the
time is required to get through teh W3C review processes, and show
interoperable implementations, etc.


Note, I'm not on the working group, this is all just unreliable gossip:)

David
 
J

Joris Gillis

Do you have any idea about the timescale (when will the recommendation
Late next year they reckon, XSLT has been very stable with very few
changes between drafts, mainly XSLT2 has been playing catch up while
Xquery got developed (as they both depend on XPath2, all three need to
be finalised together). Xquery has now been through "last call" so it is
also getting fairly stable. I don't expect any large changes now, the
time is required to get through the W3C review processes, and show
interoperable implementations, etc.

That looks very promising. Patience will finally be rewarded...
Note, I'm not on the working group, this is all just unreliable gossip:)
okay;)
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top