Re: [xsl] Using XSLT to Append a XML Document to a Master XML Document

Subject: Re: [xsl] Using XSLT to Append a XML Document to a Master XML Document
From: Francis Norton <francis@xxxxxxxxxxx>
Date: Tue, 07 Aug 2001 22:07:04 +0100
Hi Dante,

dante wrote:
> 
> Is it possible (using a xslt stylesheet) to append the content of one xml
> doc to the end of a master xml doc?

Yes, provided the data is held in a file or other URL-addressable
resource - see http://www.w3.org/TR/xslt.html#function-document. You can
may also be able to pass XML documents in as global parameters - see the
attached jscript for a demo of this using msxml4. (Feature should work
using msxml3 too.) 

This makes a more attractive prospect if you're working with large
in-memory documents in a heavily stressed server environment and you
don't want the load of serialising, saving, re-loading and re-parsing
the documents in question - it also allows you to import the results of
HTTP POST or SOAP based web services efficiently.

Francis.

---
C:\xml>type t.xml
<?xml version="1.0" encoding="UTF-8"?>
<a b="c"/>

C:\xml>type t.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
        <xsl:output indent="yes"/>
        <xsl:param name="importXML" />
        <xsl:template match="/">
                <root>
                        <xsl:for-each select="$importXML//@*">
                                <xsl:value-of select="concat('@',
name(), ': ', .)" />
                        </xsl:for-each>
                </root>
        </xsl:template>
</xsl:stylesheet>

C:\xml>importXML t.xml t.xslt t.xml
<?xml version="1.0"?>
<root>@b: c</root>
---

// This file is:  importXML.js
// first parameter is an XML files to be read in;
// second parameter is the XSLT file
// third parameter is the name of an XML file to be merged in.

// validate parameters
if(WScript.Arguments.length != 3)
{
	WScript.Echo("importXML takes three arguments - XML, XSLT and importXML
- eg:");
	WScript.Echo('importXML books.xml books.xslt authors.xml');
}
else
{
	importXML(WScript.Arguments(0), WScript.Arguments(1),
WScript.Arguments(2));
}

function importXML(datafile, transform, importXML)
{
	// load the datafile
	var xml = loadXML(datafile);
	
	// load the transform
	var xsl = new ActiveXObject("Msxml2.XSLTemplate.4.0");
	var xslDoc = loadXML(transform);
	xsl.stylesheet = xslDoc;
	var xslProc = xsl.createProcessor();
	
	// load the importXML
	var imp = loadXML(importXML);
	
	// add the import as a parameter
	xslProc.addParameter("importXML", imp);
	
	// do the transform
	xslProc.input = xml;
	xslProc.transform();
	
	WScript.Echo(xslProc.output);
}

function loadXML(source, schemaCache)
{
	var xmlDoc  = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.4.0");
	xmlDoc.async = false;
	if (schemaCache != undefined)
	{
		xmlDoc.schemas = schemaCache;
	}
	xmlDoc.load(source);
	if(xmlDoc.parseError.errorCode != 0)
	{
		loadError(source, xmlDoc);
		WScript.Quit(1);
	}
	return xmlDoc;
}

function loadError(source, xmlDoc)
{
	WScript.Echo("Error loading " + source);
	WScript.Echo("Code: " + xmlDoc.parseError.errorCode);
	WScript.Echo("Source: " + xmlDoc.parseError.srcText);
	WScript.Echo("Line: " + xmlDoc.parseError.line);
	WScript.Echo("Error: " + xmlDoc.parseError.reason);
}

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread