10

私の現在のプログラムでは、XmlDocument に適用する XPathExpression インスタンスをプログラムで作成する必要があります。xpath では、「ends-with」などの XPath 関数を使用する必要があります。しかし、XPath で「ends-with」を使用する方法が見つかりません。私

以下のような例外をスローします

未処理の例外: System.Xml.XPath.XPathException: Namespace Manager または XsltC ontext が必要です。このクエリには、プレフィックス、変数、またはユーザー定義関数があります。
MS.Internal.Xml.XPath.CompiledXpathExpr.get_QueryTree() で System.Xml.XPath.XPathNavigator.Evaluate (XPathExpression expr、XPathNodeIterator コンテキスト)
で System.Xml.XPath.XPathNavigator.Evaluate (XPathExpression expr) で

コードは次のようになります。

    XmlDocument xdoc = new XmlDocument();
    xdoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8"" ?>
                        <myXml xmlns=""http://MyNamespace"" xmlns:fn=""http://www.w3.org/2005/xpath-functions""> 
                        <data>Hello World</data>
                    </myXml>");
    XPathNavigator navigator = xdoc.CreateNavigator();

    XPathExpression xpr;
    xpr = XPathExpression.Compile("fn:ends-with(/myXml/data, 'World')");

    object result = navigator.Evaluate(xpr);
    Console.WriteLine(result);

以下のように、式をコンパイルするときに XmlNamespaceManager を挿入するようにコードを変更しようとしました

    XmlDocument xdoc = new XmlDocument();
    xdoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8"" ?>
                        <myXml xmlns=""http://MyNamespace"" xmlns:fn=""http://www.w3.org/2005/xpath-functions""> 
                        <data>Hello World</data>
                    </myXml>");
    XPathNavigator navigator = xdoc.CreateNavigator();
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
    nsmgr.AddNamespace("fn", "http://www.w3.org/2005/xpath-functions");

    XPathExpression xpr;
    xpr = XPathExpression.Compile("fn:ends-with(/myXml/data, 'World')", nsmgr);

    object result = navigator.Evaluate(xpr);
    Console.WriteLine(result);

XPathExpression.Compile 呼び出しで失敗します。

未処理の例外: System.Xml.XPath.XPathException: 不明な関数のため、このクエリには XsltContext が必要です。MS.Internal.Xml.XPath.CompiledXpathExpr.UndefinedXsltContext.ResolveFuncti on(String prefix, String name, XPathResultType[] ArgTypes) で MS.Internal.Xml.XPath.FunctionQuery.SetXsltContext(XsltContext コンテキスト) で MS.Internal.Xml. System.Xml.XPath.XPathExpression.Compile(String xpath, IXmlNamespaceResolver nsResolver) の XPath.CompiledXpathExpr.SetContext(XmlNamespaceManager nsM anager)

XPathExpression.Compile で既製の XPath 関数を使用するコツを知っている人はいますか? ありがとう

4

1 に答える 1

34

この関数は XPath 1.0に対しては定義されておらず、 XPath 2.0およびXQueryに対してのみ定義されています。ends-with()

.NET を使用しています。. NET は現時点でXPath 2.0XSLT 2.0、またはXQueryを実装していません

関数と同じ結果を生成する XPath 1.0 式を簡単に作成できますends-with()

$str2 = substring($str1, string-length($str1)- string-length($str2) +1)

以下と同じブール結果 (true()またはfalse()) を生成します。

ends-with($str1, $str2)

$str1具体的なケースでは、 and を正しい式に置き換えるだけです$str2。したがって、/myXml/data'World'です。

ends-with(/myXml/data, 'World')したがって、使用する XPath 1.0 式は、XPath 2.0 式と同等です。

'World' = 
   substring(/myXml/data,
             string-length(/myXml/data) - string-length('World') +1
             )
于 2008-12-31T05:24:40.987 に答える