General XMetaL Discussion
XMetaL Community Forum › General XMetaL Discussion › Code Request: Process content with XSLT
-
Crispness April 3, 2009 at 9:34 am
Code Request: Process content with XSLT
April 3, 2009 at 9:34 amParticipants 2Replies 3Last Activity 13 years, 12 months agoHi
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 Town ME1 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.
ghkrause April 3, 2009 at 12:23 pm
Reply to: Code Request: Process content with XSLT
April 3, 2009 at 12:23 pmWhen 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]Crispness April 3, 2009 at 12:29 pm
Reply to: Code Request: Process content with XSLT
April 3, 2009 at 12:29 pmThanks 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.
ghkrause April 3, 2009 at 1:09 pm
Reply to: Code Request: Process content with XSLT
April 3, 2009 at 1:09 pmIf 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] -
AuthorPosts
- You must be logged in to reply to this topic.