[xsl] Pass XML document (*not* file path!) as parameter to XSLT

Subject: [xsl] Pass XML document (*not* file path!) as parameter to XSLT
From: "david bessire" <david_bessire@xxxxxxxxxxx>
Date: Thu, 17 May 2007 18:54:47 +0000
What's the proper way to pass an XML document as a parameter
to an XSLT, without writing that document to a file and
passing the filepath to the XSLT ?

In Java code, i create a org.dom4j.Document that describes
a user's roles and permissions; the structure looks like:

<pre>
<permsroles>
	<role>Contributor</role>
	<perm>View</perm>
</permsroles>
</pre>

These permissions are used to restrict menu items a user
is allowed to access.  The menu items are described by a
static xml document which looks like:

<pre>
<menuItems>
	<menuItem id="1" parentId="0">
		<ItemURL>URL1</ItemURL>
		<permsroles>
			<role>ADMIN</role>
			<role>Contributor</role>
		</permsroles>
	</menuItem>
	<menuItem id="11" parentId="1">
		<ItemURL>URL2</ItemURL>
	</menuItem>
	<menuItem id="2" parentId="0">
		<ItemURL>URL3</ItemURL>
		<permsroles>
			<perm>View</perm>
		</permsroles>
	</menuItem>
</menuItems>
</pre>

i want to use an XSLT to remove restricted menu items from the
static document by passing the <permsroles> document as a parameter
to the XSLT.  My transform looks like:

<pre>
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0">

	<xsl:param name="permsroles"/>
	<xsl:variable name="permsrolesLookup" select="document($permsroles)"/>

	<xsl:template match="/menuItems">
		<menuItems>
			<xsl:for-each select="menuItem">
				<xsl:choose >

<!-- select menuItem elements with no logical parent -->

<xsl:when test="count(../menuItem[@id = current()/@parentId]) = 0">

<!-- handle top-level menuItem elements and all logical children -->

						<xsl:call-template name="resolve-hierarchical-permissions">
							<xsl:with-param name="id">
								<xsl:value-of select="@id"/>
							</xsl:with-param>
						</xsl:call-template>
					</xsl:when>
				</xsl:choose>
			</xsl:for-each>
		</menuItems>
	</xsl:template>

	<xsl:template name="resolve-hierarchical-permissions" match="menuItem">
	<xsl:param name="id"/>
		<xsl:choose>

			<xsl:when test="not(permsroles)
				or permsroles/role/text() = $permsrolesLookup/permsroles/role/text()
				or permsroles/perm/text() = $permsrolesLookup/permsroles/perm/text()">

				<menuItem>
					<xsl:attribute name="id">
						<xsl:value-of select="@id"/>
					</xsl:attribute>
					<xsl:attribute name="parentId">
						<xsl:value-of select="@parentId"/>
					</xsl:attribute>

					<ItemURL>
						<xsl:value-of select="./ItemURL"/>
					</ItemURL>
				</menuItem>

<!-- recurse for all logical children -->

				<xsl:for-each select="/menuItems/menuItem[@parentId = $id]">
					<xsl:call-template name="resolve-hierarchical-permissions">
						<xsl:with-param name="id">
							<xsl:value-of select="@id"/>
						</xsl:with-param>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>
</pre>

When i do this from the command line using xalan-2_7_0 with <permsroles> defined
in a file, it works as expected. When i try to execute it in Java code using the
dynamically computed <permsroles> document, i get this error:


Can not load requested doc: no protocol: org.dom4j.tree.DefaultElement@3a52a34b [Element: <permsroles attributes: []/>]

i think the transform is trying to load the file from disk. How can i pass the dynamically
computed <permsroles> document to the XSLT as a parameter? i've tried removing 'document(...)'
but that also generates errors.


Do i need to include code to convert the Element into a NodeList?

Any help is appreciated.

Thanks,
david bessire

_________________________________________________________________
More photos, more messages, more storageget 2GB with Windows Live Hotmail. http://imagine-windowslive.com/hotmail/?locale=en-us&ocid=TXT_TAGHM_migration_HM_mini_2G_0507


Current Thread