Search

Weird question: is it possible to output the following markup:

<ul>
<li>one</li
><li>two</li
><li>three</li>
</ul>

by means of XSLT to get rid of the extra white space between inline block elements?

You could use

<xsl:strip-space elements="ul, li" />

This is a top level element that is declared under the xsl:stylesheet level, and would apply to all elements specified in it, throughout your html, so all uls and lis would have the white space stripped.

It would result in

<ul><li>one</li><li>two</li><li>three</li></ul>

Thank you John for you super quick reply: I just tried using <xsl:strip-space elements="ul, li" /> as you suggested but unfortunately I was not able to achieve the expected result.

From I was able to understand it’s because xsl:strip-space operates on the nodes of the source XML document but not on the level of the stylesheet, so it works if the following code:

<ul>
    <li>one</li>
    <li>two</li>
    <li>three</li>
</ul>

is already part of the XML (as a HTML output produced by Markdown for example). But if I’m creating the list on the level of the stylesheet, for example:

<xsl:template match="list">
    <ul><xsl:apply-templates select="list-item"/></ul>
</xsl:template>

<xsl:template match="list/list-item">
    <li><xsl:value-of select="*"/></li>
</xsl:template>

and I use xsl:strip-space to get rid of the extra white space, I’m misusing it. I’m wondering if there’s any way to achieve:

<ul><li>one</li><li>two</li><li>three</li></ul>

without setting the indent to no. A sort of localized-no-indentation. No idea if something like this could be achievable somehow.

Yeah, it is for the source actually, you're right.

Try

<xsl:strip-space elements="list, list-item" />

When I use this with * on output, it always strips all of the whitespace in the html, so I guess it's a matter of matching the elements that hold the content originally then.

John, thank you again for the help. I guess I’m missing something because I’m not able to strip any white space in my output, even by targeting every element through <xsl:strip-space elements=“*”/>: the result is always the same.

result

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

<xsl:import href="../utilities/feet.xsl"/>
<xsl:import href="../utilities/footer.xsl"/>
<xsl:import href="../utilities/head.xsl"/>
<xsl:import href="../utilities/header.xsl"/>
<xsl:import href="../utilities/nav-main.xsl"/>
<xsl:import href="../utilities/texts.xsl"/>

<xsl:strip-space elements="*"/>

<xsl:output method="xml"
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    omit-xml-declaration="yes"
    encoding="UTF-8"
    indent="yes" />

Am I using it correctly?

You will have to use your browser's 'View Source' mode, not view it in an inspector. Inspectors re-parse all markup to get their hierarchy view.

Other than that, the xsl:output needs to have indent="no".

@theBigMandarino

well you should use a ninja technique to remove those spaces.

add this to utilities/typography.xsl

<xsl:template match="*" mode="html">
    <xsl:value-of select="normalize-space()"/>
</xsl:template>

and the white line from the picture will disappear

more about the ninja technique here

Attachments:
white_space.png

John and Constantin,

thank you very much for your help—I tried both your suggestions but unfortunately I was not able to make any of them working… I’ve even tried to reduce variables in play to the minimum configuring a super basic installation of Symphony which outputs just a list of items (here it is exported as an ensemble), but still I haven’t achieved any successful result yet.

If I set the indent in the xsl:output to no everything is stacked in a single line: it works but it’s a rather ugly solution since the readability of the source is equal to 0, plus HTML5 Doctype extension ceases to work.

I tried implementing the normalize-space() ninja technique as well, but I was not able to make it working either: I think it’s because such a solution normalizes the space of the item in the XML, but not the white space in the XSL.

I really don’t know what I’m doing wrong/missing, but looking at the source of a page of the Soario website:

Soario source

it seems something like this:

<ul><li>one</li><li>two</li><li>three</li></ul>

should be achievable (or not?).

I would also love a solution for this, but also not lucky so far.

Maybe I am a bit nearer to solve this?

<ul><xsl:text> </xsl:text>
    <li>one</li>
    <li>two</li>
    <li>three</li>
</ul>

Basically, you need to do something weird in that code block in order to have the XSLT to locally output HTML without indentation, like supplying a space for a HTML element where it should not be.

A cleaner solution would be to do this

ul {
    font-size: 0;
}

ul li {
    display: inline-block;
    font-size: 14px;
}

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details