Search

I've got some XSLT which relies on the field data coming from a standard datasource to be in a specific order. Unfortunately it seems that the order of the fields is not the same as the order of the fields as specified in the section. Is it possible to control this easily?

i.e.: This is an example of the XML output from my datasource currently

<entry id="64">
    <image size="107 KB" path="/uploads/product-images" type="image/png">
        <filename>imagelarge.png</filename>
        <meta creation="2011-10-22T12:34:30+10:00" width="576" height="492" />
    </image>
    <model-name handle="product-name">Product Name</model-name>
    <type>
        <item handle="pants">Pants</item>
    </type>
</entry>

But I want it in this order (which is the order the fields are defined in the section.)

<entry id="64">
    <model-name handle="product-name">Product Name</model-name>
    <type>
        <item handle="pants">Pants</item>
    </type>
    <image size="107 KB" path="/uploads/product-images" type="image/png">
        <filename>imagelarge.png</filename>
        <meta creation="2011-10-22T12:34:30+10:00" width="576" height="492" />
    </image>
</entry>

Is there an easy way to do this? My other option would be to use the output of a section schema datasource to match each element that way, as they are listed in the correct order. My XSLT is already getting out of hand though so I'd rather avoid it.

Well, I went and had a look at how the entry manager was building the data, and the order was getting messed up when it looks up the field id's.

                $schema_sql = sprintf(
                "SELECT `id` FROM `sym_fields` WHERE `parent_section` = %d AND `element_name` IN ('%s')",
                $section_id,
                implode("', '", array_unique($element_names))
            );

I'm assuming MySQL was just applying its internal sorting.

For now I've changed it to:

                $schema_sql = sprintf(
                "SELECT `id` FROM `sym_fields` WHERE `parent_section` = %d AND `element_name` IN ('%s') ORDER BY `sym_fields`.`sortorder`",
                $section_id,
                implode("', '", array_unique($element_names))
            );

I'm going to do some testing and see if anything breaks. Can anyone tell me why this might be a stupid idea?

This behaviour has always bugged me a little bit. Especially when I add a new field, put it in a specific location in the section, add it to the datasource, then I have to go find where it's been randomly placed in the debug output.

I did a bit of benchmarking (nothing too scientific) but couldn't see it affecting the DS generation time at all.

Sorting is an expensive operation in every database manager. So the only problem might be that it will be a little bit slower (you won't notice it with just a few simple fields though.

Yeah, the performance hit was my only other concern (apart from other unintended consequences). I've continued development on the site in question and haven't noticed any unusual behaviour, so I'm going to be keeping the modification in for now.

@Henry: In XML the order of elements has no meaning. So you should not rely on it in your XSLT. That would be conceptually wrong.

Nevertheless I often thought "Why is it ordered like this?", especially when debugging. It would be easier (and look "nicer") if the order of elements was the same as in the section.

What do others think?

@Henry: In XML the order of elements has no meaning. So you should not rely on it in your XSLT. That would be conceptually wrong.

I am not sure you're right in doing that generalization. It is true that XML 1.0 doesn't specify the order of elements but in Symphony we do heavily rely on the "by occurrence in document"-order of the XML. Take the Order-By-attribute in any of your DS'es for example. Also, without order, none of your HTML (wich is XML before the transformation) wouldn't make any sense:

<ol>
   <li>Install Symphony</li>
   <li>Build Site</li>
   <li>???</li>
   <li>Profit!</li>
</ol>

You're right though that the order of your Section-fields in the XML should not be important for your site. That's not an issue with XML though but with the way Symphony creates the XML from your data.

@phoque: Yep, agreed. But what you think about @Henry's suggestion (to "beautify" the order of field elements in the XML)?

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