title: How to Sign Just a part of XML File - Java XMLSignature
body: <p>I'm newbie and I'm in a hurry. I'm just trying to digital sign a part of an XML.</p>
  
  <p>The XML to sign is like this:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;ns0:CEE_Adenda xmlns:ns0="http://adenda.es" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://adenda.es Test_v1.xsd"&gt;
  &lt;ns0:CEE version="1.0" xmlns:ns0="http://adenda.es"&gt;//XML to sing
  ............ 
  &lt;/ns0:CEE&gt;
  &lt;ns0:Adenda&gt; //Part to exclude
  ......
  &lt;/ns0:Adenda&gt;
  &lt;/ns0:CEE_Adenda&gt;
  </code></pre>
  
  <p>, in order to get an structure like this:</p>
  
  <pre><code>        &lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;ns0:CEE_Adenda xmlns:ns0="http://adenda.es" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://adenda.es Test_v1.xsd"&gt;
      &lt;ns0:CEE version="1.0" xmlns:ns0="http://adenda.es"&gt;//XML to sing
      ............ 
  &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=""&gt;
          &lt;Transforms&gt;
              &lt;Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/&gt;
          &lt;/Transforms&gt;
          &lt;DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/&gt;
          &lt;DigestValue&gt;vMg+tzKiwC8epApusLGo23at0ss=&lt;/DigestValue&gt;
      &lt;/Reference&gt;
  &lt;/SignedInfo&gt;
  &lt;SignatureValue&gt;dVqqHp437r7jAeEOB6mxgSOKnpT6EITRscd0mzA/zDep3Wkg1CM/m0ojDHnlkC7l
      &lt;/ns0:CEE&gt;
      &lt;ns0:Adenda&gt; //Part to exclude
      ......
      &lt;/ns0:Adenda&gt;
      &lt;/ns0:CEE_Adenda&gt;
  </code></pre>
  
  <p>This is the code I'm using to try to sign it, but I need to sign only the  part of XML and put the result inside this tag. </p>
  
  <p>This is the code I'm using:</p>
  
  <pre><code>//Create a DOM XMLSignatureFactory that will be used to
          // generate the enveloped signature.
          XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
          // Create a Reference to the enveloped document (in this case,
          // you are signing the whole document, so a URI of "" signifies
          // that, and also specify the SHA1 digest algorithm and
          // the ENVELOPED Transform.
          try {
              List&lt;XPathType&gt; xpaths = new ArrayList&lt;XPathType&gt;();
              xpaths.add(new XPathType("//ns0:CFE", XPathType.Filter.INTERSECT));
              Reference ref = fac.newReference("", 
                                               fac.newDigestMethod(DigestMethod.SHA1, null),
                                               Collections.singletonList(fac.newTransform(Transform.ENVELOPED, 
                                                                                          (TransformParameterSpec) null)),null,null);
              //ori
              //Reference ref = fac.newReference("", fac.newDigestMethod(DigestMethod.SHA1, null));
              // Create the SignedInfo
              SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, 
                                                                              (C14NMethodParameterSpec)null), 
                                                fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), 
                                                Collections.singletonList(ref));
  
              // Load the KeyStore and get the signing key and certificate.
              String p12Password = clave;
              KeyStore ks = KeyStore.getInstance("PKCS12");
              ks.load(new FileInputStream(keyStoreName), p12Password.toCharArray());
              KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry)ks.getEntry(alias, 
                                                                                        new KeyStore.PasswordProtection(p12Password.toCharArray()));
              X509Certificate cert = (X509Certificate)keyEntry.getCertificate();
  
              // Create the KeyInfo containing the X509Data.
              KeyInfoFactory kif = fac.getKeyInfoFactory();
              List&lt;Serializable&gt; x509Content = new ArrayList&lt;Serializable&gt;();
              x509Content.add(cert.getSubjectX500Principal().getName());
              x509Content.add(cert);
              X509Data xd = kif.newX509Data(x509Content);
              javax.xml.crypto.dsig.keyinfo.KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd));
  
              // Instantiate the document to be signed.
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              dbf.setNamespaceAware(true);
              Document doc = dbf.newDocumentBuilder().parse(new FileInputStream(xmlEntrada));
              // Create a DOMSignContext and specify the RSA PrivateKey and
              // location of the resulting XMLSignature's parent element.
              DOMSignContext dsc = new DOMSignContext(keyEntry.getPrivateKey(), doc.getDocumentElement());
              // Create the XMLSignature, but don't sign it yet.
              XMLSignature signature = fac.newXMLSignature(si, ki);
              // Marshal, generate, and sign the enveloped signature.
              signature.sign(dsc);
  
              // Output the resulting document.
              OutputStream os = new FileOutputStream(xmlSalida);
              TransformerFactory tf = TransformerFactory.newInstance();
              Transformer trans = tf.newTransformer();
              trans.transform(new DOMSource(doc), new StreamResult(os));
  </code></pre>
  
  <p>Any help will be very appreciated.</p>
  
  <p>Regards </p>