views:

450

answers:

1

I want to automatically format an XML schema definition file. All the normal pretty-print stuff: linebreaks after end-element, indentiing. I have seen this answer, and this elisp, which gives me the basics. Beyond what is there, though, I would like line-breaks between attributes within angle-brackets.

Like so. Before:

<s:schema elementFormDefault="qualified" targetNamespace="urn:Cheeso.2009.05.Finance/TransferObject/TransactionDetail/" xmlns:tns="urn:Cheeso.2009.05.Finance/TransferObject/TransactionDetail/"  xmlns:detail="urn:Cheeso.2009.05.Finance/TransferObject/TransactionDetail/"  xmlns:to="urn:Cheeso.2009.05.Finance/TransferObject/"  xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:address="urn:Cheeso.2009.05.Finance/TransferObject/Address/" xmlns:caller="urn:Cheeso.2009.05.Finance/TransferObject/Caller/" xmlns:gwy="urn:Cheeso.2009.05.Finance/TransferObject/Gateway/" xmlns:tender="urn:Cheeso.2009.05.Finance/TransferObject/Tender/"  >
 ...
</s:schema>

After:

<s:schema
    elementFormDefault = "qualified"
    targetNamespace    = "urn:Cheeso.2009.05.Finance/TransferObject/TransactionDetail/"
    xmlns:tns          = "urn:Cheeso.2009.05.Finance/TransferObject/TransactionDetail/"
    xmlns:detail       = "urn:Cheeso.2009.05.Finance/TransferObject/TransactionDetail/"
    xmlns:to           = "urn:Cheeso.2009.05.Finance/TransferObject/"
    xmlns:s            = "http://www.w3.org/2001/XMLSchema"
    xmlns:address      = "urn:Cheeso.2009.05.Finance/TransferObject/Address/"
    xmlns:caller       = "urn:Cheeso.2009.05.Finance/TransferObject/Caller/"
    xmlns:gwy          = "urn:Cheeso.2009.05.Finance/TransferObject/Gateway/"
    xmlns:tender       = "urn:Cheeso.2009.05.Finance/TransferObject/Tender/"  >
 ...
</s:schema>

Can anyone suggest some elisp that can line up the = ?

+2  A: 

Try something like the following:

(defun prettyprint-xml ()
  (interactive)
  (goto-char (point-min))
  (while (search-forward "=" (point-max) t)
    (search-forward "\"")
    (search-forward "\"")
    (forward-char)
    (newline-and-indent))
  (align-regexp (point-min) (point-max) "\#"))

It may not do exactly what you want ( I justcoded it up), but it looks like it should work for the case you showed.

Nathaniel Flath