Microsoft KB Archive/325692

= FIX: Document Function in XSLT Style Sheet Does Not Raise a .NET Exception =

Article ID: 325692

Article Last Modified on 9/22/2003

-

APPLIES TO


 * Microsoft .NET Framework 1.0

-



This article was previously published under Q325692



SYMPTOMS
With the document XSLT function, you can retrieve other XML resources from an XSLT style sheet in addition to the initial data that the input stream provides. The .NET Framework XSLT processor does not raise any exception when it cannot resolve a document reference in an XSLT style sheet to an external XML resource. The XSLT transformation completes successfully and the result that is generated is not as you expect.



RESOLUTION
Check the Uniform Resource Identifier (URI) parameter that is used to reference the external XML resource in the document function to make sure that it points to a valid location (for example, the correct file name and path).

The default resolution process can also fail if the referenced XML resource is located in a secured location (for example, a Web site or a network share that requires specific user credentials to access its resources). To address this, you must create an instance of, and then use a custom XmlUrlResolver object to supply the credentials that you must have to access the XML resource from the secured location.



STATUS
This bug was corrected in Microsoft .NET Framework Class Libraries 1.1. This is a design limitation in the implementation of the document XSLT function of the .NET Framework XSLT Processor. Microsoft is currently researching this problem.



Steps to Reproduce the Behavior
To reproduce this behavior by using a sample Visual Basic. NET Console Application project, follow these steps:  Start Visual Studio. NET, and create a new Visual Basic .NET Console Application project.  In a text editor such as Notepad, paste the following XML to create an XML Document and name it Books.xml. Save Books.xml in the Bin sub-folder of the project folder:    .NET Framework Programming 1 </Book> <Book> XML Programming</Title> 2</Author> </Book> </Books> </li>  In a text editor such as Notepad, paste the following XML to create an XML Document, and name it Authors.xml. Save Authors.xml in the Bin sub-folder of the project folder: <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?>   <Id>1</Id> <Name>John</Name> </Author>  <Id>2</Id> <Name>Peter</Name> </Author> </Authors> </li>  In a text editor such as Notepad, paste the following XSLT code to create an XSLT Document, and name it BookListing.xsl. Save BookListing.xsl in the Bin sub-folder of the project folder: <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> <xsl:template match=&quot;Books&quot;> <HTML> <BODY>

<H3>Book Listing</H3> </BODY> </HTML> </xsl:template> </xsl:stylesheet> </li> This style sheet is used to transform and to display the data in Books.xml in an HTML table. Notice that the document function is used to reference the XML document that contains Author data. This reference is used to display the names of the Authors whose Ids are referenced in the Book elements in Books.xml. Also notice that there is a typo in the XML file name in the URI parameter of the document function. This is an intentional lapse to demonstrate the behavior that is described in the &quot;Symptoms&quot; section of this article.</li>  Use the following code to replace the default code that is generated in Module.vb (in the Visual Basic. NET project): Imports System Imports System.Xml.XPath Imports System.Xml.Xsl

Module Module1

Sub Main Dim xml As New XPathDocument(&quot;Books.xml&quot;)

Dim xslt As New XslTransform xslt.Load(&quot;bookListing.xsl&quot;)

Dim fstream As New System.IO.FileStream(&quot;bookListing.html&quot;, IO.FileMode.Create) xslt.Transform(xml, Nothing, fstream) fstream.Close

Console.WriteLine(&quot;Done&quot;) End Sub

End Module

</li> Save the changes, and then run the project. The code runs successfully and returns control to the designer window. The output of the XSLT transformation is generated to an HTML file that is named BookListing.html in the Bin sub-folder of the project. Notice that no exception is raised to indicate that the XSLT processor cannot resolve the reference to the non-existent XML document (Author.xml) that is referenced by the document function in the style sheet</li> Click the Show All Files and Refresh buttons in the Visual Studio. NET Solution Explorer window to display the sub-folders of the project.</li> Expand the Bin sub-folder. You see the BookListing.html file that is generated by the XSLT transformation. Double-click BookListing.html to open it in Visual Studio .Net. Notice that the HTML table that is generated does not display the names of the authors for the books that are listed. This occurs because the default XmlResolver that is used by the XSLT processor cannot resolve the reference to the non-existent Author.xml document.</li>  Modify the URI parameter of the document function in the BookListing.xsl XSLT style sheet to specify the correct file name (Authors.xml) of the XML document that contains the Author data. The modified XSLT statement is as follows: <xsl:value-of select=&quot;document('Authors.xml')/Authors/Author[Id=$vId]/Name&quot;/> </li> Save the change, and then run the project. Open the generated BookListing.html file to examine the output. You see that the HTML table lists the names of the Authors for each of the listed books, as you expect.</li></ol>

<div class="references_section">