共用方式為


使用者定義函式和變數

XPathNavigator 類別會提供一組可用來與 XPathDocument 資料互動的方法。 您可以透過實作擴充函式和變數供 XPath 查詢運算式使用,提供標準 XPath 函式。 SetContext 方法可以接受衍生自 XsltContext 的使用者定義內容。 使用者定義函式是由自訂內容解析。

擴充函式和變數在防範 XML 插入式攻擊時可能很有用。 在這些案例中,使用者輸入會指派給自訂變數並由擴充函式處理,而非與處理指示串連的原始輸入。 擴充函式和變數包含使用者輸入,因此它只會依照設計者的預期方式針對 XML 資料運作。

若要使用擴充功能,您必須實作自訂 XsltContext 類別以及支援擴充函式和變數的 IXsltContextFunctionIXsltContextVariable 介面。 XPathExpression 會將具有其 XsltArgumentList 的使用者輸入加入至自訂 XsltContext

XPathExpression 代表 XPathNavigator 用來尋找和處理運算式所識別之節點的已編譯查詢。

下列範例顯示衍生自 XsltContext 之自訂內容類別的實作。 程式碼中的註解將描述類別成員以及它們在自訂函式中的用法。 函式和變數實作以及使用這些實作的範例應用程式都會遵循這個程式碼區段。

class CustomContext : System.Xml.Xsl.XsltContext
{
    private const string ExtensionsNamespaceUri = "http://xpathExtensions";
    // XsltArgumentList to store names and values of user-defined variables.
    private XsltArgumentList argList;

    public CustomContext()
    {
    }

    public CustomContext(NameTable nt, XsltArgumentList args)
        : base(nt)
    {
        argList = args;
    }

    // Function to resolve references to user-defined XPath extension
    // functions in XPath query expressions evaluated by using an
    // instance of this class as the XsltContext.
    public override System.Xml.Xsl.IXsltContextFunction ResolveFunction(
                                string prefix, string name,
                                System.Xml.XPath.XPathResultType[] argTypes)
    {
        // Verify namespace of function.
        if (this.LookupNamespace(prefix) == ExtensionsNamespaceUri)
        {
            string strCase = name;

            switch (strCase)
            {
                case "CountChar":
                    return new XPathExtensionFunctions(2, 2, XPathResultType.Number,
                                                                argTypes, "CountChar");

                case "FindTaskBy": // This function is implemented but not called.
                    return new XPathExtensionFunctions(2, 2, XPathResultType.String,
                                                                argTypes, "FindTaskBy");

                case "Right": // This function is implemented but not called.
                    return new XPathExtensionFunctions(2, 2, XPathResultType.String,
                                                                    argTypes, "Right");

                case "Left": // This function is implemented but not called.
                    return new XPathExtensionFunctions(2, 2, XPathResultType.String,
                                                                     argTypes, "Left");
            }
        }
        // Return null if none of the functions match name.
        return null;
    }

    // Function to resolve references to user-defined XPath
    // extension variables in XPath query.
    public override System.Xml.Xsl.IXsltContextVariable ResolveVariable(
                                                     string prefix, string name)
    {
        if (this.LookupNamespace(prefix) == ExtensionsNamespaceUri || !prefix.Equals(string.Empty))
        {
            throw new XPathException(string.Format("Variable '{0}:{1}' is not defined.", prefix, name));
        }

        // Verify name of function is defined.
        if (name.Equals("text") || name.Equals("charToCount") ||
            name.Equals("right") || name.Equals("left"))
        {
            // Create an instance of an XPathExtensionVariable
            // (custom IXsltContextVariable implementation) object
            //  by supplying the name of the user-defined variable to resolve.
            XPathExtensionVariable var;
            var = new XPathExtensionVariable(prefix, name);

            // The Evaluate method of the returned object will be used at run time
            // to resolve the user-defined variable that is referenced in the XPath
            // query expression.
            return var;
        }
        return null;
    }

    // Empty implementation, returns false.
    public override bool PreserveWhitespace(System.Xml.XPath.XPathNavigator node)
    {
        return false;
    }

    // empty implementation, returns 0.
    public override int CompareDocument(string baseUri, string nextbaseUri)
    {
        return 0;
    }

    public override bool Whitespace
    {
        get
        {
            return true;
        }
    }

    // The XsltArgumentList property is accessed by the Evaluate method of the
    // XPathExtensionVariable object that the ResolveVariable method returns. It is used
    // to resolve references to user-defined variables in XPath query expressions.
    public XsltArgumentList ArgList
    {
        get
        {
            return argList;
        }
    }
}

Class CustomContext
    Inherits XsltContext

    Private Const ExtensionsNamespaceUri As String = "http://xpathExtensions"

    ' XsltArgumentList to store names and values of user-defined variables.
    Private m_ArgList As XsltArgumentList

    Public Sub New()

    End Sub

    Public Sub New(ByVal NT As NameTable, ByVal Args As XsltArgumentList)
        MyBase.New(NT)
        m_ArgList = Args
    End Sub

    ' Empty implementation, returns 0.
    Public Overrides Function CompareDocument(ByVal BaseUri As String, ByVal NextBaseUri As String) As Integer
        Return 0
    End Function

    ' Empty implementation, returns false.
    Public Overrides Function PreserveWhitespace(ByVal Node As XPathNavigator) As Boolean
        Return False
    End Function

    Public Overrides Function ResolveFunction(ByVal Prefix As String, ByVal Name As String, ByVal ArgTypes() As XPathResultType) As IXsltContextFunction

        If LookupNamespace(Prefix) = ExtensionsNamespaceUri Then
            Select Case Name
                Case "CountChar"
                    Return New XPathExtensionFunctions(2, 2, _
                                    XPathResultType.Number, ArgTypes, "CountChar")

                Case "FindTaskBy" ' Implemented but not called.
                    Return New XPathExtensionFunctions(2, 2, _
                                    XPathResultType.String, ArgTypes, "FindTaskBy")

                Case "Right"  ' Implemented but not called.
                    Return New XPathExtensionFunctions(2, 2, _
                                    XPathResultType.String, ArgTypes, "Right")

                Case "Left"   ' Implemented but not called.
                    Return New XPathExtensionFunctions(2, 2, _
                                    XPathResultType.String, ArgTypes, "Left")

                Case Else

            End Select
        End If
        ' Return Nothing if none of the functions match name.
        Return Nothing

    End Function

    ' Function to resolve references to user-defined XPath
    ' extension variables in XPath query.
    Public Overrides Function ResolveVariable(ByVal Prefix As String, ByVal Name As String) As IXsltContextVariable
        If LookupNamespace(Prefix) = ExtensionsNamespaceUri OrElse Len(Prefix) > 0 Then
            Throw New XPathException(String.Format("Variable '{0}:{1}' is not defined.", Prefix, Name))
        End If

        Select Case Name
            Case "charToCount", "left", "right", "text"
                ' Create an instance of an XPathExtensionVariable
                ' (custom IXsltContextVariable implementation) object
                ' by supplying the name of the user-defined variable to resolve.
                Return New XPathExtensionVariable(Prefix, Name)

                ' The Evaluate method of the returned object will be used at run time
                ' to resolve the user-defined variable that is referenced in the XPath
                ' query expression.
            Case Else

        End Select
        ' Return Nothing if none of the variables match name.
        Return Nothing

    End Function

    Public Overrides ReadOnly Property Whitespace() As Boolean
        Get
            Return True
        End Get
    End Property

    ' The XsltArgumentList property is accessed by the Evaluate method of the
    ' XPathExtensionVariable object that the ResolveVariable method returns.
    ' It is used to resolve references to user-defined variables in XPath query
    ' expressions.
    Public ReadOnly Property ArgList() As XsltArgumentList
        Get
            Return m_ArgList
        End Get
    End Property
