home *** CD-ROM | disk | FTP | other *** search
- !!Script
- // Copyright ⌐ 2001 - Modelworks Software
-
- /**
- @Tool: transform~an XML document using a stylesheet.
- Note that this script should be considered an example
- and may need modifications to do what you want. See
- Microsoft's documentation for infomation about the
- MSXML2 object and related classes.
- @Paragraph: To use this script open a xml file and
- then run this script. .
-
- @EndTool:
- @Summary: transform~an XML document using a stylesheet
- */
-
-
- function DoCommand()
- {
- var editor = getActiveEditor();
- if (editor)
- {
- var findData = editor.findFirst("xml-stylesheet", 0, 0, true, true, true);
- if (findData && findData.found)
- {
- findData = editor.findFirst("href=\"([^\"]+)", findData.range.startLineIndex, findData.range.startCharIndex, true, true, true);
- if (findData && findData.found)
- {
- var xslFile = editor.copy(findData.getSubRange(1));
-
- if (xslFile.length > 0)
- {
- var xmldoc = createObject("MSXML2.FreeThreadedDOMDocument");
- var xsldoc = createObject("MSXML2.FreeThreadedDOMDocument");
-
- if(xmldoc && xsldoc)
- {
- var lineCount = editor.getLineCount();
- var data = editor.copy(0,0,lineCount-1,32000); // copy all the data
-
- if (xmldoc.loadXML(data))
- {
- xsldoc.async = false;
- if (xsldoc.load(xslFile))
- {
-
- var xsltemp = createObject("MSXML2.XSLTemplate");
- xsltemp.stylesheet = xsldoc.documentElement;
- var xslproc = xsltemp.createProcessor();
-
- xslproc.input = xmldoc.documentElement;
- xslproc.transform();
-
- var display = newEditor("*." + GetFileType(editor), true);
- display.append(xslproc.output);
- }
- else
- {
- alert("load failed - you may one or more errors in your xsl file");
- }
- }
- else
- {
- alert("loadXML failed - you may one or more errors in your xml file");
- }
- }
- else
- {
- alert("Cannot create MSXML2.FreeThreadedDOMDocument");
- }
- }
- }
- }
- else
- {
- alert("No xml-stylesheet element found in document");
- }
- }
- }
-
- function GetFileType(editor)
- {
- var findData = editor.findFirst("<xslt:output", 0, 0, true, true, false);
- if (findData && findData.found)
- {
- findData = editor.findFirst("method=\"text/([^\"]+)", findData.range.startLineIndex, findData.range.startCharIndex, true, true, true);
- if (findData && findData.found)
- {
- return editor.copy(findData.getSubRange(1));
- }
- findData = editor.findFirst("method=\"([^\"]+)", findData.range.startLineIndex, findData.range.startCharIndex, true, true, true);
- if (findData && findData.found)
- {
- return editor.copy(findData.getSubRange(1));
- }
- }
-
- return "xml";
- }
-
- !!/Script
-
-