Skip to content

如何在 keys 配置文件中定义 operation 元素相关的检索点 #1200

@DigitalPlatform

Description

@DigitalPlatform

数据库的 keys 配置文件负责定义检索点。

对于一些复杂的,无法用普通 XPATH 语句取出数据内容的 XML 结构,可以利用 key 配置文件中内嵌的 C# 脚本来实现数据内容取出的功能。

下面举例说明:

  1. 在 keys 配置文件中找到 script 元素,在里面适当位置增添一个函数:
	public void getMoveToMyselfOperator()
	{
		var nodes = this.DataDom.SelectNodes("//operations/operation[@name='move']");

                var operation_nodes = dp2StringUtil.MatchTargetRecPath(nodes, this.RecPath);
		var lines = dp2StringUtil.GetOperators(operation_nodes);

		if (lines.Count > 1)
		{
			this.ResultStrings = lines;
		}
		else if (lines.Count == 1)
			this.ResultString = lines[0];
	}

注意在脚本前部的 using 区域内,不要忘了添加一行:
(这是为了定义代码中用到的 dp2StringUtil 实用类的名字空间)

using DigitalPlatform.Text;

函数中,先通过 SelectNodes() 选出 name 属性符合要求的 operation 元素。这一步,得到的元素集合 nodes 中,包括各种源记录路径和目标记录路径的 operation 元素。
然后,对 nodes 集合进行进一步的筛选,通过 dp2StringUtil.MathTargetRecPath() 函数,把符合目标记录路径要求的 operation 元素筛选出来,放入 operation_nodes 集合中。注意,this.RecPath 是一个环境变量,内容为当前这一条记录的路径。
最后,用 dp2StringUtil.GetOperators() 函数进一步把 operation_nodes 这个元素集合中的 operator 属性内容提取出来,放到 lines 字符串集合中。注意,根据数据记录的实际情况,lines 集合中可能为零个、一个或者多个元素。
在返回给宿主的时候,要根据这个字符串集合的元素数量区分一下,只有一个元素,就返回给 this.ResultString;多个元素,就返回给 this.ResultStrings。注意最后一个 s 字母的差异,两个变量是不同的变量。(注: 样例代码这里主要是示范多样性用法,其实不管一个还是多个字符串的集合,都直接赋值给 this.ResultStrings 这个变量也是完全可以的)

  1. 在 keys 配置文件中找到适当位置,插入 key 和 table 元素,片段如下:
	<key>
		<xpath scripting="on">getMoveToMyselfOperator</xpath>
		<from>moveoperator</from>
		<table ref="moveoperator" />
	</key>
	<table name="moveoperator" id="300" type="moveoperator">
		<convert>
			<string style="" />
		</convert>
		<convertquery>
			<string style="" />
		</convertquery>
		<caption lang="zh-CN">最近移动操作者</caption>
		<caption lang="en">Recently Move Operator</caption>
	</table>

可以看出,key/xpath 元素不像平时那样定义 XPATH,而是添加了一个 scripting="on" 元素,那么 path 元素中的文本就是一个函数名,函数通过上一步骤已经定义在 script 元素内的 C# 代码中了。

思考

上面的示范代码中,可能会得到一个或者多个 operation 元素的 operator 属性,也就是说同一条记录会创建“最近移动操作者”的多个检索点 key。

如果希望最多只创建一个检索点 key 要怎么做呢?可以在得到 lines 以后,只取最后一个元素,放入 this.ResultString 环境变量,就可以达到目的。这就是真正的“最近移动操作者”。

(注: 数据记录中 operations 元素下的多个 operation 元素,较新的会靠后创建)

扩展

根据实际业务需要,如果发现 dp2StringUtil 实用类里面的函数不够用,可以反馈给我们,我们进行适当扩展。也可以在 keys 里面的 C# 脚本中直接书写代码实现具体 XML 操作功能,而不用 dp2 的函数库。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions