Jump to content

Problem setting context using document() function


SteveMurphy

Recommended Posts

I have a stylesheet I'm using to output a text file from an XML file.In addition to processing my XML file, I'm using the document() function to open another xml file and set the context as follows:

<xsl:apply-templates select="document($descrFileName)/Object/ObjectMethods/Method[1]"/>

However, in addition to opening the file, the method seems also to be outputting the value of the Method[1] node and also the value of all its child nodes. Is the the expected behavior?This is not my intended functionality -- I want to set the context to the Method[1] node, then process a template that matches it. How do I accomplish this?Another question. If I want to open the file and set the context to a Method element with a certain value, how would I use the document() method? I tried the following, but it does not seem to work:

<xsl:apply-templates select="document($descrFileName)/Object/ObjectMethods/Method['steve']"/>

Obviously there's something I'm missing. Any help you can provide is much appreciated!Thanks.

Link to comment
Share on other sites

You're on the right track, but you can can place your document node-set into a variable that can be accessed later on. A favorite technique of mine. Have a look. This is a complete example using four components1. methodfill.xml (file used to parse against the xslt, has no content, thats your job)2. method.xml (file called by the document function)3. method.xslt 4. method.htm (does the parsing)I'm not sure that I've explained this properly, but take a look at see if this is what you are trying to domethodfill.xml

<top/>

method.xsl

<xsl:stylesheet		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"		xmlns:user="http://www.post-n-track.com"		xmlns:msxsl="urn:schemas-microsoft-com:xslt"		exclude-result-prefixes="msxsl user"		version="1.0">		<xsl:param name="descrFileName">Method.xml</xsl:param>		<xsl:template match="*">			<xsl:variable name="methods" select="document($descrFileName)//methods"/>			<xsl:apply-templates select="$methods//method[@name = 'steve']"/>		</xsl:template>		<xsl:template match="method">			<xsl:value-of select="data"/>		</xsl:template>	</xsl:stylesheet>

method.xml

<methods>	<method name="steve">		<data>Steven</data>	</method>		<method name="frank" />	<method name="john" /></methods>

method.htm

<script>	var m_xmlSource		= 	new ActiveXObject("Msxml2.DOMDocument.4.0")	var m_xslStyle 		=	new ActiveXObject("Msxml2.DOMDocument.4.0")	m_xmlSource.async 	= 	false	m_xslStyle.async	=	false	m_xmlSource.load("methodfill.xml")	m_xslStyle.load("method.xsl")	m_xslStyle.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'")	document.write(m_xmlSource.transformNode(m_xslStyle))</script>

result

Steven

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...