s c h e m a t i c s : c o o k b o o k

/ Cookbook.GnucashImport

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Manipulating Gnucash Files in DrScheme using SSAX

Problem

Gnucash is nice, but its toolset is limited and I want to perform interesting computations on its data

Solution

Use SSAX.

This is strictly preliminary; here's some code that may be helpful to you if you use gnucash (1.8.11) & DrScheme.

(require (planet "ssax.ss" ("lizorkin" "ssax.plt" 1 3))
         (only (lib "list.ss") quicksort)
         (lib "list.ss"))

; this must go in place of the bare <gnc-v2> tag in the file...
(define insert-this-text
  #<<|
<gnc-v2 xmlns:gnc="http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/io-gncxml-version-2.dtd#gnc"
xmlns:book="http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/io-gncxml-version-2.dtd#book"
xmlns:cd="http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/io-gncxml-version-2.dtd#cd"
xmlns:act="http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/account-v2.dtdt#act"
xmlns:trn="http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/transactions-v2.dtd#trn"
xmlns:split="http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/transactions-v2.dtdt#split"
xmlns:cmdty="http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/comodity-v2.dtd#cmdty"
xmlns:ts="http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/types.dtd#ts"
xmlns:slots="http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/types.dtd#slots"
xmlns:slot="http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/types.dtd#slot"
xmlns:price="http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/prices-v1.dtd#price"
xmlns:pricedb="http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/prices-v1.dtd#pricedb">
|
  )

(define all (time (ssax:xml->sxml (open-input-file "~/clements/collects/gnucash/Mugwort-gnucash") '())))

(define elts
  (match all
    [`(*TOP* (@ . ,top-attribs) (gnc-v2 ,count-data (,book-tag (@ . ,book-attribs) . ,content)))
      content]))

(define book-id-tag (string->symbol "http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/io-gncxml-version-2.dtd#book:id"))
(define count-data-tag (string->symbol "http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/io-gncxml-version-2.dtd#gnc:count-data"))
(define commodity-tag (string->symbol "http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/io-gncxml-version-2.dtd#gnc:commodity"))
(define pricedb-tag (string->symbol "http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/io-gncxml-version-2.dtd#gnc:pricedb"))
(define account-tag (string->symbol "http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/io-gncxml-version-2.dtd#gnc:account"))
(define transaction-tag (string->symbol "http://www.gnucash.org/lxr/gnucash/source/src/doc/xml/io-gncxml-version-2.dtd#gnc:transaction"))

(define (tag-filter tag elts)
  (filter (lambda (elt)
            (eq? (car elt) tag))
          elts))

(define book-ids (tag-filter book-id-tag elts))
(define count-data (tag-filter count-data-tag elts))
(define commodities (tag-filter commodity-tag elts))
(define accounts (tag-filter account-tag elts))
(define transactions (tag-filter account-tag elts))

(define (transaction-date transaction)
  (unless (eq? (car transaction) transaction-tag)
    (error 'transaction-date "not a transaction: ~v" transaction))
  (let ([date-fields (tag-filter ,date-entered-tag (sxml-elts transaction))])
    (unless (and (pair? date-fields) (null? (cdr date-fields)))
      (error 'transaction-date "no entered date found for transaction: ~v" transaction))))

;; hmm... off to read up on sxml.

Discussion


Comments about this recipe

Contributors

-- JohnClements - 29 Apr 2006

CookbookForm
TopicType: Recipe
ParentTopic:
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest