HOW TO: Access Protected Document in Document Function of XSLT Using Visual Basic .NET (330587)
The information in this article applies to:
- Microsoft XML Classes (included with the .NET Framework 1.0)
- Microsoft XML Classes (included with the .NET Framework 1.1)
- Microsoft Visual Basic .NET (2002)
- Microsoft Visual Basic .NET (2003)
This article was previously published under Q330587 SUMMARY This step-by-step article describes how to create an instance of and supply a custom XmlUrlResolver class to resolve document() function references to external XML resources that are located on authenticated Web sites or folders. The XmlTransform class uses the underlying XmlResolver class to read the files that are referenced in the XSL. You can specify the credentials to the XmlResolver. The XmlResolver class supports the following three methods to authenticate Web requests: - Basic authentication
- Digest authentication
- Windows authentication
back to the topTo create a sample XML Document and XSLT Document, follow these steps: Note XML Document and XSLT Document are used by the sample code in the "Create and Run the Code" section of this article. - 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
subfolder 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> - Create a virtual directory on the Web server, and then name it vdir.
- Right-click vdir, and then click Properties. On the Directory Security tab, click Edit. Clear the Anonymous access check box, and then select the Basic
authentication check box.
- 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 vdir folder of Web server:
<?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> - 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 subfolder 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>
<xsl:value-of select="document('http://server/vdir/Author.xml')/Authors/Author[Id=$vId]/Name"/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet> 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. back to the top- In Visual Studio. NET, create a new Visual Basic .NET
Console Application project.
- Use the following code to replace the default code that is
generated in module1.vb (in the Visual Basic .NET project):
Imports System
Imports System.Xml.XPath
Imports System.Xml.Xsl
Imports System.Net
Module Module1
Sub Main()
Dim xmldoc 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)
Dim urlResolver As New Xml.XmlUrlResolver()
' You can uncomment the following line to use Intergrated Authentication
'urlResolver.Credentials = CredentialCache.DefaultCredentials
'You can uncomment the following three lines to use the Basic or Digest Authentication
'Before you do this, you must make sure that the User has sufficient permission to access the Web site
'And that IIS is configured correctly, or the accessing will fail
'Dim cache As New CredentialCache()
'cache.Add(New Uri("http://server/vdir"), "Basic", New NetworkCredential("UserName", "Password"))
'cache.Add(New Uri("http://server/vdir"), "Digest", New NetworkCredential("UserName", "Password"))
'urlResolver.Credentials = cache
xslt.XmlResolver = urlResolver
xslt.Transform(xmldoc, Nothing, fstream)
fstream.Close()
Console.WriteLine("Done")
End Sub
End Module
- 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 subfolder of the project.
- In Solution Explorer, click the Show All Files and Refresh buttons to display the subfolders of the project.
- Expand the Bin subfolder. 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 behavior occurs because the XmlResolver that is used by the XSLT
processor cannot read the Author.xml document. Credentials were not provided to
access the protected document.
- The commented code gives the XSLT processor a
credential to access the Web. The codes use two authentication methods. Uncomment one of the code blocks.
- Save the change, and then run the project. Open the BookListing.html file that is
generated 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.
back to the topREFERENCES
For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
325692
PRB: Document Function in XSLT Style Sheet Does Not Raise a .NET Exception
308135 HOW TO: Create a Virtual Directory in Windows 2000
back to the top
Modification Type: | Major | Last Reviewed: | 9/4/2003 |
---|
Keywords: | kbHOWTOmaster KB330587 kbAudDeveloper |
---|
|