General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion Code Request: Process content with XSLT

  • Crispness

    Code Request: Process content with XSLT

    Participants 2
    Replies 3
    Last Activity 13 years, 2 months ago

    Hi
    Has anyone got a generic code fragment they would be willing to share that would do the following:

    • Select a well-formed XML fragment from the current doc
    • Apply a known XSLT transformation file
    • Return the result of the transformation to the current doc in place of the original fragment

    My context for this is a situation where we want to extract a series of hard-coded contact details from documents, store them in a database, and return in their place a reference to their location in the db.

    Example:
    Take this fragment from the doc
    [code]

    Fred Bloggs
    Acme Widgets plc
    13 High St
    Old TownME1 9RU

    [/code]

    Apply an XSLT file 'process-address.xslt'

    and return
    [code]

    [/code]

    I already have the xslt file which will process the data, and know it works.
    But I am currently running it outside Xmetal.
    It would be nice to be able to run the process from a button or an event.

    Any suggestions much appreciated.

    Reply

    ghkrause

    Reply to: Code Request: Process content with XSLT

    When running JScript in Internet Explorer to render a preview I use the function below. It will transform XML by MSXML engine. Hope this helps and gets you going. I never tried it inside an XMetaL macro, though. You will have to replace the initialization of xml_source with your valid XML trunk and convert the result to XML via the load method …
    [code]function xml_convert( strXmlSourceUrl, strXslPath ) {
      var xml_source = new ActiveXObject(“Msxml2.DOMDocument”);
      xml_source.async = false;
      xml_source.validateOnParse = false;
      xml_source.resolveExternals = blnResolveExternals;
      // use this to load a file (a path) to the MSXML
      xml_source.load( strXmlSourceUrl );
      // load XSLT stylesheet document
      var stylesheet = new ActiveXObject(“Msxml2.DOMDocument”);
      stylesheet.async = false;
      stylesheet.validateOnParse=false;
      stylesheet.load( strXslPath );
      // transform the source using the XSLT stylesheet
      var converted_html = “”;
      try {
        converted_html = xml_source.transformNode( stylesheet );
      }
      catch( err ) {
        converted_html = “Error transforming with ” + strXslPath + “. ” + err.decsription;
        for (var i in err) {
          converted_html=converted_html+” “+ err+”. “;
        }
        alert(converted_html);
      }
      return converted_html;
    }[/code]

    Reply

    Crispness

    Reply to: Code Request: Process content with XSLT

    Thanks Gunnar but not really what I was looking for. I already have some code to do something very similar
    [code]function doXformXML(strXMLIn,xslPath)
    {
    var strXMLOut = null;
    if(!isWellformedDoc()){
    Application.Alert(“Current document is an invalid document!”);
    return;
    }

    var fso = null;
    var xmldoc = null;
    var xsldoc = null;
    try {

    var fso = createFileSystemObject();
    if (fso == null)
    throw null;

    //xsl-hmtl file doesn't exist, do nothing.
    // if (!fso.FileExists(xslPath))
    // throw null;

     
    // Load the XML document into MSXML
    var xmldoc = getDOMDocument();
    if (xmldoc == null)
    throw null;

    xmldoc.async = false;
    xmldoc.validateOnParse = false;
    xmldoc.loadXML(strXMLIn);
    if (xmldoc.parseError.errorCode != 0)
    throw “XML: ” + formatParseError(xmldoc.parseError);

    // Load the XSL stylesheet
    xsldoc = getDOMDocument();
    if (xsldoc == null)
    throw null;
    xsldoc.async = false;
    xsldoc.load(xslPath);
    if (xsldoc.parseError.errorCode != 0)
    throw “XSL: ” +  formatParseError(xsldoc.parseError);

    var htmlout = “”;
    try {
    htmlout = xmldoc.transformNode(xsldoc);
    } catch (exception) {
    throw “TRANSFORMNODE: ” + formatRuntimeError(“Save As HTML Error:”, exception);
    }
       
    } catch (e) {
    if (e)
    Application.Alert(e);
    return false;
    } finally {
    fso = null;
    xmldoc = null;
    xsldoc = null;
    }
    return htmlout;
    }
    [/code]

    What it doesn't do is to take a fragment and replace it with the results of the transform.

    Thats the part I need help with.

    Reply

    ghkrause

    Reply to: Code Request: Process content with XSLT

    If you just return the id value from the XSLT instead of

    the task is to remove the children of

    and add a new attribute with the calculated value.
    I wrote the lines enclosed but I did not test. This is for reference only.
    Let me know if that was of help 🙂

    [code]var objNode = ActiveDocument.documentElement;
    // get list of all elements to change
    var nodes2change= objNode.getNodesByXPath(“address”);
    var node2change;
    var strAddressId;
    // Work through the list if length > 0
    for (var index = 0; index < nodes2change.length; index++) {
      node2change = nodes2change.item(index);
      strAddressId = your_great_xslt_function( node2change );
      // remove old stuff
      while( node2change.firstChild ) {
        node2change.firstChild.removeChild;
      }
      // add new attribute
      node2change.setAttribute( “addressid”, strAddressId );
    }[/code]

    Reply

  • You must be logged in to reply to this topic.

Lost Your Password?

Products
Downloads
Support