Saxon扩展:XdmNode上的XPath

时间:2017-02-10 09:46:00

标签: c# .net xslt xslt-2.0 saxon

您好我尝试在C#中实现撒克逊的扩展。我使用saxon9he界面。扩展本身工作正常,但现在我想使用XPath表达式从节点获取值。我把它分解为相关的代码部分(其余的工作正常)。

扩展名有两个参数。第一个是字符串,第二个是节点集。

public override IXdmEnumerator Call(IXdmEnumerator[] arguments, DynamicContext context)
{
    if (arguments.Length == 2)
    {
        arguments[0].MoveNext();

        string text = (arguments[0].Current as XdmAtomicValue).Value as string;
        IXdmEnumerator enumerator = arguments[1];

        while (enumerator.MoveNext())
        {
            XdmNode node = (XdmNode)enumerator.Current;

            // how can I get values from node here by using XPath expressions?
            // e.g. I want the value of the attribute "type" of the subnode "xy"
            // XPath would be something like this: "./xy/@type"

            text = text.Replace(node.NodeName.LocalName, node.StringValue);
        }

        var result = new XdmAtomicValue(text);

        return (IXdmEnumerator)result.GetEnumerator();
    }
    ...
}

中间的3条评论显示了我的问题。我想通过XPath表达式访问子节点,属性等。这是一个简化版本。稍后应将XPath作为附加参数传递。所以它不是一个固定的XPath表达式,我可以转换为代码。我真的需要一个XPath评估器。

我通过从处理器创建XPathEvaluator看到了一个解决方案。但是我现在没有处理器,或者我呢?

感谢您的帮助。

这是解决方案(感谢迈克尔):

var configuration = context.Implementation.getConfiguration();
var processor = (Processor)configuration.getProcessor();
var xpathCompiler = processor.NewXPathCompiler();

while (enumerator.MoveNext())
{
    XdmNode node = (XdmNode)enumerator.Current;

    var keyResult = xpathCompiler.Evaluate(searchXPath, node);
    var valueResult = xpathCompiler.Evaluate(replaceXPath, node);

    string key = "";
    string value = "";

    if (keyResult is XdmAtomicValue)
        key = (string)(keyResult as XdmAtomicValue).Value;
    else if (keyResult is XdmNode)
        key = (string)(keyResult as XdmNode).StringValue;

    if (valueResult is XdmAtomicValue)
        value = (string)(valueResult as XdmAtomicValue).Value;
    else if (valueResult is XdmNode)
        value = (string)(valueResult as XdmNode).StringValue;

    if (string.IsNullOrWhiteSpace(key) || value == null)
        continue;

    text = text.Replace(key, value);
}

Saxon 9.7的解决方案:

上述解决方案不再适用于Saxon 9.7。在这种情况下,我将处理器传递给扩展类,并在我注册扩展时从那里传递到扩展调用类。

public static void RegisterSaxonExtensions(Saxon.Api.Processor processor)
{
   processor.RegisterExtensionFunction(new MyExtension1(processor));
   processor.RegisterExtensionFunction(new MyExtension2(processor));
}

...

public class MyExtension1 : Saxon.Api.ExtensionFunctionDefinition
{
     private Saxon.Api.Processor processor = null;

     public MyExtension1(Saxon.Api.Processor processor)
     {
         this.processor = processor;
     }

     public override ExtensionFunctionCall MakeFunctionCall()
     {
         return new MyExtension1Call(this.processor);
     }

     ...
}

public class MyExtension1Call : Saxon.Api.ExtensionFunctionCall
{
     private Saxon.Api.Processor processor = null;

     public MyExtension1Call(Saxon.Api.Processor processor)
     {
         this.processor = processor;
     }

     public override IXdmEnumerator Call(IXdmEnumerator[] arguments, DynamicContext context)
     {
         if (arguments.Length == 2)
         {
             arguments[0].MoveNext();

             string text = (arguments[0].Current as XdmAtomicValue).Value as string;
             IXdmEnumerator enumerator = arguments[1];
             var xpathCompiler = this.processor.NewXPathCompiler();

             while (enumerator.MoveNext())
             {
                 XdmNode node = (XdmNode)enumerator.Current;

                 var keyResult = xpathCompiler.Evaluate(searchXPath, node);
                 var valueResult = xpathCompiler.Evaluate(replaceXPath, node);

                 string key = "";
                 string value = "";

                 if (keyResult is XdmAtomicValue)
                     key = (string)(keyResult as XdmAtomicValue).Value;
                 else if (keyResult is XdmNode)
                     key = (string)(keyResult as XdmNode).StringValue;

                 if (valueResult is XdmAtomicValue)
                     value = (string)(valueResult as XdmAtomicValue).Value;
                 else if (valueResult is XdmNode)
                     value = (string)(valueResult as XdmNode).StringValue;

                 if (string.IsNullOrWhiteSpace(key) || value == null)
                     continue;

                 text = text.Replace(key, value);
             }

             var result = new XdmAtomicValue(text);

             return (IXdmEnumerator)result.GetEnumerator();
         }
     }
}

1 个答案:

答案 0 :(得分:0)

DynamicContext.Implementation为您提供了一个XPathContext对象,它有一个getConfiguration()方法来访问Configuration,而Processor对象应该在Configuration.getProcessor()中找到。从那以后你应该能够创建一个XPathEvaluator。