RE: [xsl] XML fragment as a param ?

Subject: RE: [xsl] XML fragment as a param ?
From: "Nick Fitzsimons" <nick@xxxxxxxxxxxxxx>
Date: Thu, 3 Nov 2005 12:34:24 -0000 (GMT)
>>>>>>"Fraser" == Fraser Goffin <goffinf@xxxxxxxxxxx> writes:

> Fraser> Is it possible to pass in an XML fragment as a param and then
> Fraser> access nodes
> Fraser> in the fragment as well as those in the main source instance
>
> Michael>Yes. The way you pass the value depends of course on your
> processor
> API.
>
> Michael, not sure what you mean, I'm using Xerces/Xalan if that helps ?
>

I'm not certain how Xalan handles this, but the general technique is:

get your XSLT processor, configured with your stylesheet and input XML
document;
get your XML fragment (probably by selecting a node from a DOMDocument, or
if it's text, by having it parsed into a DOMDocument);
add it as a parameter to the processor (MSXML uses "addParameter"; Xalan's
processor class will have a similar method);
access it via the parameter in your stylesheet.

In pseudocode:

processor = getProcessor(yourStylesheet);
processor.input = inputXMLDocument;
processor.output = outputXMLDocument;
processor.addParameter("myXMLFragment", supplementaryXML);
processor.transform();

Within your stylesheet you will have the parameter declared, and access
the content of the fragment via that:

<xsl:stylesheet>
   <xsl:param name="xmlFragment" select="/.." />

   <!-- later... -->

   <xsl:template match="some/node/in/main/document">
      <!-- get a heading from the XMl fragment passed in -->
      <h1><xsl:value-of select="$xmlFragment/supplementary/heading" /></h1>
   </xsl:template>

   <!-- lots of other stuff... -->

</xsl:stylesheet>

How you actually implement the above in real-life code depends on your
processor, so check the Xalan docs for a function with a name similar to
"addParameter", or google for a tutorial.

The procedure for using the "document()" function is different to this,
and "data:" URIs are a different kettle of fish altogether, but as you
originally asked about passing the fragment in a param, this will
hopefully point you in the right direction.

HTH,

Nick.
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/

Current Thread