System.Xml.Xsl.XsltContext Class

Assembly: System.Xml.dll
Namespace: System.Xml.Xsl
Summary
Encapsulates the current execution context of the XSLT processor allowing XPath to resolve functions, parameters, and namespaces within XPath expressions.
C# Syntax:
public abstract class XsltContext : XmlNamespaceManager
See also:
System.Xml.Xsl Namespace

System.Xml.Xsl.XsltContext Member List:

Public Constructors
ctor #1 Overloaded:
.ctor()

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the XsltContext class.
ctor #2 Overloaded:
.ctor(NameTable table)

Initializes a new instance of the XsltContext class with the specified NameTable.
Public Properties
DefaultNamespace
(inherited from System.Xml.XmlNamespaceManager)
Read-only

See base class member description: System.Xml.XmlNamespaceManager.DefaultNamespace


Gets the namespace URI for the default namespace.
NameTable
(inherited from System.Xml.XmlNamespaceManager)
Read-only

See base class member description: System.Xml.XmlNamespaceManager.NameTable


Gets the XmlNameTable associated with this object.
Whitespace Read-only

When overridden in a derived class, gets a value indicating whether to include white space nodes in the output.
Public Methods
AddNamespace
(inherited from System.Xml.XmlNamespaceManager)
See base class member description: System.Xml.XmlNamespaceManager.AddNamespace


Adds the given namespace to the collection.
CompareDocument When overridden in a derived class, compares the base URIs of two documents based upon the order the documents were loaded by the XSLT processor (that is the XslTransform class).
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetEnumerator
(inherited from System.Xml.XmlNamespaceManager)
See base class member description: System.Xml.XmlNamespaceManager.GetEnumerator


Provides support for the "foreach" style iteration over the collection of namespaces in the XmlNamespaceManager.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
HasNamespace
(inherited from System.Xml.XmlNamespaceManager)
See base class member description: System.Xml.XmlNamespaceManager.HasNamespace


Gets a value indicating whether the supplied prefix has a namespace defined for the current pushed scope.
LookupNamespace
(inherited from System.Xml.XmlNamespaceManager)
See base class member description: System.Xml.XmlNamespaceManager.LookupNamespace


Gets the namespace URI for the specified prefix.
LookupPrefix
(inherited from System.Xml.XmlNamespaceManager)
See base class member description: System.Xml.XmlNamespaceManager.LookupPrefix


Finds the prefix declared for the given namespace URI.
PopScope
(inherited from System.Xml.XmlNamespaceManager)
See base class member description: System.Xml.XmlNamespaceManager.PopScope


Pops a namespace scope off the stack.
PreserveWhitespace When overridden in a derived class, evaluates whether to preserve white space nodes or strip them for the given context.
PushScope
(inherited from System.Xml.XmlNamespaceManager)
See base class member description: System.Xml.XmlNamespaceManager.PushScope


Pushes a namespace scope onto the stack.
RemoveNamespace
(inherited from System.Xml.XmlNamespaceManager)
See base class member description: System.Xml.XmlNamespaceManager.RemoveNamespace


Removes the given namespace for the given prefix.
ResolveFunction When overridden in a derived class, resolves a function reference and returns an IXsltContextFunction representing the function. The IXsltContextFunction is used at execution time to get the return value of the function.
ResolveVariable When overridden in a derived class, resolves variable reference and returns an IXsltContextVariable representing the variable.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Xml.Xsl.XsltContext Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the XsltContext class.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public XsltContext();

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the XsltContext class with the specified NameTable.
C# Syntax:
public XsltContext(
   NameTable table
);
Parameters:

table

The NameTable to use.

Return to top


Property: DefaultNamespace (read-only)
Inherited
See base class member description: System.Xml.XmlNamespaceManager.DefaultNamespace

Summary
Gets the namespace URI for the default namespace.
C# Syntax:
public virtual string DefaultNamespace {get;}
Remarks
This method is equivalent to calling LookupNamespace(String.Empty).
Example
The following example displays the default namespace, if one exists.
if (nsmgr.HasNamespace(String.Empty))
  Console.WriteLine(nsmgr.DefaultNamespace);

    
See also:
XmlNamespaceManager.LookupNamespace

Return to top


Property: NameTable (read-only)
Inherited
See base class member description: System.Xml.XmlNamespaceManager.NameTable

Summary
Gets the XmlNameTable associated with this object.
C# Syntax:
public XmlNameTable NameTable {get;}
Remarks
The name table is used to look up prefixes and namespace URIs.
See also:
XmlReader

Return to top


Property: Whitespace (read-only)
Summary
When overridden in a derived class, gets a value indicating whether to include white space nodes in the output.
C# Syntax:
public abstract bool Whitespace {get;}

Return to top