End Class

下列程式碼會實作 IXsltContextFunction。 實作 IXsltContextFunction 的類別會解析並執行使用者定義函式。 此範例會使用下列宣告所識別的函式:private int CountChar(string title, char charToCount)

程式碼註解將描述類別成員。

// The interface that resolves and executes a specified user-defined function.
public class XPathExtensionFunctions : System.Xml.Xsl.IXsltContextFunction
{
    // The data types of the arguments passed to XPath extension function.
    private System.Xml.XPath.XPathResultType[] argTypes;
    // The minimum number of arguments that can be passed to function.
    private int minArgs;
    // The maximum number of arguments that can be passed to function.
    private int maxArgs;
    // The data type returned by extension function.
    private System.Xml.XPath.XPathResultType returnType;
    // The name of the extension function.
    private string FunctionName;

    // Constructor used in the ResolveFunction method of the custom XsltContext
    // class to return an instance of IXsltContextFunction at run time.
    public XPathExtensionFunctions(int minArgs, int maxArgs,
        XPathResultType returnType, XPathResultType[] argTypes, string functionName)
    {
        this.minArgs = minArgs;
        this.maxArgs = maxArgs;
        this.returnType = returnType;
        this.argTypes = argTypes;
        this.FunctionName = functionName;
    }

    // Readonly property methods to access private fields.
    public System.Xml.XPath.XPathResultType[] ArgTypes
    {
        get
        {
            return argTypes;
        }
    }
    public int Maxargs
    {
        get
        {
            return maxArgs;
        }
    }

    public int Minargs
    {
        get
        {
            return maxArgs;
        }
    }

    public System.Xml.XPath.XPathResultType ReturnType
    {
        get
        {
            return returnType;
        }
    }

    // XPath extension functions.

    private int CountChar(XPathNodeIterator node, char charToCount)
    {
        int charCount = 0;
        for (int charIdx = 0; charIdx < node.Current.Value.Length; charIdx++)
        {
            if (node.Current.Value[charIdx] ==  charToCount)
            {
                charCount++;
            }
        }
        return charCount;
    }

    // This overload will not force the user
    // to cast to string in the xpath expression
    private string FindTaskBy(XPathNodeIterator node, string text)
    {
        if (node.Current.Value.Contains(text))
            return node.Current.Value;
        else
            return "";
    }

    private string Left(string str, int length)
    {
        return str.Substring(0, length);
    }

    private string Right(string str, int length)
    {
        return str.Substring((str.Length - length), length);
    }

    // Function to execute a specified user-defined XPath extension
    // function at run time.
    public object Invoke(System.Xml.Xsl.XsltContext xsltContext,
                   object[] args, System.Xml.XPath.XPathNavigator docContext)
    {
        if (FunctionName == "CountChar")
            return (Object)CountChar((XPathNodeIterator)args[0],
                                             Convert.ToChar(args[1]));
        if (FunctionName == "FindTaskBy")
            return FindTaskBy((XPathNodeIterator)args[0],
                                          Convert.ToString(args[1]));

        if (FunctionName == "Left")
            return (Object)Left(Convert.ToString(args[0]),
                                             Convert.ToInt16(args[1]));

        if (FunctionName == "Right")
            return (Object)Right(Convert.ToString(args[0]),
                                             Convert.ToInt16(args[1]));

        return null;
    }
}

