declare option saxon:output "method=text";
declare option saxon:output "omit-xml-declaration=yes";

(: remove all whitespace nodes from elements of the input tree :)
declare function local:strip-space($element){
    element {local-name($element)} {
        $element/@*,
        for $child in $element/node()
        return if ($child instance of text() and normalize-space($child)='')
               then ()
               else if ($child instance of element()) 
                    then local:strip-space($child)
                    else $child
    }
};

declare function local:nl-and-indent($nb){
    concat('
',string-join(for $i in 1 to $nb return ' ',''))
};

declare function local:attribute($attribute){
    concat('@',local-name($attribute),'[',data($attribute),']')
};

declare function local:text($text){
    normalize-space($text)
};

declare function local:element($elem,$indent){
  let $newName := local-name($elem),
      $newIndent := $indent+string-length($newName)+1
  return
      concat(
        $newName,"[",
        string-join(
            for $child at $pos in $elem/(@*|*|text())
            return (
                if($pos>1)
                then local:nl-and-indent($newIndent)
                else (),
                typeswitch ($child)
                    case attribute() return local:attribute($child)
                    case text()      return local:text($child)
                    case element()   return local:element($child,$newIndent)
                default return ()
             ),""),
          "]"
        )
};

local:element(local:strip-space(/*),0)