Archive for August, 2008

27
Aug

Music to mention…

Some good albums I am listening to now.

20
Aug

Finding the correct XPath…

The idea here is that if a <para> is preceded by 1 or more notes, they need to be grouped with the <para> in a <group> node.  The problem is how do you write the Xpath to only grab the <note> nodes which are between the current <para> and the previous <para> node?

Here is the XML:

<?xml version="1.0"?>
<document>
     <note>First note</note>
     <para>First paragraph</para>
     <note>Second note</note>
     <para>Second paragraph</para>
     <note>Third note</note>
     <note>Fourth note</note>
     <para>Third paragraph</para>
</document>

Here is the XSLT template for the <para> node:

<xsl:template match="para">
    <xsl:choose>
        <xsl:when test="local-name(preceding-sibling::*[1]) = 'note'">
            <xsl:variable name="current_para" select="."/>
              <group>
                <spara>
                   <xsl:apply-templates
                        select="preceding-sibling::note[following-sibling::para[1]=$current_para]"/>
                   <para><xsl:value-of select="."/></para>
                </spara>
              </group>
           </xsl:when>
        <xsl:otherwise>
           <group>
               <para><xsl:value-of select="."/></para>
           </group>
        </xsl:otherwise>
     </xsl:choose>
  </xsl:template>

Here is the output XML:

<?xml version="1.0"?>
<document>
  <group>
    <spara>
       <note>First note</note>
       <para>First paragraph</para>
    </spara>
  </group>
  <group>
    <spara>
       <note>Second note</note>
       <para>Second paragraph</para>
    </spara>
  </group>
  <group>
    <spara>
      <note>Third note</note>
      <note>Fourth note</note>
      <para>Third paragraph</para>
    </spara>
  </group>
</document>

The XPath “preceding-sibling::note” gives us everything that is prior to the current <para>.   The “secret sauce” here is that we are applying a filter to include only <para> nodes which have a “following-sibling::para” which matches the current <para> that we are processing.  If anyone has a more elegant solution, I would be interested in seeing it.

13
Aug

YUI and parsing JSON

Am I missing something here?  I started a web app using YUI.  The server can generate XML or JSON as a response.  I started with XML but I found the parsing rather limited.  I switched to JSON using the XML::XML2JSON module on CPAN.  This produces JSON following a convention set up by Google.  It seems that YUI can not parse data in this format.

{"encoding":"UTF-8","version":"1.0",
 "metadata":{
    "artist-list":{
        "artist":{
           "life-span":{"@begin":"1992-02-14"},
           "sort-name":{"$t":"Weezer"},
           "name":{"$t":"Weezer"},
           "@id":"6fe07aa5-fec0-4eca-a456-f29bff451b04",
           "@ext:score":"100",
           "@type":"Group"
         },
     "@count":"1","@offset":"0"
   }}}

I found that I can get around the invalid “$t” and “-” in the name string by using a different syntax for JSON location code which includes using brackets and single quotes instead of periods.

mySearchDataSource.responseSchema = {
          resultsList: "['metadata']['artist-list']['artist']",
          fields: ["['sort-name']['$t']", "['@type']", "['@ext:score']"],
                 metaFields: {
                    totalRecords: "['metadata']['artist-list']['@count']"
                 }
             };

But YUI still does not like the “@” in the front of names and the “:” in the middle of a name.

I guess I have these choices:

  1. Switch server response to XML.  Write custom parseXMLData method for YUI DataSource.
  2. Write my own parseJSONData method for YUI DataSource.
  3. Write my own XML -> JSON conversion which constucts JSON data that YUI is happy with.
  4. Server side hack to “clean” JSON data of any offending characters.

Now I’m off to do some more research on this…

07
Aug

Blurb.com Recommendation

I recently composed a book of wedding photos through blurb.com.  Now that I have the book printed, I have to say that I am really impressed with this service.  The desktop software used to compose the book is easy to use and very intuitive.  The software was definitely sluggish running on my G4 iMac but it was usable.  The printed book is very high quality.  I chose to get the “Hardcover, ImageWrap” type of book it really is impressive.  Just thought I would pass along this recommendation for this service.  I plan to use this in the future for other photo books now that I have seen the results.