Method: AddNamespace(
   string prefix,
   string uri
)
Inherited
See base class member description: System.Xml.XmlNamespaceManager.AddNamespace

Summary
Adds the given namespace to the collection.
C# Syntax:
public virtual void AddNamespace(
   string prefix,
   string uri
);
Parameters:

prefix

The prefix to associate with the namespace being added. Use String.Empty to add a default namespace.

uri

The namespace to add.

Exceptions
Exception Type Condition
InvalidOperationException The value for prefix is "xmlns".
Remarks
XmlNamespaceManager does not check prefix and uri for conformance.

XmlReader checks names, including prefixes and namespaces, to ensure they are valid XML names according to the W3C specification.XmlNamespaceManager is used internally by XmlReader, so to avoid a duplication of efforts, XmlNamespaceManager assumes all prefixes and namespaces are valid.

If the prefix and namespace already exists within the current scope, it will replace the existing prefix/namespace combination. The same prefix and namespace combination can exist across different scopes.

The following prefix/namespace pairs are added by default to the XmlNamespaceManager. They can be determined at any scope.



Prefix Namespace
xmlns http://www.w3.org/2000/xmlns/ (the xmlns prefix namespace)
xml http://www.w3.org/XML/1998/namespace (The XML namespace)
String.Empty String.Empty (The empty namespace). This value can be reassigned a different prefix. For example, xmlns="" defines the default namespace to be the empty namespace
Example
The following example uses XmlNamespaceManager to resolve namespaces in an XML fragment.
using System;
using System.IO;
using System.Xml;

//Reads an XML fragment

public class Sample
{
  public static void Main()
  {
    try
    {
      //The string containing the XML to read
      String xmlFrag="<book>" +
                     "<title>Pride And Prejudice</title>" +
                     "<author>" +
                     "<first-name>Jane</first-name>" +
                     "<last-name>Austen</last-name>" +
                     "</author>" +
                     "<curr:price>19.95</curr:price>" +
                     "<misc>&h;</misc>" +
                     "</book>";  

      ReadSample sample = new ReadSample(xmlFrag);
      sample.ReadIt();
            
    }
    catch (Exception e)
    {
      Console.WriteLine ("Exception: {0}", e.ToString());
    }

  }

} // End class

//Reads an XML fragment 

public class ReadSample
{
   XmlValidatingReader reader = null;
   
   public ReadSample(String xmlFrag)
   {

     //create an XmlNamespaceManager to resolve namespaces
     NameTable nt = new NameTable();
     XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
     nsmgr.AddNamespace(String.Empty, "urn:samples"); //default namespace
     nsmgr.AddNamespace("curr", "urn:samples:dollar");

     //Create an XmlParserContext.  The XmlParserContext contains all the information
     //required to parse the XML fragment, including the entity information
     XmlParserContext context;
     String subset="<!ENTITY h 'hardcover'>";
     context=new XmlParserContext(nt, nsmgr, "book", null, null, subset, null, null, XmlSpace.None);

     //create the reader
     reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);

   }

   //Read the XML fragment 
   public void ReadIt()
   {
    try
    {
      while(reader.Read())
      {
        if (reader.HasValue)
          Console.WriteLine("{0} [{1}] = {2}",reader.NodeType, reader.Name, reader.Value);
        else
          Console.WriteLine("{0} [{1}]",reader.NodeType, reader.Name);
      }

    }
    catch (Exception e)
    {
      Console.WriteLine ("Exception: {0}", e.ToString());
    }
   }
 
} //End class

    

Return to top


Method: CompareDocument(
   string baseUri,
   string nextbaseUri
)
Summary
When overridden in a derived class, compares the base URIs of two documents based upon the order the documents were loaded by the XSLT processor (that is the XslTransform class).
C# Syntax:
public abstract int CompareDocument(
   string baseUri,
   string nextbaseUri
);
Parameters:

baseUri

The base URI of the first document to compare.

nextbaseUri

The base URI of the second document to compare.

Return Value:
An integer value describing the relative order of the two base URIs.-1 if baseUri occurs before nextbaseUri, 0 if the two base URIs are identical and 1 if baseUri occurs after nextbaseUri.
Remarks
This method supports the XSLT document() function which specifies that nodes selected across multiple documents are always returned in the same order.Note: the order is implementation specific.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~XsltContext();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetEnumerator()
Inherited
See base class member description: System.Xml.XmlNamespaceManager.GetEnumerator

Summary
Provides support for the "foreach" style iteration over the collection of namespaces in the XmlNamespaceManager.
C# Syntax:
public virtual IEnumerator GetEnumerator();
Return Value:
An IEnumerator.
Implements:
IEnumerable.GetEnumerator

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: HasNamespace(
   string prefix
)
Inherited
See base class member description: System.Xml.XmlNamespaceManager.HasNamespace

