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

Using XML-format files as databases

  • 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.

Data Files


   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"
   ExampleDB: xmldoc
   Chapters, Models, Ratings, Files: list of integer 

! 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), ")")

(! 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")), ")")

! **** 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))

! **** Search for all examples with several model files
 writeln("Examples with several model files:")
          "/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)")

Back to examples browserPrevious exampleNext example