xsl-list
[Top] [All Lists]

[xsl] Re: xhtml via xslt failure

2013-12-16 17:03:12

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.


Why is it that when the element 'value-of' in the stylesheet above is
substituted for the element 'apply-templates', the result is a
truncation of the third line in the xhtml file, i.e. to:


...
<p>James McGovern</p>
...


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).


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?


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.


(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 :)


Also, it it possible to add whitespace  and punctuation to the xhtml output, 
eg.


...
<p>James McGovern, Per Bothner, Kurt Cagle, James Linn, Vaidyanathan
Nagarajan</p>


Yes, it is possible. In fact, to do so, use a template matching 'author':


<xsl:template match="author">
  <!-- provide a comma if not the first author -->
  <xsl:if test="preceding-sibling::author">, </xsl:if>
  <!-- process the element contents -->
  <xsl:apply-templates/>
</xsl:template>


(Of course this solution is available only if you apply templates to
the 'author' elements instead of using value-of.)


In XSLT 2.0 you could skip the templates and say


<xsl:value-of select="author" separator=", "/>


But I would recommend this only if 'author' elements never have any
structure (markup) but only text.


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!

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--