Jump to content

document() function


rthiel@telkomsa.net

Recommended Posts

Greetings,I am using the document() function to access nodes in an external xml document from my stylesheet. this works fine so long as i know the exact external document URI.However, the external document is created by an external application and i can only partly resolve its name. The document name consists of a random prefix, a constant middle part and a random suffix.As far as i know, the document() function doesn't accept a name pattern.Can anyone help me towards finding a way of accessing the external document content even though I only know part of the document name. At the moment i have to rename the document externally, removing the variable prefix and suffix.RegardsRainer

Link to comment
Share on other sites

Can the other application tell you the name of the file (on the very least)? If it can, you should pass it along as a parameter, and then use up the parameter as part of the path, like so:

<xsl:param name="path" /><!-- We're assuming the application can fill this out with something like "DASRcontstantPartRQWE.xml"-->...<xsl:value-of select="document(concat('http://domain/knownDirectory/', $path))/" />

If not, the best thing I can suggest is to screen scrap the directory listing page (assuming such exists), and try to "guess" the right file.

Link to comment
Share on other sites

Hi, thanks for your reply.

Can the other application tell you the name of the file (on the very least)?
No, they are just plonked into a directory location from where i can retrieve them. The delivering application is just a very black box.It looks like i will have to put a small app in front of my transform to standardize the file names, or as you suggest, pass the full names into my transform as params.I was hoping to do the whole thing in XSLT.CheersRainer
Link to comment
Share on other sites

The document value is a string, no reason why you can't concat onethis worked for me:<xsl:variable name="doc1">SupportItems/Common277Codes.xml</xsl:variable> <!-- create your document name here --><xsl:variable name="STCCodes1" select="document($doc1)//CodeList/Codes"/> <!-- apply it here -->Note the ABSCENSE of the single quotes in the document function.

Link to comment
Share on other sites

The document value is a string, no reason why you can't concat onethis worked for me:<xsl:variable name="doc1">SupportItems/Common277Codes.xml</xsl:variable> <!-- create your document name here -->
aah yes, but you know the full document name. I only know part of it. If you document were prefixed with some random prefixStringValue and suffixed with some random suffixStringValue, then what?
Link to comment
Share on other sites

Then I misunderstood the question. I thought you knew all the parts and were just trying to build a string from them. After much experimentation, here hows I did it1. add the user namespace to your stylsheet

<xsl:stylesheet	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"	xmlns:user="http://yournamespace.com">

2. Create an internal script function that will pattern match for the file name

<msxsl:script language="JScript" implements-prefix="user">	var fso	= new ActiveXObject("Scripting.FileSystemObject");	function GetFileNameFromPattern(pattern)	{		var subfolders,files,folder,file;		var sReturn = ""		var rePattern = new RegExp(pattern,"i")		folder = fso.GetFolder("C://documents//dev//XSLT//SupportItems");		files = new Enumerator(folder.files);		for (;!files.atEnd();files.moveNext())		{			file = files.item();			if (file.Name.match(rePattern) != null)			{				sReturn  = file.Name				return sReturn			}		}		return "file not found"	}</msxsl:script>

3. Call the function and put the value returned into a variable

	<xsl:variable name="doc1">		<xsl:value-of select="concat('SupportItems/',user:GetFileNameFromPattern(string('277Codes')))" />	</xsl:variable>

4. Deploy

	<xsl:variable name="STCCodes1" select="document($doc1)//CodeList/Codes[@FileType='277'][@Version='3051']"/>

Here is the entire stylesheet, you can anaylze and see how the complete path is assumed. I also did not deal with file not found

<xsl:stylesheet	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"	xmlns:user="http://yournamespace.com"	xmlns:msxsl="urn:schemas-microsoft-com:xslt"	version="1.0">	<xsl:output method="html"/>	<xsl:variable name="doc1">		<xsl:value-of select="concat('SupportItems/',user:GetFileNameFromPattern(string('277Codes')))" />	</xsl:variable>	<xsl:variable name="STCCodes1" select="document($doc1)//CodeList/Codes[@FileType='277'][@Version='3051']"/>	<msxsl:script language="JScript" implements-prefix="user">		var fso	= new ActiveXObject("Scripting.FileSystemObject");		function GetFileNameFromPattern(pattern)		{			var subfolders,files,folder,file;			var sReturn = ""			var rePattern = new RegExp(pattern,"i")			folder = fso.GetFolder("C://documents//dev//XSLT//SupportItems");			files = new Enumerator(folder.files);			for (;!files.atEnd();files.moveNext())			{				file = files.item();				if (file.Name.match(rePattern) != null)				{					sReturn  = file.Name					return sReturn				}			}			return "file not found"		}	</msxsl:script>	<xsl:template match="/">		<!-- <xsl:value-of select="$doc1"/> --> <!-- uncomment to see the file name returned -->		<xsl:value-of select="$STCCodes1//Code[Value='A21']/Desc" />	</xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

Then I misunderstood the question. I thought you knew all the parts and were just trying to build a string from them. After much experimentation, here hows I did it
Oh wow! Thank you so much. It works perfectly, and you have opened my eyes to further possibilities using XSLT that i have not yet explored.Your efforts much appreciated.Rainer
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...