title: How to produce XML signature with no whitespaces and line-breaks in Java?
body: <p>I work with the brazilian "<a href="http://www.oobj.com.br/" rel="nofollow" title="Nota Fiscal Eletronica">Nota Fiscal Eletronica</a>" project, in which they define a standart way to sign XML documents.</p>
  
  <p>Recently, they started to require that there are absolutely no whitespaces between tags, including signature tags (*).</p>
  
  <p>We happen to use apache's <a href="http://santuario.apache.org/Java/api/org/apache/xml/security/signature/XMLSignature.html" rel="nofollow">XMLSignature</a> and I can't seem to produce an unindented signature.</p>
  
  <p>If I remove the whitespaces after signing, the signature gets broken.</p>
  
  <p>I cannot change the canonicalizer / transformers set either, since they're predefined.</p>
  
  <p>I couldn't find an option or parameter in the XMLSignature API to control indentation or whitespaces.</p>
  
  <p>Below is the code:</p>
  
  <pre><code>    // the element where to insert the signature
      Element element = ...;
      X509Certificate cert = ...;
      PrivateKey privateKey = ...;
  
      XMLSignature signer =
              new XMLSignature(doc, "http://xml-security",
              XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1);
  
      element.appendChild(signer.getElement());
  
      Transforms transforms = new Transforms(doc);
  
      // Define as regras de transformação e canonicalização do documento
      // XML, necessário para fazer a verificação do parsing e da
      // assinatura pelos destinatários
      transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE); //, xpath.getElementPlusReturns());
  
      transforms.addTransform(Transforms.TRANSFORM_C14N_OMIT_COMMENTS); //,xpath.getElementPlusReturns());
  
      String id = "";
  
      id = ((Element) element.getElementsByTagName("infNFe").item(0)).getAttributeNode("Id").getNodeValue();
  
      signer.addDocument("#" + id, transforms, 
                         Constants.ALGO_ID_DIGEST_SHA1);
      signer.addKeyInfo(cert);
      signer.sign(privateKey);
  </code></pre>
  
  <p>And below is the resulting signature (snippet):</p>
  
  <pre><code>&lt;Signature xmlns="http://www.w3.org/2000/09/xmldsig#"&gt;
  &lt;SignedInfo&gt;
  &lt;CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/&gt;
  &lt;SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/&gt;
  &lt;Reference URI="#NFe43110189716583000165550010000076011492273645"&gt;
  &lt;Transforms&gt;
  &lt;Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/&gt;
  &lt;Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/&gt;
  &lt;/Transforms&gt;
  &lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
  &lt;DigestValue&gt;fas0ra5uRskQgRHSrIYhEjFEjKQ=&lt;/DigestValue&gt;
  &lt;/Reference&gt;
  &lt;/SignedInfo&gt;
  &lt;SignatureValue&gt;
  2RGltUZy0HfNoiKtVanAeN+JUPyglWDuQNnMudSgA7kESoHBZ/q/GMbc+xMSN1eV8u7+2PxSKl1T
  Zl592FWmCSAkL8pwMujDxJ4iTLU20Hf0dNF7oGcyB+g9GgbipW2udq0kwJLz6HzXUD/Evf/0y+3T
  NtsXeIaA6A29ttD/UEs=
  &lt;/SignatureValue&gt;
  &lt;KeyInfo&gt;
  &lt;X509Data&gt;
  &lt;X509Certificate&gt;
  MIIFqTCCBJGgAwIBAgIEQeNSuzANBgkqhkiG9w0BAQUFADBaMQswCQYDVQQGEwJicjETMBEGA1UE
  ChMKSUNQLUJyYXNpbDEgMB4GA1UECxMXQ2FpeGEgRWNvbm9taWNhIEZlZGVyYWwxFDASBgNVBAMT
  C0FDIENBSVhBIFBKMB4XDTEwMDYwODE5MjQwNVoXDTExMDYwODE5NTQwNVowgYQxCzAJBgNVBAYT
  AmJyMRMwEQYDVQQKEwpJQ1AtQnJhc2lsMSAwHgYDVQQLExdDYWl4YSBFY29ub21pY2EgRmVkZXJh
  bDEUMBIGA1UECxMLQUMgQ0FJWEEgUEoxKDAmBgNVBAMTH0EgQlVITEVSIFNBIENVUlRVTUU6NDA5
  NDI0OTAwMTAwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAOFxgvG35RQWgXec4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DVR0jBBgwFoAUjkAvCv4T1ao5oHZ0htO8fcfx5c8w
  CQYDVR0TBAIwADAZBgkqhkiG9n0HQQAEDDAKGwRWNy4xAwIDqDANBgkqhkiG9w0BAQUFAAOCAQEA
  nZHUvdnZsiCIDjKm1zHehbtuDtDJha4O4FZ03J74Y+AxyAFs/4JED+xUvZ5jFuEsdqgA0V/dxUFy
  Uz/ca10Ievd578GQdGwYl1GFhRtO/SlxeaOEf7eDdGOWXO3VmUA3NmNo0X8RRTIoifnhpDXu7RbN
  5sijyH/uXyRFWX9XH2N0U/r3oJtNKXsvoUlbDrkalgkuLzLKsaEj0TkwisXO3cmMoWGuBpAZC+46
  e4x/2vTqOvYkzZO+O9NLi0YWSYY7OJKiKBjMC6MzdlPM9VTkIwO9WvWEMdbU0/jhO2cMcVMzNZc1
  r6ZmdTDrwqV3elSTkQtJ0RIZNgMJUn+Y8c7Aog==
  &lt;/X509Certificate&gt;
  &lt;/X509Data&gt;
  &lt;/KeyInfo&gt;
  &lt;/Signature&gt;
  </code></pre>
  
  <p>Notice the (unwanted) line-breaks.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>Thanks a lot in advance.</p>
  
  <p>(*) Clarification: the new rule forbids whitespaces (or any other text) between element-only tags. As an example, this would be <em>allowed</em>:</p>
  
  <pre><code>&lt;a&gt;&lt;b&gt;
    text
    inside
    tag
  &lt;/b&gt;&lt;/a&gt;
  </code></pre>
  
  <p>while this would be <em>forbidden</em>:</p>
  
  <pre><code>&lt;a&gt;
  &lt;b&gt;text&lt;/b&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p>because on the latter case, the whitespaces (line-breaks) are between two tags, or, in other words, placed inside an element-only tag.</p>