[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Re: xhtml via xslt failure

Subject: [xsl] Re: xhtml via xslt failure
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxx>
Date: Tue, 17 Dec 2013 10:17:30 -0500

Hi e-letter,

On Mon, Dec 16, 2013 at 6:03 PM, e-letter <inpost@xxxxxxxxx> wrote:
>> To what specification do you refer? Certainly, the XSLT Recommendation
>> (neither versions 1.0 nor 2.0) say this, since the two instructions do
>> not do the same thing.
> Firstly, sorry for delay to reply; am subscribing in digest mode and
> the mulberry mail list server is very slow to show messages. Started
> to search for alternative mail lists and found 'mark-logic'; terrible
> interface but at least it has RSS...
> Anyway, to answer, referring to version 2; it does not state that the
> two methods above are equivalent, that is a personal conclusion based
> upon reading the specifications.

I am sure you will soon understand how different they can be.

It may be helpful to keep in mind that the built-in template rule for
text nodes would (if written out) look like so:

<xsl:template match="text()">
  <xsl:value-of select="."/>

Since this is the rule the processor uses any time a text node is
processed without a better template (provided in the stylesheet), the
processor will emit the values of any text nodes processed, even
without your having to give a value-of instruction (anywhere). This is
generally the best way of proceeding.

>> Under XSLT 1.0, value-of select="./author" (an abbreviation for
>> select="self::node()/child::author") will copy the value of the first
>> 'author' element child (only) of the context into the result. (Under
>> XSLT 2.0 it will copy the values of all 'author' element children.)
>> This is usually not what you want, inasmuch as there may be more than
>> author element.
> The version number was changed, but no effect observed (only the first
> author is selected).

An XSLT 1.0 processor will not observe XSLT 2.0 semantics even if the
stylesheet says "2.0". You haven't said what processor you are using.

>> apply-templates select="author", in contrast, selects all author
>> element children and applies templates to them. As you have not
>> provided an author, the built-in template for elements will be used;
>> it applies templates to the element contents, which results (unless
>> you intervene to change this behavior) in the element's data contents
>> (text node descendants) being copied into the result.
> Surely the statement: "...select='./author'..." provides details of the authors?

Applying templates to the 'author' elements will result in whatever
output those templates specify. Matching an element with an empty
template is a normal way of suppressing its data from the results.

So you will get the details of the authors if your template matching
'author' provides for them. Ordinarily this will be either by matching
templates to elements appearing inside the 'author' elements, or by
dumping the text contents (probably by using the forementioned
built-in template to do so) :->

>> Indeed, using xsl:value-of instead of xsl:apply-templates in such a
>> case, and expecting them to emit the same results, is a good
>> indication that the author of the code does not understand XSLT. They
>> will often emit the same results, but not always, and the difference
>> (when they do and don't) is crucial.
> True about the pre-novice status! To clarify, it was not an
> expectation of identical results, more an observation and subsequent
> question "why". At this early stage, 'apply-templates' seems
> personally preferable.

That is reassuring. Beginners often get off on the wrong foot by using
xsl:value-of, and then needing functionality given by templates.

>> (And using "./author" instead of the simpler and more efficient
>> "author" is an indication that the author doesn't know XPath very well
>> yet. :-)
> Partially ;) , originally 'author' was used, but find using './' to be
> beneficial as a personal reminder of the relative positions of
> elements in the main document tree :)

That is as good a reason as any and better than most.

However, once you learn that "author" is short for "child::author" and
"./author" is short for "self::node()/child::author" you may feel a
little differently about this. :-)

>> You also did not fix the namespace problem I pointed out this morning,
>> which has not bitten you yet, but is likely to do so as your XSLT
>> becomes more complex. Keep an eye out for xmlns="" turning up in your
>> results where you don't want them.
> Sorry, only now read the early message, thanks. This was solved by
> applying the namespace to the element 'p', but the recommendation to
> apply to root of the stylesheet is understood. Was distracted to write
> to w3 and notify that the use of 'prefix' in the context of namespaces
> is incorrect English: for xlmns:html... html is the suffix!

Well, 'html' will be the prefix on element names assigned to the
namespace, e.g. 'html:h1', and that is the sense they mean it.

Cheers, Wendell

Wendell Piez | http://www.wendellpiez.com
XML | XSLT | electronic publishing
Eat Your Vegetables

Current Thread