I am creating an Xform that reads an XML document and creates an input form for updating the document. However, apparently due to a namespace issue none of my Xpath expressions resolve.My form works fine on a simple instance when the instance fi开发者_JAVA技巧le has no namespace. However, I need the namespace support.
My instance file has a namespace "ai:inventory."
I am referencing the instance data
Where should I be declaring the prefix "ai" for my namespace so that my XPath expressions can find the appropriate elements? /ai:inventory/products ?
I've tried creating the prefix in the html opening tag... that didn't help.
thanks,
An XForms instance is an XML document in its own right, so you should include appropriate namespace declarations for every instance in your form:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/2002/xforms" xmlns:ai="http://example.com/">
<head>
<xf:model>
<xf:instance>
<ai:inventory xmlns="" xmlns:ai="http://example.com/">
<product>foo</product>
</ai:inventory>
</xf:instance>
</xf:model>
</head>
<body>
<xf:output ref="/ai:inventory/product"/>
</body>
</html>
There are some processors that will evaluate the XPath correctly if your instance is inline (rather than an external resource), but I wouldn't recommend relying on that behaviour.
Generally namespace declaration applies to current element and all descending elements. If you need to use the same namespace both in model and view, then html tag is a good choice. Something like this:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/2002/xforms" xmlns:ai="yournamespace">
<head>
<xf:model>
<xf:instance xmlns="">
<ai:inventory>
<product>Hello</product>
</ai:inventory>
</xf:instance>
</xf:model>
</head>
<body>
<xf:output ref="/ai:inventory/product"/>
</body>
</html>
Keep in mind that if you use non-namespaced elements in instance and also use XHTML as your default namespace, then you need to redeclare default namespace with xmlns="", as in example.
Both answers were corrected. However, I found that my issue was related to using the xsltforms xsl stylesheet to render my xform. when i switched to orbeon, everything worked fine.
精彩评论