' The interface that resolves and executes a specified user-defined function.
Public Class XPathExtensionFunctions
    Implements IXsltContextFunction

    ' The data types of the arguments passed to XPath extension function.
    Private m_ArgTypes() As XPathResultType
    ' The minimum number of arguments that can be passed to function.
    Private m_MinArgs As Integer
    ' The maximum number of arguments that can be passed to function.
    Private m_MaxArgs As Integer
    ' The data type returned by extension function.
    Private m_ReturnType As XPathResultType
    ' The name of the extension function.
    Private m_FunctionName As String

    ' Constructor used in the ResolveFunction method of the custom XsltContext
    ' class to return an instance of IXsltContextFunction at run time.
    Public Sub New(ByVal MinArgs As Integer, ByVal MaxArgs As Integer, ByVal ReturnType As XPathResultType, ByVal ArgTypes() As XPathResultType, ByVal FunctionName As String)
        m_MinArgs = MinArgs
        m_MaxArgs = MaxArgs
        m_ReturnType = ReturnType
        m_ArgTypes = ArgTypes
        m_FunctionName = FunctionName
    End Sub

    ' Readonly property methods to access private fields.
    Public ReadOnly Property ArgTypes() As XPathResultType() Implements IXsltContextFunction.ArgTypes
        Get
            Return m_ArgTypes
        End Get
    End Property

    Public ReadOnly Property MaxArgs() As Integer Implements IXsltContextFunction.Maxargs
        Get
            Return m_MaxArgs
        End Get
    End Property

    Public ReadOnly Property MinArgs() As Integer Implements IXsltContextFunction.Minargs
        Get
            Return m_MinArgs
        End Get
    End Property

    Public ReadOnly Property ReturnType() As XPathResultType Implements IXsltContextFunction.ReturnType
        Get
            Return m_ReturnType
        End Get
    End Property

    ' Function to execute a specified user-defined XPath
    ' extension function at run time.
    Public Function Invoke(ByVal Context As XsltContext, ByVal Args() As Object, ByVal DocContext As XPathNavigator) As Object Implements IXsltContextFunction.Invoke

        Select Case m_FunctionName
            Case "CountChar"
                Return CountChar(DirectCast(Args(0), XPathNodeIterator), CChar(Args(1)))
            Case "FindTaskBy"
                Return FindTaskBy(DirectCast(Args(0), XPathNodeIterator), CStr(Args(1).ToString()))
            Case "Left"
                Return Left(CStr(Args(0)), CInt(Args(1)))
            Case "Right"
                Return Right(CStr(Args(0)), CInt(Args(1)))
            Case Else

        End Select
        ' Return Nothing for unknown function name.
        Return Nothing

    End Function

    ' XPath extension functions.
    Private Function CountChar(ByVal Node As XPathNodeIterator, ByVal CharToCount As Char) As Integer

        Dim CharCount As Integer = 0

        For CharIndex As Integer = 0 To Node.Current.Value.Length - 1
            If Node.Current.Value(CharIndex) = CharToCount Then
                CharCount += 1
            End If
        Next

        Return CharCount

    End Function

    ' This overload will not force the user
    ' to cast to string in the xpath expression
    Private Function FindTaskBy(ByVal Node As XPathNodeIterator, ByVal Text As String) As String

        If (Node.Current.Value.Contains(Text)) Then
            Return Node.Current.Value
        Else
            Return ""
        End If

    End Function

End Class

下列程式碼會實作 IXsltContextVariable。 這個類別會在執行階段中解析 XPath 查詢運算式中使用者定義變數的參考。 這個類別的執行個體是由自訂 ResolveVariable 類別的覆寫 XsltContext 方法所建立並傳回。

程式碼註解將描述類別成員。

// The interface used to resolve references to user-defined variables
// in XPath query expressions at run time. An instance of this class
// is returned by the overridden ResolveVariable function of the
// custom XsltContext class.
public class XPathExtensionVariable : IXsltContextVariable
{
    // Namespace of user-defined variable.
    private string prefix;
    // The name of the user-defined variable.
    private string varName;

    // Constructor used in the overridden ResolveVariable function of custom XsltContext.
    public XPathExtensionVariable(string prefix, string varName)
    {
        this.prefix = prefix;
        this.varName = varName;
    }

