Search

Hello.

I am trying to genereate ul/li tree from the XML I posted to pastie.org. The XML contains one branch only (I can add more, if needed):

Czech Republic —> Prague —> Prague Castle

Here is the pastie entry: Pastie

Note: The section “Places” of course contains lot of other information pieces that were stripped from the sample XML off for better readibility…

Both sections “Places” and “Places Keys” are linked together with the “key” field. I simply can not find an elegant way how to list the “Places” by the hierarchy stored in “Places Keys”. Can anyone help me with it, please?

(I know it’s not Symphony-related, but still may be useful for somebody in the future too who would eventually use similar schema…)

hi,

if you can create a sample of your desired output then i can certainly help!

We had a similar problem over in the “Making Navigation” thread.

<ul>
    <xsl:apply-templates select="place-keys/entry[not(parent/item)]" mode="parent"  />
</ul>

<xsl:template match="place-keys/entry" mode="parent">
    <li><xsl:value-of select="title" />
        <ul>
            <xsl:apply-templates select="../entry[parent/item/@id = current()/@id]" mode="child" />
        </ul>
    </li>   
</xsl:template>

<xsl:template match="place-keys/entry" mode="child">
    <li><xsl:value-of select="title" />
        <ul>
            <xsl:apply-templates select="/data/places/entry[key/item/@id = current()/@id]" />
        </ul>
    </li>
</xsl:template>

<xsl:template match="places/entry">
    <li><xsl:value-of select="title" /></li>
</xsl:template>

@Treemonkey:

It does not matter very much. In general some hierarchy constructed of ul/li tags suite my needs, like:

<ul>
  <li>
    <div>Czech Republic</div>
    <ul>
      <li>
        <div>Prague</div>
        <ul>
          <li>
            <div>Prague Castle</div>
          </li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

Or something alike. Very general problem, I must say. I use the schema “Foo Keys” section vs. “Foos” section almost everywhere in the project (for some special reasons). I wanted to know how to cope with listing the hiearchy the way I described…

@phoque: Thanks. I will look at it later… It looks promising (Non-recurring un-named template solution? Looks cool!) but I can not test it right now…

It looks like a kind of destination structure you’re building? I have experience in doing something like this, but it got very technical… Custom Datasources, lots of xslt…

@designermonkey: I am not sure what exactly does the “destination structure” mean. Could you please provide some simple example of what is it? Then I may be able to provide you answer to your question, I hope.

@phoque: In fact, I am at the point I am trying to understand your solution, because the lack of recursive named template interested me very much and I am not yet familiar with the “mode” conceptions but it looks very promising, I must say… (once one understands it)

UPDATE: I got some problems to make it work. The hier. structure can be done by using the @id param., but the names (titles) can not be linked by @id having different unique value while beying from other section. The titles must be linked by @handle or the key value itself. I am not sure how to achieve it.

Hello.

After some 1 or 2 hours of struggling I found out the following way (critics from you is highly expected, needed and appreciated). Here it is:

<?xml version="1.0" encoding="UTF-8" ?>

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

  <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" />

  <xsl:template match="/">
    <html>
      <head />
      <body>
        <xsl:apply-templates />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="data">
    <ul>
      <xsl:apply-templates
        select="place-keys/entry[not(parent/item)]" />
    </ul>
  </xsl:template>

  <xsl:template match="place-keys/entry">
    <li>
      <xsl:apply-templates
        select="../../places/entry/name[@handle = current()/key/@handle]" />
      <xsl:variable name="branches"
        select="../entry[parent/item/@handle = current()/key/@handle]" />
      <xsl:if test="$branches">
        <ul>
          <xsl:apply-templates select="$branches" />
        </ul>
      </xsl:if>
    </li>  
  </xsl:template>

  <xsl:template match="places/entry/name">
    <div>
      <xsl:apply-templates />
    </div>
  </xsl:template>

</xsl:stylesheet>

Here is the XML data-source to be seen.

So, what do you think? How to improve it? Are there some fatal mistakes? It seems to work just fine, but you know…

Perhaps I learned something today, perhaps not, but I would like to know that precisely… Thank you.

I have looked at this now, there could be an issue in the case that you wanted for exaple saint vitus cathedral in two sections.

but for your current use it looks very streamlined an importantly keeps a nice level of simplicity! well done :)

@Treemonkey: Oh, you’re definitely right. It may happen I will need to place one place under more than one places. I forgot about it completely. Can it be fixes in some simple way? I am not sure about it. :(

I had a quick look and couldn’t see a quick solution, although i know its possible to generate keys which may handle the situation in finding distinct values. Although its quite complex!

your data stucture is good tho for getting your head around various common problems with xslt layouts :) cant think where i used keys, must have something laying around for you to look at ^^

@Treemonkey: you mean the xsl:key and the key() function?

Anyway, thanks a lot.

yep, having looked at possible combinations i cant see there actually being any need for keys!

if you have any problems later then it might be worth looking into it.

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