FIX: Document Function in XSLT Style Sheet Does Not Raise a .NET Exception (325692)



The information in this article applies to:

  • Microsoft XML Classes (included with the .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.

MORE INFORMATION

Steps to Reproduce the Behavior

To reproduce this behavior by using a sample Visual Basic. NET Console Application project, follow these steps:
  1. Start Visual Studio. NET, and create a new Visual Basic .NET Console Application project.
  2. 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:
    <?xml version="1.0" encoding="utf-8" ?> 
    <Books>
    <Book>
    	<Title>.NET Framework Programming</Title>
    	<Author>1</Author>
    </Book>
    <Book>
    	<Title>XML Programming</Title>
    	<Author>2</Author>
    </Book>
    </Books>
    					
  3. 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="1.0" encoding="utf-8" ?> 
    <Authors>
    <Author>
    	<Id>1</Id>
    	<Name>John</Name>
    </Author>
    <Author>
    	<Id>2</Id>
    	<Name>Peter</Name>
    </Author>
    </Authors>
    					
  4. 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="1.0" encoding="utf-8" ?> 
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     
     <xsl:template match="Books">
     <HTML>
     <BODY>
    
     <H3>Book Listing</H3>
      
     <TABLE BORDER="1">
    
     <TR>
    	<TH>Title</TH>
    	<TH>Author</TH>
     </TR>
    
    
     <xsl:for-each select="Book">
        <xsl:variable name="vId" select="Author"/>
    	<TR>
    		<TD>  
    			  <xsl:value-of select="Title"/>  
    		</TD>
    		<TD>  
    			  <!-- 
    				The document() in the following statement references a non-existent XML document.
    				There is a typo in the file name (Author.xml instead of Authors.xml)
    			  -->				
    			       
    			  <xsl:value-of select="document('Author.xml')/Authors/Author[Id=$vId]/Name"/>
    			  
    			  
    		</TD>
    	</TR>
      </xsl:for-each>
     
     </TABLE>
     </BODY>
     </HTML>
     </xsl:template>
     
     </xsl:stylesheet>
    					
  5. 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 "Symptoms" section of this article.
  6. 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("Books.xml")
    
            Dim xslt As New XslTransform()
            xslt.Load("bookListing.xsl")
    
            Dim fstream As New System.IO.FileStream("bookListing.html", IO.FileMode.Create)
            xslt.Transform(xml, Nothing, fstream)
            fstream.Close()
    
            Console.WriteLine("Done")
        End Sub
    
    End Module
    
    					
  7. 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
  8. Click the Show All Files and Refresh buttons in the Visual Studio. NET Solution Explorer window to display the sub-folders of the project.
  9. 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.
  10. 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="document('Authors.xml')/Authors/Author[Id=$vId]/Name"/>
    					
  11. 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.

REFERENCES


Modification Type:MajorLast Reviewed:9/22/2003
Keywords:kbprb KB325692