【问题标题】:orbeon: applying xslt transform in xbl on bound documentorbeon:在绑定文档的 xbl 中应用 xslt 变换
【发布时间】:2011-10-17 20:04:06
【问题描述】:

在 Orbeon Forms 中,我需要创建一个组件(使用 XBL),当绑定到类似的实例时

<OCinstructionitem>
  <OCp>paragraph 1</OCp>
  <OCp>paragraph 2 <OCem>with italics part</OCem> rest of paragraph 2 </OCp>
</OCinstructionitem>

像这样创建一个可编辑的 div:

<div contentEditable="true">
  <p>paragraph 1</p>
  <p>paragraph 2 <i>with italics part</i> rest of paragraph 2 </p>
</div>

我的想法是我需要使用 XSLT 来执行此操作。当要转换的 XML 在 xforms 文档中时,我得到了这个工作:

<oc:instructionitem>
   <OCinstructionitem>
      <!-- here the xml of above -->
      ...
   </OCinstructionitem>
</oc:instructionitem> 

但我想让 XSLT 在绑定节点上运行,如下所示:

<oc:instructionitem ref="OCinstructionitem"/>

但是,我无法从 XSLT 访问绑定节点。

我的问题:这不可能吗?还是我必须修改我的 XBL?

我的 XBL 代码:

<xbl:binding id="oc-instructionitem" element="oc|instructionitem">
    <xbl:template xxbl:transform="oxf:xslt">
        <xsl:transform version="2.0">
              <xsl:template match="@*|node()" priority="-100">
                   <xsl:apply-templates select="@*|node()"/>
              </xsl:template>
              <xsl:template match="text()" priority="-100" mode="in-paragraph">
                  <xsl:copy>
                     <xsl:value-of select="."/>
                  </xsl:copy>
              </xsl:template>
              <xsl:template match="OCem" mode="in-paragraph">
                  <x:i><xsl:value-of select="."/></x:i>
              </xsl:template>
              <xsl:template match="OCp">
                  <x:div>
                      <xsl:apply-templates mode="in-paragraph"/>
                  </x:div>
              </xsl:template>
              <xsl:template match="/*">
                <x:div contentEditable="true">
                    <xsl:apply-templates/>
                </x:div>
            </xsl:template>
    </xsl:transform>
    </xbl:template>
</xbl:binding>

</xbl:xbl>

任何帮助将不胜感激,

编辑:在 tohuwawohu 的有用评论后(下) 您似乎需要定义一个绑定到实例数据的变量。像这样:

<xsl:template match="oc:instructionitem">
   <xforms:group xbl:attr="model context ref bind" xxbl:scope="outer">
      <xxforms:variable name="binding" as="node()?" xxbl:scope="inner" >
         <xxforms:sequence select="." xxbl:scope="outer"/>
      </xxforms:variable>
      <xforms:group xxbl:scope="inner">
         <!-- Variable pointing to external single-node binding -->
         <xforms:group ref="$binding">
              <xsl:call-template name="main"/>
         </xforms:group>
      </xforms:group>
   </xforms:group>
</xsl:template>
<xsl:template name="main">
    <x:div contentEditable="true">
       <xforms:repeat nodeset="*">
           <xsl:call-template name="paragraph"/>
       </xforms:repeat>
    </x:div>
</xsl:template>

但是,XSLT 元素仍然不能作用于数据。它只能生成 XFORMS 元素,这些元素可以作用于数据。这意味着永远不会选择像

马丁

【问题讨论】:

    标签: xslt xforms orbeon xbl


    【解决方案1】:

    我没有测试它,但我认为通过打破封装应该是可能的。如果没有这个,您的 XBL 将只能访问绑定节点的内容,就像在您的第三个代码 sn-p 中一样。如果您想让 XBL 访问 XForms 实例数据,您必须声明一个variable inside the XBL that's pointing to the single-node binding of the bound node

    这是来自 Wiki 示例的代码 sn-p:

    <xforms:group xbl:attr="model context ref bind" xxbl:scope="outer">
        <xforms:group xxbl:scope="inner">
            <!-- Variable pointing to external single-node binding -->
            <xxforms:variable name="binding" as="node()?">
                <xxforms:sequence select="." xxbl:scope="outer"/>
            </xxforms:variable>
            ...
        </xforms:group>
    </xforms:group>
    

    已经像这样定义了单节点绑定,应该可以引用该变量并将其用于 xslt 转换。只需替换匹配根节点的XSLT模板元素,并将其指向变量$binding的内容即可:

    <xsl:transform version="2.0">
        <xsl:template match="@*|node()" priority="-100">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:template>
        <xsl:template match="text()" priority="-100" mode="in-paragraph">
            <xsl:copy>
                <xsl:value-of select="."/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="OCem" mode="in-paragraph">
            <x:i><xsl:value-of select="."/></x:i>
        </xsl:template>
        <xsl:template match="OCp">
            <x:div>
                <xsl:apply-templates mode="in-paragraph"/>
            </x:div>
        </xsl:template>
        <xsl:template match="oc:instructionitem">
            <xforms:group ref="$binding">
              <x:div contentEditable="true">
                  <xsl:apply-templates/>
              </x:div>
            </xforms:group>
        </xsl:template>
    

    希望这对你有用。如果绑定节点(在您的示例中:oc:instructionitem)不为空,则可能需要做出规定,因此 xslt 可以同时处理该内容以及 $binding 变量的内容。

    【讨论】:

    • 感谢您的回答。但是,我仍然无法对来自 XSLT 的数据采取行动。变量 $binding 仅在 xforms 元素中可见,在 xslt 元素中不可见。
    • @Martijn:它是否适用于您的修改(使用xforms:group)?如果没有,我最好删除我的答案。
    猜你喜欢
    • 2021-09-19
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2012-03-11
    • 1970-01-01
    相关资源
    最近更新 更多