FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious exampleNext example

Using XML-format files as databases

Description
  • Using xpath search expressions to retrieve (lists of) nodes
  • Editing an XML file: adding, copying, deleting nodes
  • Configuring the appearance (e.g. spacing) of XML output files
The example 'booksearch.mos' works with a database file documenting the example models from the book 'Applications of optimization with Xpress-MP'.

The example 'xmlrefexpl.mos' works with a personnel database, retrieving and modifying some of its entries.

Further explanation of this example: 'Mosel Language Reference', Chapter 'mmxml'


Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
booksearch.mos[download]
xmlrefexpl.mos[download]

Data Files





booksearch.mos

(!******************************************************
   Mosel Example Problems
   ======================

   file booksearch.mos
   ```````````````````
   Retrieving information from an XML database file.
   
  (c) 2010 Fair Isaac Corporation
      author: S.Heipcke, July 2010, rev. Sep 2012
*******************************************************!)

model "Search book examples"
 uses "mmxml"
 
 declarations
   ExampleDB: xmldoc
   Chapters, Models, Ratings, Files: list of integer 
 end-declarations

! Read in the XML file
 load(ExampleDB, "bookexamples.xml")          


! **** Get all chapters
 getnodes(ExampleDB, "/examples/chapter", Chapters)
 writeln("Chapter titles:\n  ", 
         union(c in Chapters) {getattr(ExampleDB, c, "title")})


! **** Search for all difficult examples (difficulty rating 4 or larger)
 writeln("Difficult examples:")
 getnodes(ExampleDB, "/examples/chapter/model/modRating[number()>=4]", Ratings)
 forall(r in Ratings) do
   m:=getparent(ExampleDB, r)
   t:=getnode(ExampleDB, m, "modTitle")
   writeln("  Example: ", getattr(ExampleDB, m, "id"), " ",
           getvalue(ExampleDB, t), " (rating: ",
           getvalue(ExampleDB, r), ")")
 end-do

(! Alternatively: 
 getnodes(ExampleDB, "//model/modRating[number()>=4]/..", Models)
 forall(e in Models) do
   t:=getnode(ExampleDB, e, "modTitle")
   writeln("  Example: ", getattr(ExampleDB, e, "id"), " ",
           getvalue(ExampleDB, t), " (rating: ",
           getvalue(ExampleDB, getnode(ExampleDB, e, "modRating")), ")")
 end-do
!)


! **** Search for all model files where "SOS" is mentioned among features
 writeln("Models using SOS:")
 forall(c in Chapters) do
   getnodes(ExampleDB, c,
            "model/modFeatures[contains(string(),'SOS')]/../modFile", Files)
   forall(f in Files)
      writeln("  File: ", getattr(ExampleDB, c, "directory"), "/",
              getvalue(ExampleDB, f))
 end-do


! **** Search for all examples with several model files
 writeln("Examples with several model files:")
 getnodes(ExampleDB, 
          "/examples/chapter/model/modFile[position()=2]/..", Models)
 forall(e in Models) do
   getnodes(ExampleDB, e, "modFile[position()>2]", Files)
   writeln("  Example: ", getattr(ExampleDB, e, "id"), " (",
           2+Files.size, " files)")
 end-do
 
end-model 

Back to examples browserPrevious exampleNext example