code: private static String addScriptDirectives(Element scriptElement) throws IOException, DocumentTemplateException {
      String scriptReplacement = "";
  
      List scriptParts = parseScriptParts(scriptElement.getValue());
      for (int index = 0; index < scriptParts.size(); index++) {
        ScriptPart scriptPart = (ScriptPart) scriptParts.get(index);
        if (scriptPart.getLocation() == null) {
          scriptReplacement = scriptPart.getText();
        }
        else {
          Element enclosingElement = findEnclosingElement(scriptElement, scriptPart.getLocation());
          if (scriptPart.isTagAttribute()) {
            String[] nameValue = scriptPart.getText().split("=", 2);
            if (nameValue.length != 2) {
              throw new DocumentTemplateException("script error: # attribute name=value not found");
            }
  
            String attributeNamespace = enclosingElement.getNamespaceURI();
            if (nameValue[0].contains(":")) {
              String prefix = nameValue[0].split(":")[0];
              if (!prefix.equals(enclosingElement.getNamespacePrefix())) {
                attributeNamespace = XPATH_CONTEXT.lookup(prefix);
                if (attributeNamespace == null) {
                  throw new DocumentTemplateException("unsupported attribute namespace: " + prefix);
                }
              }
            }
  
            enclosingElement.addAttribute(new Attribute(nameValue[0], attributeNamespace, nameValue[1]));
          }
          else {
            ParentNode parent = enclosingElement.getParent();
            int parentIndex = parent.indexOf(enclosingElement);
            if (scriptPart.afterEndTag()) {
              parentIndex++;
            }
            parent.insertChild(newNode(scriptPart.getText()), parentIndex);
          }
        }
      }
  
      return scriptReplacement;
    }
docs: @param scriptElement
  
  @return the text that should replace the input field
  
  @throws DocumentTemplateException