    // Function to return the value of the specified user-defined variable.
    // The GetParam method of the XsltArgumentList property of the active
    // XsltContext object returns value assigned to the specified variable.
    public object Evaluate(System.Xml.Xsl.XsltContext xsltContext)
    {
        XsltArgumentList vars = ((CustomContext)xsltContext).ArgList;
        return vars.GetParam(varName, prefix);
    }

    // Determines whether this variable is a local XSLT variable.
    // Needed only when using a style sheet.
    public bool IsLocal
    {
        get
        {
            return false;
        }
    }

    // Determines whether this parameter is an XSLT parameter.
    // Needed only when using a style sheet.
    public bool IsParam
    {
        get
        {
            return false;
        }
    }

    public System.Xml.XPath.XPathResultType VariableType
    {
        get
        {
            return XPathResultType.Any;
        }
    }
}

' The interface used to resolve references to user-defined variables
' in XPath query expressions at run time. An instance of this class
' is returned by the overridden ResolveVariable function of the
' custom XsltContext class.
Public Class XPathExtensionVariable
    Implements IXsltContextVariable

    ' Namespace of user-defined variable.
    Private m_Prefix As String
    ' The name of the user-defined variable.
    Private m_VarName As String

    ' Constructor used in the overridden ResolveVariable function of custom XsltContext.
    Public Sub New(ByVal Prefix As String, ByVal VarName As String)
        m_Prefix = Prefix
        m_VarName = VarName
    End Sub

    ' Function to return the value of the specified user-defined variable.
    ' The GetParam method of the XsltArgumentList property of the active
    ' XsltContext object returns value assigned to the specified variable.
    Public Function Evaluate(ByVal Context As XsltContext) As Object Implements IXsltContextVariable.Evaluate
        Dim vars As XsltArgumentList = DirectCast(Context, CustomContext).ArgList
        Return vars.GetParam(m_VarName, m_Prefix)
    End Function

    ' Determines whether this variable is a local XSLT variable.
    ' Needed only when using a style sheet.
    Public ReadOnly Property IsLocal() As Boolean Implements IXsltContextVariable.IsLocal
        Get
            Return False
        End Get
    End Property

    ' Determines whether this parameter is an XSLT parameter.
    ' Needed only when using a style sheet.
    Public ReadOnly Property IsParam() As Boolean Implements IXsltContextVariable.IsParam
        Get
            Return False
        End Get
    End Property

    Public ReadOnly Property VariableType() As XPathResultType Implements IXsltContextVariable.VariableType
        Get
            Return XPathResultType.Any
        End Get
    End Property
End Class

如果上述類別定義位於範圍內,下列程式碼就會使用自訂函式來計算 Tasks.xml 文件項目中的字元。 程式碼中的註解將描述編譯自訂函式並針對 Tasks.xml 文件執行此函式的程式碼。

using System;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.IO;