Summary
Gets a value indicating whether the supplied prefix has a namespace defined for the current pushed scope.
C# Syntax:
public virtual bool HasNamespace(
   string prefix
);
Parameters:

prefix

The prefix of the namespace you want to find.

Return Value:
true if there is a namespace defined; otherwise, false.
Remarks
To determine whether there is a default empty namespace defined, set prefix to String.Empty. If the method returns true, this indicates that there is a default namespace defined in current scope. Returning false indicates that no default namespace is defined.Note: xmlns:x= "" is illegal according to W3C Namespaces in XML recommendation.
Example
The following example displays the default namespace, if one it exists.
if (nsmgr.HasNamespace(String.Empty))
  Console.WriteLine(nsmgr.DefaultNamespace);

    

Return to top


Method: LookupNamespace(
   string prefix
)
Inherited
See base class member description: System.Xml.XmlNamespaceManager.LookupNamespace

Summary
Gets the namespace URI for the specified prefix.
C# Syntax:
public virtual string LookupNamespace(
   string prefix
);
Parameters:

prefix

The prefix whose namespace URI you want to resolve. To match the default namespace, pass String.Empty.

Return Value:
Returns the namespace URI for prefix or null if there is no mapped namespace. The returned string is atomized.

For more information on atomized strings, see XmlNameTable.

Example
The following example adds prefix/namespace pairs to the XmlNamespaceManager and then displays all the pairs in the collection.


using System;
using System.IO;
using System.Xml;


public class Sample
{
  public static void Main()
  {
    Sample test = new Sample();
  }
  public Sample()
  {
    //Create the XmlNamespaceManager.
    NameTable nt = new NameTable();
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);

    //Add prefix/namespace pairs to the XmlNamespaceManager.
    nsmgr.AddNamespace("", "www.wideworldimporters.com"); //Adds a default namespace.
    nsmgr.AddNamespace("europe", "www.wideworldimporters.com/europe");
    nsmgr.PushScope();  //Pushes a namespace scope on the stack.
    nsmgr.AddNamespace("", "www.lucernepublishing.com"); //Adds another default namespace.
    nsmgr.AddNamespace("partners", "www.lucernepublishing.com/partners");

    Console.WriteLine("Show all the prefix/namespace pairs in the XmlNamespaceManager...");
    ShowAllNamespaces(nsmgr);
  }

  private void ShowAllNamespaces(XmlNamespaceManager nsmgr)
  {
    do{
       foreach (String prefix in nsmgr)
       {
        Console.WriteLine("Prefix={0}, Namespace={1}", prefix,nsmgr.LookupNamespace(prefix));
       } 
    }
    while (nsmgr.PopScope());
  }
}

    

Return to top


Method: LookupPrefix(
   string uri
)
Inherited
See base class member description: System.Xml.XmlNamespaceManager.LookupPrefix

Summary
Finds the prefix declared for the given namespace URI.
C# Syntax:
public virtual string LookupPrefix(
   string uri
);
Parameters:

uri

The namespace to resolve for the prefix.

Return Value:
The matching prefix. If there is no mapped prefix, the method returns String.Empty. If a null value is supplied then null is returned.
Remarks
This method finds the mapped prefix by walking the stack (that is it looks globally). The supplied string must be atomized for the lookup to succeed. In other words the supplied string object must exist in the XmlNamespaceManager's XmlNamespaceManager.NameTable.

The returned string is also atomized. For more information on atomized strings, see XmlNameTable.

Example
The following example removes a namespace from the namespace manager.
String prefix = nsmgr.LookupPrefix("www.wideworldimporters.com/europe");
nsmgr.RemoveNamespace(prefix, "www.wideworldimporters.com/europe");

    

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: PopScope()
Inherited
See base class member description: System.Xml.XmlNamespaceManager.PopScope

Summary
Pops a namespace scope off the stack.
C# Syntax:
public virtual bool PopScope();
Return Value:
true if there are namespace scopes left on the stack; false if there are no more namespaces to pop.
Remarks
When you call this method, all namespaces which were added to XmlNamespaceManager (by calling XmlNamespaceManager.AddNamespace) since the last call to PopScope, are removed.
Example
The following example adds prefix/namespace pairs to the XmlNamespaceManager and then displays all the pairs in the collection.


using System;
using System.IO;
using System.Xml;


public class Sample
{
  public static void Main()
  {
    Sample test = new Sample();
  }
  public Sample()
  {
    //Create the XmlNamespaceManager.
    NameTable nt = new NameTable();
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);

