title: Cannot resolve element with ID while signing a part of SOAP REQUEST X509
body: <p>I had the following error while trying to sign a part of SOAP Request : </p>
  
  <p><i></p>
  
  <pre><code>org.apache.xml.security.utils.resolver.ResourceResolverException: Cannot resolve element with ID _53ea293168db637b15e2d4d7894
  at org.apache.xml.security.utils.resolver.implementations.ResolverFragment.engineResolve(ResolverFragment.java:86)
  at org.apache.xml.security.utils.resolver.ResourceResolver.resolve(ResourceResolver.java:279)
  at org.apache.xml.security.signature.Reference.getContentsBeforeTransformation(Reference.java:417)
  at org.apache.xml.security.signature.Reference.dereferenceURIandPerformTransforms(Reference.java:597)
  at org.apache.xml.security.signature.Reference.calculateDigest(Reference.java:689)
  at org.apache.xml.security.signature.Reference.generateDigestValue(Reference.java:396)
  at org.apache.xml.security.signature.Manifest.generateDigestValues(Manifest.java:206)
  at org.apache.xml.security.signature.XMLSignature.sign(XMLSignature.java:595)
  </code></pre>
  
  <p></i></p>
  
  <p>It comes from the resolution of the URI declared on the reference tag.</p>
  
  <p>Here is the java code i'm using for signing via X509 : </p>
  
  <pre><code>    KeyStore.PrivateKeyEntry pke = ISKeyStoreManager.getInstance().getPrivateKeyEntry(keyStoreAlias, keyAlias);
  
  
      AlgorithmStrings algStrings = AlgorithmStrings.getAlgDSStrings( pke.getPrivateKey(), signatureAlgorithmString, digestAlgorithmString);
  
      String resultantXPath = StringUtils.join(xpaths, '|');
  
      Transforms transforms = new Transforms(originalDocument);
  
      NodeList targetDocumentList = obtainNodesForXPath(originalDocument, resultantXPath, nc);
  
      if(targetDocumentList != null &amp;&amp; targetDocumentList.getLength() &gt; 0)
      {
  
          if(targetDocumentList.item(0).hasAttributes()){
              Node attrId = targetDocumentList.item(0).getAttributes().getNamedItem("Id");
              if(attrId != null &amp;&amp; !attrId.getNodeValue().equals("")){
                  uri = new StringBuilder().append('#').append(attrId.getNodeValue()).toString();
              }
              else{
  
                  ((Element) targetDocumentList.item(0)).setAttribute("xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
                  ((Element) targetDocumentList.item(0)).setAttribute("wsu:Id", idForXmlObject);
              }
          }
          else{
  
              ((Element) targetDocumentList.item(0)).setAttribute("xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
              ((Element) targetDocumentList.item(0)).setAttribute("wsu:Id", idForXmlObject);
          }
      }else{
          log.debug("Target not found in the original document with xpath: " + resultantXPath);
      }
  
      transforms.addTransform("http://www.w3.org/2000/09/xmldsig#enveloped-signature");
  
      if (resultantXPath != null) {
          log.debug("Instantiation XPATHContainer");
          XPathContainer xpathC = new XPathContainer(originalDocument);
          xpathC.setXPath(resultantXPath);
  
          if ((ncMap != null) &amp;&amp; (!ncMap.isEmpty())) {
              for (Map.Entry&lt;String,String&gt; e : ncMap.entrySet()) {
                  log.debug("Adding namespace to XPATH Container: " + e.getKey() + " -&gt; " + e.getValue());
                  xpathC.setXPathNamespaceContext(e.getKey(), e.getValue());
              }
          }
          transforms.addTransform("http://www.w3.org/TR/1999/REC-xpath-19991116", xpathC.getElement());
      }
      log.debug("Instantiation Signature");
      XMLSignature sig = new XMLSignature(originalDocument, null, algStrings.signatureAlgorithm, canonicalizationAlg);
  
      sig.setFollowNestedManifests(true);
      log.debug("Ajout des assertions de transformation");
      sig.addDocument("", transforms, algStrings.digestMethod);
  
      if (idAttrForSignature != null) {
          sig.setId(idAttrForSignature);  
      }
  
      log.debug("DOMToString: " + serializeDOMToString(originalDocument));
  
      // signature node insertion
      NodeList nodeList = obtainNodesForXPath(originalDocument, insertSignatureAtXPath, nc);
  
      if(nodeList != null &amp;&amp; nodeList.getLength() &gt; 0){
          Node nodeSignature = nodeList.item(0);
          Node childNode = nodeSignature.getFirstChild();
          if (childNode != null) {
              if (addSignatureAsLastElement)
                  nodeSignature.appendChild(sig.getElement());
              else
                  nodeSignature.insertBefore(sig.getElement(), childNode);
          }
          else nodeSignature.appendChild(sig.getElement());
      }
      else{
          throw new ServiceException("INVALID_SIGNATURE_NODE_SELECTOR_XPATH");
      }
  
  
      // Public key insertion     
      //X509Data x509Data = getX509Data(includeCertChain, certificateData, originalDocument, pke);
  
      //KeyInfoReference kir = new KeyInfoReference(x509Data.getDocument());
      SecurityTokenReference str = new SecurityTokenReference(sig.getKeyInfo().getDocument());
      str.setKeyIdentifier(ISKeyStoreAccessorUtil.getIaikCertificate(pke.getCertificate()));
      sig.getKeyInfo().getElement().appendChild(str.getElement());
  
      log.debug("DOMToString: " + serializeDOMToString(originalDocument));
  
      //sig.getSignedInfo().addResourceResolver(new ResolverXPointer());
  
      ((Element)(sig.getSignedInfo().getElement().getElementsByTagNameNS("http://www.w3.org/2000/09/xmldsig#", "Reference").item(0))).setAttribute("URI", uri);   
  
      log.debug("DOMToString: " + serializeDOMToString(originalDocument));
      //sig.addDocument(uri, trans);
      // Signature generation 
      sig.sign(pke.getPrivateKey());
  </code></pre>
  
  <p>Do you have any proposition of workaround ? or another way to set URI attribute  ? </p>
  
  <p>Thank you for helping  !</p>