namespace XPathExtensionFunctions
{
    class Program
    {
        static void Main(string[] args)
        {
            char keychar = ' ';

            while (true)
            {
                Console.Write("\r\nEnter key character to count in names: ");
                keychar = Console.ReadKey().KeyChar;
                if (keychar.Equals('q')) return;

                try
                {
                    // Load source XML into an XPathDocument object instance.
                    XPathDocument xmldoc = new XPathDocument("Tasks.xml");

                    // Create an XPathNavigator from the XPathDocument.
                    XPathNavigator nav = xmldoc.CreateNavigator();

                    //Create argument list and add the parameters.
                    XsltArgumentList varList = new XsltArgumentList();

                    varList.AddParam("charToCount", string.Empty, keychar);

                    // Create an instance of custom XsltContext object.
                    // Pass in the XsltArgumentList object
                    // in which the user-defined variable will be defined.
                    CustomContext context = new CustomContext(new NameTable(), varList);

                    // Add a namespace definition for the namespace prefix that qualifies the
                    // user-defined function name in the query expression.
                    context.AddNamespace("Extensions", "http://xpathExtensions");

                    // Create the XPath expression using extension function to select nodes
                    // that contain 2 occurrences of the character entered by user.
                    XPathExpression xpath = XPathExpression.Compile(
                        "/Tasks/Name[Extensions:CountChar(., $charToCount) = 2]");

                    xpath.SetContext(context);
                    XPathNodeIterator iter = nav.Select(xpath);

                    if (iter.Count.Equals(0))
                        Console.WriteLine("\n\n\rNo results contain 2 instances of "
                                                                 + keychar.ToString());
                    else
                    {
                        Console.WriteLine("\n\n\rResults that contain 2 instances of : "
                                                                 + keychar.ToString());
                        // Iterate over the selected nodes and output the
                        // results filtered by extension function.
                        while (iter.MoveNext())
                        {
                            Console.WriteLine(iter.Current.Value);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Xsl
Imports System.IO

Module Program

    Sub Main()

        Dim KeyChar As Char = " "

        While (True)
            Console.WriteLine()
            Console.Write("Enter key character to count in names: ")
            KeyChar = Console.ReadKey().KeyChar()
            If KeyChar = "q" Then Return

            Try
                ' Load source XML into an XPathDocument object instance.
                Dim XmlDoc As New XPathDocument("Tasks.xml")
                ' Create an XPathNavigator from the XPathDocument.
                Dim Navigator As XPathNavigator = XmlDoc.CreateNavigator()

                ' Create argument list and add the parameters.
                Dim VarList As New XsltArgumentList()
                VarList.AddParam("charToCount", "", KeyChar)

                ' Create an instance of custom XsltContext object.
                ' Pass in the XsltArgumentList object in which
                ' the user-defined variable will be defined.
                Dim Context As New CustomContext(New NameTable(), VarList)

                ' Add a namespace definition for the namespace prefix that qualifies
                ' the user-defined function name in the query expression.
                Context.AddNamespace("Extensions", "http://xpathExtensions")

                ' Create the XPath expression using extension function select nodes
                ' that contain 3 occurrences of the character entered by user.
                Dim XPath As XPathExpression = _
                   XPathExpression.Compile("/Tasks/Name[Extensions:CountChar(., $charToCount) = 2]")

                XPath.SetContext(Context)
                Dim Iterator As XPathNodeIterator = Navigator.Select(XPath)

                Console.WriteLine(vbCrLf)
                If Iterator.Count = 0 Then
                    Console.WriteLine("No results contain 2 instances of {0}.", _
                        KeyChar.ToString())
                Else
                    Console.WriteLine("Results that contain 2 instances of {0}: ", _
                        KeyChar.ToString())
                    ' Iterate over the selected nodes and output
                    ' the results filtered by extension function.
                    While Iterator.MoveNext()
                        Console.WriteLine(Iterator.Current.Value)
                    End While
                end if
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End While

    End Sub

End Module

此範例會使用下列 XML 資料。

<?xml version="1.0" encoding="utf-8" ?>
<Tasks>
    <Name>Reserve orders by customer</Name>
    <Name>Reserve orders by region</Name>
    <Name>Reserve orders by phone number</Name>
    <Name>Reserve orders by priority</Name>
    <Name>Total orders by customer</Name>
    <Name>Total orders by region</Name>
    <Name>Total orders by phone number</Name>
    <Name>Total orders by priority</Name>
    <Name>Schedule delivery by customer</Name>
    <Name>Schedule delivery by region</Name>
    <Name>Schedule delivery by phone number</Name>
    <Name>Schedule delivery by priority</Name>
    <Name>Follow up delivery by customer</Name>
    <Name>Follow up delivery by region</Name>
    <Name>Follow up delivery by phone number</Name>
    <Name>Follow up delivery by priority</Name>
</Tasks>