This project is read-only.

little help to reformat Posterous output

Sep 16, 2010 at 2:51 AM

Hi folks... Just wondering if anyone could help me reformat the following output. I just want to output a simple blog-type format for each "post".

Thanks for any help.

Here's an example source:

<rsp stat="ok"> 
<post> 
  <url>http://post.ly/xmg4</url> 
  <link>http://pack1050.posterous.com/den-10-service-project</link> 
  <title>Den 10 Service Project</title> 
  <id>28123586</id> 
  <body> 
    <![CDATA[>>>><p><img src="http://posterous.com/getfile/files.posterous.com/pack1050/pHvJS8Ab0VJ6CkarWE864hWsUmCw79NrCjBBUGdLwS9ayYiW4dUpRuUYHTnh/photo.jpeg" width="240" height="320"/> 
</p><p>>>>></p>]]>
  </body> 
  <date>Tue, 14 Sep 2010 13:01:12 -0700</date> 
  <views>6</views> 
  <private>false</private> 
  <commentsenabled>true</commentsenabled> 
  <media> 
    <type>image</type> 
    <medium> 
      <url>http://posterous.com/getfile/files.posterous.com/pack1050/pHvJS8Ab0VJ6CkarWE864hWsUmCw79NrCjBBUGdLwS9ayYiW4dUpRuUYHTnh/photo.jpeg</url> 
      <filesize>33</filesize> 
      <height>320</height> 
      <width>240</width> 
    </medium> 
    <thumb> 
      <url>http://posterous.com/getfile/files.posterous.com/pack1050/98qu2dUsm0mJMv8Qm4EW7WY8yGcHHdvJMQpbbg39f5LZIQyPmZ6mmHnsGA9Q/photo.jpeg.thumb.jpg</url> 
      <filesize>33</filesize> 
      <height>36</height> 
      <width>36</width> 
    </thumb> 
  </media> 
  <commentsCount>0</commentsCount> 
</post> 
</rsp>

Coordinator
Sep 16, 2010 at 1:30 PM

Hi,

This is simple to do and I can make a working sample for you if you provide a URL that returns XML.

If to put your sample into an xsl:variable you can use the following code to output a list of records

<xsl:variable name="rsp">
  <!-- Your XML goes here  -->
</xsl:variable>
<xsl:for-each select="mdo:node-set($rsp)//post">
  {{title}} - {{mdo:fmt-date(date, 'dd.MM.yyyy HH:mm')}}
  <img src="{media/thumb/url}" style="float:left;margin-right:5px;" />
  <div style="clear:both;"/>
</xsl:for-each>

Here I used a title, date and thumbnail. If you need to analyse body tag I'd like you to provide more meaningfull sample. To simply output body as HTML you can use

{h{body}}
inside xsl:for-each. ( "{h{" disables HTML encoding. )

 

 

 

 

 

Sep 16, 2010 at 2:05 PM
Edited Sep 16, 2010 at 2:08 PM

Thanks Findy!

Ok... I tried that with the URL and I think I am good to go!

Sep 16, 2010 at 2:53 PM

Is it possible to parse html items within a CDATA tag? When I add the {{body}} to the output, the html is output as plain text. I would like to pull out the <div class="downlloadIcon">...</div>
content and place below the <p>...</p> content.

The actual URL is: http://posterous.com/api/readposts?hostname=pack1050

<![CDATA[ 
       <div class='downloadFlyout'>
       <div class="downloadIcon"><a href='http://posterous.com/getfile/files.posterous.com/pack1050/jF5ypGBIjZDWATLYvbODRaad5auMdDwqtIjtsrHS0uinxCEA8hV4ItzjYYBr/POPCORN_FUNDRAISER.pdf'>
<img src='http://posterous.com/images/filetypes/pdf.png' style='border: none;'/></a></div> <div class="downloadlinkInstructions">Download now or <a href='http://pack1050.posterous.com/popcorn-information-2' style='color: #bc7134;'>preview on posterous</a></div> <b><a href='http://posterous.com/getfile/files.posterous.com/pack1050/jF5ypGBIjZDWATLYvbODRaad5auMdDwqtIjtsrHS0uinxCEA8hV4ItzjYYBr/POPCORN_FUNDRAISER.pdf'>POPCORN FUNDRAISER.pdf</a></b> <span class="downloadSize">(157 KB)</span> <br class="clearboth"/></div> <p>Attached to this post is the popcorn flyer.<p /></p>
]]>
Thanks!

Developer
Sep 16, 2010 at 4:26 PM

jyjohnson,

you can try disabling output-escaping, that is: {h{body}} or 

<xsl:value-of select="body" disable-output-escaping="yes" />

Bye,
al.

Coordinator
Sep 16, 2010 at 6:25 PM

jyjohnson,

This is a short sample. You can find it live here: http://xsltdb.com/jyjohnson.aspx. Here you can see how you can parse body and analyse it using XPath.

<style type="text/css">
  .downloadIcon { float:left; }
</style>

<!-- download feed and put it in cache for 600 seconds -->
<xsl:variable name="rsp" select="mdo:nodeset('http://posterous.com/api/readposts?hostname=pack1050', 600)"/>

<!-- iterate posts -->
<xsl:for-each select="$rsp//post">

  <!-- turn body in XML format: <root> the body </root> -->
  <xsl:variable name="body" select="mdo:node-set(concat('&lt;root&gt;', body, '&lt;/root&gt;'))"/>

  <!-- print date ena title -->
  <div style="padding-bottom:3px;">
    <b>{{mdo:fmt-date(date, 'dd.MM.yyyy HH:mm')}}</b>. {{title}}
  </div>

  <!-- print image if it is in post -->
  <xsl:if test="media/thumb/url">
    <img src="{media/thumb/url}" style="margin-left:5px;"/>
  </xsl:if>

  <!-- if body contain a downloadable document - print body -->
  <xsl:if test="$body//div[@class='downloadIcon']">
    {h{body}}
  </xsl:if>

  <div style="clear:both;" />
  <br/>
</xsl:for-each>