XPTO: An XPath preprocessor with type-aware optimization. Various languages allow specific query languages for selection and transformation of portions of documents. Such queries are defined generically for different data types, and only specify specific behaviours for a few relevant subtypes. This is a well-known feature of XML query languages, that allow selection of element nodes without exhaustively specifying intermediate nodes. We have implemented a system for performing optimizations on XPath expressions through schema-specialization of their structure-shy properties. The core of the system consists of a combinator library, based on algebraic laws for transformation of structure-shy programs, conversion into structure-sensitive programs, and vice-versa. We show how the core library can be extended with laws for specific XPath features and adapted to construct an effective rewrite system for specialization and optimization of XPath structure-shy programs. The front-end for this system carries the conversion of XML Schema and XPath files into internal representations and the generation of Haskell programs containing optimized queries as Haskell functions. The front-end itself was implemented over the functional language Haskell.