如何在最后不需要 xforms:delete 的情况下执行 xforms:insert?

How to do an xforms:insert without the need of xforms:delete at the end?

<xf:action ev:event="xforms-model-construct">
    <xf:insert nodeset="instance('subInstance')/type" origin="instance('defaultType')/type"/>
</xf:action>

我想根据另一个实例填充一个实例。我可以使用 xf:insert 来做到这一点,如上所示。

但是,我意识到实例 'subInstance' 在启动 xf:insert 之前必须包含一个空类型元素。

<subInstance>
  <type/>
</subInstance>

所以在所有 xf:insert 之后,我需要执行以下操作来删除第一个空的:

<xf:delete nodeset="instance('subInstance')/type" at="1" />

这个方法有什么问题吗?或者有什么方法可以直接插入而不用初始为空?

两个答案:

你真的不需要初始类型元素

您的原始实例可以简单地是:

<subInstance/>

然后您可以将 插入 subInstance 元素中:

<xf:action ev:event="xforms-model-construct">
    <xf:insert
        context="instance('subInstance')"
        origin="instance('defaultType')/type""/>
</xf:action>

Using context without nodeset or ref表示你想插入 context指向的节点.

在 XForms 2.0 的支持下,您仍然可以做您想做的事

如果你想保留原来的嵌套 type 元素,你可以这样写:

<xf:action ev:event="xforms-model-construct">
    <xf:insert
        nodeset="instance('subInstance')"
        origin="
            xf:element(
                'subInstance',
                instance('defaultType')/type
            )
        "/>
</xf:action>
  1. 通过定位目标实例的根元素,整个实例将被替换。 XForms 1.1 已经是这种情况。
  2. 通过 origin 属性使用 XForms 2.0 中的 xf:element() 函数,您可以动态创建一个 XML 文档,该文档以 subInstance 为根并且仅包含 [=来自 defaultType 个实例的 19=] 个元素。

为了使其更加现代,您可以将 nodeset 替换为 ref,因为 nodeset 在 XForms 2.0 中已弃用:

<xf:action ev:event="xforms-model-construct">
    <xf:insert
        ref="instance('subInstance')"
        origin="
            xf:element(
                'subInstance',
                instance('defaultType')/type
            )
        "/>
</xf:action>