XSLT optimizations for PDF stylesheets

Manually picked from #1672