diazo icon indicating copy to clipboard operation
diazo copied to clipboard

Whitespace breaks xsl:attribute when Modifying theme on-the-fly

Open davilima6 opened this issue 10 years ago • 5 comments

This works:

  <replace css:theme-children="#portal-logo">
    <img alt="Site Logo" class="img-responsive"><xsl:attribute name="src"><xsl:value-of select="$site_logo" /></xsl:attribute></img>
  </replace>

This does not:

  <replace css:theme-children="#portal-logo">
    <img alt="Site Logo" class="img-responsive">
      <xsl:attribute name="src"><xsl:value-of select="$site_logo" /></xsl:attribute>
    </img>
  </replace>

Even this won't work (one single whitespace before xsl:attribute):

  <replace css:theme-children="#portal-logo">
    <img alt="Site Logo" class="img-responsive"> <xsl:attribute name="src"><xsl:value-of select="$site_logo" /></xsl:attribute></img>
  </replace>

Here's the error msg:

_runtime error, element 'attribute' [123:0] xsl:attribute: Cannot add attributes to an element if children have been already added to the element. [0:0]_

Using Plone 4.3.

/cc @ebrehault @davisagli @lrowe

davilima6 avatar Nov 12 '15 14:11 davilima6

Relates to http://stackoverflow.com/questions/21634539/how-can-i-insert-a-diazo-theme-parameter-into-some-themes-class-attribute#comment32724855_21648460

davilima6 avatar Nov 12 '15 18:11 davilima6

My initial reaction is to just close this as whitespace is significant in html and diazo puposefully preserves it - the whitespace following the open tag is therefore treated as a text node and any xsl:attributes may not follow a text node.

We do currently special case blank text node children of xsl elements: https://github.com/plone/diazo/blob/1.2.2/lib/diazo/emit-stylesheet.xsl#L261-L271 so perhaps an argument could be made for special casing blank text nodes that immediately lead or follow an xsl element. That would change behaviour of code like this though:

  <replace css:theme="#welcome">
    <p>Hello <include css:content="#username"/></p>
  </replace>

lrowe avatar Nov 12 '15 19:11 lrowe

Thanks for the quick response, Lawrence. Let me see if I understand. Are you saying that in order to make room for my proposed usecase the result of your example rule would be <p>Hellousername</p>, without whitespace? I mean, can't we have the best of both worlds?

If that's an XSL limitation then we should just document it better, since this syntax seems to differ from HTML nodes expected behavior.

davilima6 avatar Nov 13 '15 14:11 davilima6

I got my example case slightly wrong as it would only affect whitespace only text nodes:

  <replace css:theme-content="#welcome">
    <b>Hello</b> <xsl:value-of select="$username"/>
  </replace>

Currently it produces <b>Hello</b> username whereas to support your usecase we'd need to special case blank text nodes before and after an xsl element the same as a blank child node of an xsl element, so you'd get ``Hellousername` without the space.

lrowe avatar Nov 13 '15 21:11 lrowe

Does it make sense to add such a special case? or should we just improve documentation?

frapell avatar Nov 19 '15 15:11 frapell