Orbeon Form Builder动态下拉框关联其他字段问题排查
Let's break down why your item[@group=context()/../pet] expression isn't working and how to fix it:
The Core Issue
When you use <fr:databound-select1> with an external resource (like your http://192.168.0.80/petnames.xml), the XPath context inside the <xf:itemset> is tied to the external XML document, not your form's main instance. That means context()/../pet is looking for a pet node within the external XML (which doesn't exist), instead of referencing the pet field in your form.
The Fix: Explicitly Reference the Form Instance
You need to directly point to the pet value in your form's main instance using the instance ID. Replace your itemset's ref attribute with this:
<xf:itemset ref="/root/item[@group=instance('fr-form-instance')/section-1/pet]">
Why This Works
instance('fr-form-instance')explicitly targets your form's main data instance./section-1/petnavigates to the exactpetfield you want to use for filtering.- The external XML's
itemnodes are filtered where theirgroupattribute matches the selectedpetvalue from your form.
Additional Tips for Reliability
- Ensure Automatic Refresh: To make sure the dropdown updates immediately when the
petselection changes, confirm your form's bindings are set to refresh dependent controls. The explicit instance reference should already trigger updates in most cases, but if you run into delays, you can add anxxf:refreshevent trigger tied to thepetfield's change event. - Test with Internal Instance First (If Possible): If you ever can use your internal
petnamesinstance instead of the external XML, the expression would simplify to:
This works because the context here would be within your form's instance, so<xf:itemset ref="instance('petnames')/root/item[@group=../pet]">../petcorrectly references the sibling field.
Updated Code Snippet for the Dropdown
Here's how your corrected <fr:databound-select1> should look:
<fr:databound-select1 xmlns="http://orbeon.org/oxf/xml/form-builder" xmlns:xxbl="http://orbeon.org/oxf/xml/xbl" id="mypetname-control" resource="http://192.168.0.80/petnames.xml" bind="mypetname-bind"> <xf:label ref="$form-resources/mypetname/label"/> <xf:hint ref="$form-resources/mypetname/hint"/> <xf:alert ref="$fr-resources/detail/labels/alert"/> <xf:itemset ref="/root/item[@group=instance('fr-form-instance')/section-1/pet]"> <xf:label ref="@value"/> <xf:value ref="@value"/> </xf:itemset> </fr:databound-select1>
This should correctly filter the pet names based on the selected pet value in your form.
内容的提问来源于stack exchange,提问作者Peter Gruenbeck

