We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5209
    • 10 Posts
    Hey,

    I need to create an XML document using ditto.

    In ModX i have the following:

    Gallery
    |_2008
    |_January
    |_Image A

    etc.. you get the idea.

    How can i take that file tree and create an XML document that looks like this;

    <gallery>
    <year year="2009">
    <January>
    <image id="1" src="assets/2009/January/imageA.jpg">
    </January>
    </year>
    </gallery>

    (obviously i have more years, months and images)


    Any help would be much appreciated!
      • 4172
      • 5,888 Posts
      for me I would use my snippet Xett for this.

      have to create a new projectfolder with the file getdatas.php in the includes-projectfolder:

      <?php
      
      $startid='819';//the gallery-folder
      $imagecount=0;
      $yeardocs=$modx->getDocumentChildren($startid);
      $out_yeardocs=array();
      if (count($yeardocs)>0){
      	foreach($yeardocs as $yeardoc){
      		$year=$yeardoc['pagetitle'];
      		$monthdocs=$modx->getDocumentChildren($yeardoc['id']);
              $out_monthdocs=array();
      		if (count($monthdocs)>0){
      	    foreach($monthdocs as $monthdoc){
      	    $month=$monthdoc['pagetitle'];	
      		$imagedocs=$modx->getDocumentChildren($monthdoc['id']);
      		$out_imagedocs=array();
      		    if (count($imagedocs)>0){
      		    	foreach ($imagedocs as $imagedoc){
      		    		$imagecount++;
      					$imagedoc['count']=$imagecount;
      		    		$imagedoc['month']=$month;
      					$imagedoc['year']=$year;
      					$out_imagedocs[]=$imagedoc;
      		    	}
      		    }
      		    $monthdoc['innerrows']['images']=$out_imagedocs;
      			$out_monthdocs[]=$monthdoc;
      	    }
      	
              }
      		$yeardoc['innerrows']['months']=$out_monthdocs;
      		$out_yeardocs[]=$yeardoc;		
      	}
      	
      }
      
      $xettdatas['innerrows']['years']=$out_yeardocs;
      
      ?>


      and in the templates-projectfolder these files:
      xetouterTpl.html:
      <gallery>
      [+xett.innerrows.years+]
      </gallery>

      yearsTpl.html:
      <year year="[+xett.row.pagetitle+]">
      [+xett.innerrows.months+]
      </year>

      monthsTpl.html:
      <[+xett.row.pagetitle+]>
      [+xett.innerrows.images+]
      </[+xett.row.pagetitle+]>

      and imagesTpl.html:
      <image id="[+xett.row.count+]" src="assets/[+xett.row.year+]/[+xett.row.month+]/[+xett.row.pagetitle+].jpg"/>


      and call it with
      [!Xett? &project_path=`xmltest` &service=`xmltest`!]


      if the new createt projectfolders are

      assets
      -snippets
      --Xett
      ---projectfiles
      ----xmltest
      -----xmltest
      ------templates
      ------includes


      made a test here: http://www.red-dragon-cup.de/bocksbeutel/index.php?id=819 (you have to look to the sourcecode to see the result)
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 5209
        • 10 Posts
        thanks for replying!

        Surely this is possible with just ditto?

        like ditto including a template for the years then that years template includes months then that includes the images?

        can you call ditto from within chunks or snippets?
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          If you want an entire document served as an XML document, just call Ditto with the &format=`xml` and set the document’s Content Type to text/xml. Generating a feed works this way also with &format=`rss`.

          Here are the default tpls used for XML output; you can specify your own chunk tpls in the Ditto call to replace them:
          $xml_header = <<<TPL
          <?xml version="1.0" encoding="[+xml_charset+]" ?>
          [+xml_xsl+]
          <xml version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
          		<channel>
          				<title>[*pagetitle*]</title>
          				<link>[+xml_link+]</link>
          				<description>[*description*]</description>
          				<language>[+xml_lang+]</language>
          				<copyright>[+xml_copyright+]</copyright>
          				<ttl>[+xml_ttl+]</ttl>
          TPL;
          
          $xml_tpl = <<<TPL
          		<item>
          			<title>[+xml_pagetitle+]</title>
          			<link>[(site_url)][~[+id+]~]</link>
          			<summary><![CDATA[ [+xml_introtext+] ]]></summary>
          			<date>[+xml_createdon+]</date>
          			<createdon>[+xml_createdon+]</createdon>
          			<author>[+xml_author+]</author>
          			[+tags+]
          		</item>
          TPL;
          
          $xml_footer = <<<TPL
          			</channel>
          </xml>
          TPL;
          
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 5209
            • 10 Posts
            sorry bruno17, i’m having some difficulty understanding how to implement your plugin.

            Do you have any documentation? Or could you talk me through how to use it?


            Thanks - it looks like your plugin will do the trick (when i get it working!) smiley

            Chris

            EDIT: i’ve just made a quick page outside of modx to generate the xml i required. Thanks for your help all the same smiley