    //Add prefix/namespace pairs to the XmlNamespaceManager.
    nsmgr.AddNamespace("", "www.wideworldimporters.com"); //Adds a default namespace.
    nsmgr.AddNamespace("europe", "www.wideworldimporters.com/europe");
    nsmgr.PushScope();  //Pushes a namespace scope on the stack.
    nsmgr.AddNamespace("", "www.lucernepublishing.com"); //Adds another default namespace.
    nsmgr.AddNamespace("partners", "www.lucernepublishing.com/partners");

    Console.WriteLine("Show all the prefix/namespace pairs in the XmlNamespaceManager...");
    ShowAllNamespaces(nsmgr);
  }

  private void ShowAllNamespaces(XmlNamespaceManager nsmgr)
  {
    do{
       foreach (String prefix in nsmgr)
       {
        Console.WriteLine("Prefix={0}, Namespace={1}", prefix,nsmgr.LookupNamespace(prefix));
       } 
    }
    while (nsmgr.PopScope());
  }
}

    

Return to top


Method: PreserveWhitespace(
   XPathNavigator node
)
Summary
When overridden in a derived class, evaluates whether to preserve white space nodes or strip them for the given context.
C# Syntax:
public abstract bool PreserveWhitespace(
   XPathNavigator node
);
Parameters:

node

The white space node that is to be preserved or stripped in the current context.

Return Value:
Returns true if the white space is to be preserved or false if the white space is to be stripped.

Return to top


Method: PushScope()
Inherited
See base class member description: System.Xml.XmlNamespaceManager.PushScope

Summary
Pushes a namespace scope onto the stack.
C# Syntax:
public virtual void PushScope();
Remarks
After a call to this method all namespaces, which are added to XmlNamespaceManager (by calling XmlNamespaceManager.AddNamespace), belong to the pushed namespace scope.
Example
The following example adds prefix/namespace pairs to the XmlNamespaceManager and then displays all the pairs in the collection.


using System;
using System.IO;
using System.Xml;


public class Sample
{
  public static void Main()
  {
    Sample test = new Sample();
  }
  public Sample()
  {
    //Create the XmlNamespaceManager.
    NameTable nt = new NameTable();
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);

    //Add prefix/namespace pairs to the XmlNamespaceManager.
    nsmgr.AddNamespace("", "www.wideworldimporters.com"); //Adds a default namespace.
    nsmgr.AddNamespace("europe", "www.wideworldimporters.com/europe");
    nsmgr.PushScope();  //Pushes a namespace scope on the stack.
    nsmgr.AddNamespace("", "www.lucernepublishing.com"); //Adds another default namespace.
    nsmgr.AddNamespace("partners", "www.lucernepublishing.com/partners");

    Console.WriteLine("Show all the prefix/namespace pairs in the XmlNamespaceManager...");
    ShowAllNamespaces(nsmgr);
  }

  private void ShowAllNamespaces(XmlNamespaceManager nsmgr)
  {
    do{
       foreach (String prefix in nsmgr)
       {
        Console.WriteLine("Prefix={0}, Namespace={1}", prefix,nsmgr.LookupNamespace(prefix));
       } 
    }
    while (nsmgr.PopScope());
  }
}

    

Return to top


Method: RemoveNamespace(
   string prefix,
   string uri
)
Inherited
See base class member description: System.Xml.XmlNamespaceManager.RemoveNamespace

Summary
Removes the given namespace for the given prefix.
C# Syntax:
public virtual void RemoveNamespace(
   string prefix,
   string uri
);
Parameters:

prefix

The prefix for the namespace

uri

The namespace to remove for the given prefix. The namespace removed is from the current namespace scope. Namespaces outside the current scope are ignored. Exceptions are never thrown.

Return to top


Method: ResolveFunction(
   string prefix,
   string name,
   XPathResultType[] ArgTypes
)
Summary
When overridden in a derived class, resolves a function reference and returns an IXsltContextFunction representing the function. The IXsltContextFunction is used at execution time to get the return value of the function.
C# Syntax:
public abstract IXsltContextFunction ResolveFunction(
   string prefix,
   string name,
   XPathResultType[] ArgTypes
);
Parameters:

prefix

The prefix of the function as it appears in the XPath expression.

name

The name of the function.

ArgTypes

An array of argument types for the function being resolved. This allows you to select between methods with the same name (for example, overloaded methods).

Return Value:
An IXsltContextFunction representing the function.

Return to top


Method: ResolveVariable(
   string prefix,
   string name
)
Summary
When overridden in a derived class, resolves variable reference and returns an IXsltContextVariable representing the variable.
C# Syntax:
public abstract IXsltContextVariable ResolveVariable(
   string prefix,
   string name
);
Parameters:

prefix

The prefix of the variable as it appears in the XPath expression.

name

The name of the variable.

Return Value:
An IXsltContextVariable representing the variable at runtime.
Remarks
The IXsltContextVariable is used at execution time to get the value of the variable.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.