Jump to content
Nepomuk

Manually loading XML data into XForms

Recommended Posts

Hi everyone!I'm new both to this forum as to XForms, but I've been working on a project with it and got stuck. Yes, I know, XForms isn't supported natively by most browsers and so on, but I'd like to use it anyway if possible.So, here's my problem: My project uses XForms and eXist, so I have an XML file as a database. My XForms document looks something like this:

...   <xf:model version="1.0">	<xf:instance id="data-instance" src="data.xml" xmlns="">	 <Data>	  <FirstName/>	  <LastName/>	 </Data>	</xf:instance>	<xf:submission id="read-from-file" method="get" action="http://localhost:8080/exist/rest/db/eXist_test/data.xml" replace="instance" instance="data-instance"/>	<xf:submission id="save-to-file" method="put" action="http://localhost:8080/exist/rest/db/eXist_test/data.xml" replace="instance" instance="data-instance"/>   </xf:model> ...   <xf:input ref="/Data/FirstName"><xf:label>First Name: </xf:label></xf:input><br/>   <xf:input ref="/Data/LastName"><xf:label>Last Name: </xf:label></xf:input><br/>   <xf:submit submission="read-from-file"><xf:label>Load</xf:label></xf:submit>   <xf:submit submission="save-to-file"><xf:label>Save</xf:label></xf:submit> ...

and my XML file looks like this:

<Data> <FirstName>Peter</FirstName> <LastName>Parker</LastName></Data>

Now, when the document loads, it loads the values from the XML file into the Document, ditto when I click on "Load" and when I click on "Save" it submits the changes to my XML file. So far so good - but now I want to be able to add new tags to the XML file via the webinterface (e.g. <MiddleName>). When I have further paths in my XForms document, they just won't display, unless they belong to a different instance (which isn't a choice I guess, because it will only submit the data of one instance when I save).My idea was therefore to cut out the src="data.xml" part in the instance description and just load the entries manually. Also, the read-from-file submission would have to be changed. However, I can't seem to be able to find anything like that online. Any ideas or suggestions, how do do this differently?Thanks,NepomukEDIT:I just had another thought, maybe this could work, though I have no idea how to implement it:Let's assume, when I want to add a new tag, I click on an "Add" button. What happens then, is that a different instance is selected for saving and this instance gets a copy of the data from the original instance plus the new stuff that's supposed to be added. So, the new question would be: How do I copy data from one instance to another?Greetings,Nepomuk

Edited by Nepomuk

Share this post


Link to post
Share on other sites

you should probably have a moderator ask you to move this thread the XML forums.

Share this post


Link to post
Share on other sites
you should probably have a moderator ask you to move this thread the XML forums.
Well, I searched for XForms and got results in both the XHTML and the XML forum... so as XForms is something used in XHTML, while XML is just (for lack of a better description) the "general idea behind it", I thought this would be the better choice... But if you think, I'll get better results over there, sure, I'll do that.Greetings,Nepomuk

Share this post


Link to post
Share on other sites

I think this may be one of the few cases with XForms which would require script intervention... you need something (in a pure client side implementation, that would be JavaScript, but I'm not sure how eXist operates) that can access the XML tree, and modify the model. Once the model change is made, you can make any control to alter the value of the element.If there's a way to do this without script, I'm not aware of it, due to lack of practical experience with XForms.<topic moved to the XML forum>[edit]What if you remove the instance model altogether, and just use the file, like:

<xf:instance id="data-instance" src="data.xml" />

?[/edit]

Share this post


Link to post
Share on other sites
I think this may be one of the few cases with XForms which would require script intervention... you need something (in a pure client side implementation, that would be JavaScript, but I'm not sure how eXist operates) that can access the XML tree, and modify the model. Once the model change is made, you can make any control to alter the value of the element.
Hm, well I have little experience with scripting, although I'm sure eXist is capable of that. If anyone has experience with that, I'd be glad to get a few tips.
What if you remove the instance model altogether, and just use the file, like:
<xf:instance id="data-instance" src="data.xml" />

?

Hm, trying it now (eXist is being a bit difficult atm, but that's a different problem), but as far as I remember, it just shows up nothing in that case. Also, that won't allow me to add new tags, will it?Greetings,Nepomuk

Share this post


Link to post
Share on other sites

If you could use JavaScript, the script for adding the tag would go something like:

document.getElementsByTagName('Data')[0].appendChild(document.createElement('MiddleName'));

The "leave only the src" was an idea out of nowhere, but I think I remembered why - doing that, in combination with providing a schema, means you allow whatever the schema allows to be written in the file.Then, to modify the allowed fields, you need to modify the schema instead, and you can modify it by loading it in a separate XForm, or just a separate insteance of the same form... then again, adding an element requires adding an element in the schema so...But wait... why do you want to add arbitrary data anyway? The whole idea is to limit yourself to what you allow, not allow arbitrary allow anything. If you need arbitrary name/value pairs, make your data model to allow a sequence of such pairs, and use that.

Share this post


Link to post
Share on other sites

OK, I tried leaving the model out and the output is the same - it still shows the input fields and the entries from the XML file. However, if I manually add an input for MiddleName like this:

<xf:input ref="/Data/MiddleName"><xf:label>Middle name: </xf:label></xf:input>

and don't change the XML file, it won't show the input box. (It will, if I add a <MiddleName/> tag to the XML file.) So, even if the JavaScript you gave me would work, it's something to try at a later stage, I guess...

But wait... why do you want to add arbitrary data anyway? The whole idea is to limit yourself to what you allow, not allow arbitrary allow anything. If you need arbitrary name/value pairs, make your data model to allow a sequence of such pairs, and use that.
I'm not quite sure, I understand what you're saying... The reason I want to do this is because I want to be able to edit the XML file in a limited way, but that includes adding some tags. In this case, I might have realised that there's more personal information I want to give and I'd like to be able to do that via the web interface instead of by editing the XML- and the XHTML-file.Greetings,Nepomuk Edited by Nepomuk

Share this post


Link to post
Share on other sites

In that case, you could create a schema that puts the optional data as an optional element.I'm not completely sure how an XForms client would operate with such a schema, but I would expect it to only create the element if some data for it was entered.In fact, what W3C's XForms tutorial suggests is that all form fields are optional by default, and you need to specify an XPath expression to determine if the data is required.

Share this post


Link to post
Share on other sites

As I said, I'm pretty new to XForms, so I don't know, how to create such a schema. Could you give me an example or a link, which explains that? However, I'm not sure if a schema would solve that, because basically I either have to

  1. be able to add a new tag or copy and modify an existing one in the model or
  2. make inputs show up, even if there is no tag for them in the model

and from my brief experience with XML schemas a while back I seem to remember that these only allow defining a general structure, not modifying an existing one. So, even though I could define <MiddleName> as an optional entry, I may want to add <Age> at some point, which wouldn't be in the structure. And I'm not aware of a possibility to create Meta-tags... But as I said, it's been quite a while. Of course, I could also be completely missing the point here.Oh, and the link about the XForms tutorial and required controls only applies to submissions, as far as I understand it - stuff like "Please give a name, otherwise I won't continue." As far as I know, this doesn't apply to whether an element is visible or not - or am I missing something?Greetings,Nepomuk

Share this post


Link to post
Share on other sites

Again, the whole idea of a form is to let users enter data that you know how to process. You don't currently support "Age" (i.e. if you had such a field, your app won't know what to do with it), but if in the future you decide to add it, you modify the form to have it, either as a required or an optional field AND make the rest of your application treat that field properly.The idea of an XML schema is to define the allowed data (i.e. the allowed kinds of XML documents) once, so that both the client and the server could reference the schema to make sure the data is valid. In traditional HTML forms, you need to do validation of the name/value pairs on the server, and you may do validation on the client with JavaScript by analyzing the actual form field's value. XForms clients are supposed to come with XML Schema validators, so that they validate the generated XML document against it, and if valid, pass it to the server, which will use the same schema to validate it once more (in the case the client doesn't support validation or is purposefully trying to fool the server into accepting a document it's not supposed to accept).The W3Schools' tutorial on XML Schema has a section on indicators. There you'll see the minOccurs and maxOccurs attribute, with which you can define that an element can have from n to m occurances in the XML. In your case, that would be the XML generated by the XForms client.[edit]Just had an idea... you can create a separate form that accept an arbitrary name/value pair sequence of elements... like, you define you accept a <name> and a <value> element that can occur 1 or more times. Once your server receives that, it can process the XML Schema and/or XForm to add the field you specified as "name", and add whatever constrains are in "value".... It will do that with some server side scripting of course... well, that's just a basic outline (implementing it will probably be a little more complicated), but I think you get the idea.[/edit]

Share this post


Link to post
Share on other sites

OK, I'll have a look at that tutorial and see what I can do with it. Though from your description it seems to me like that will let me define how many <Name> tags I can have in the document, for example.If I'm understanding this correctly, the way to do this may be to have a sort meta-tag with attributes in my XML file, e.g.

<Data>  <option name="FirstName" value="Peter"/>  <option name="LastName" value="Parker"/></Data>

If so, how can I access/use such attributes in XForms? I'm guessing, I would have to change the ref-attribute in the input tags somehow? This solution, if it works, would allow the XML document to be valid compared to a schema (although in that case, I'm not sure I'd even need a schema), right?The support isn't so much of a problem in this case, because in the long run, the XForms document should be created by the XML document via XSLT, so as soon as I add a new tag, I automatically add a way of working with it in the Form. So, what I'm planing is:

   XML ------XSLT-----> XForms   /|\					|	|________eXist________|

I'm beginning to think, that this may be a impossible or at least highly complicated, but I'd still like to try... After all, you can't learn if you don't try. ^^Greetings,Nepomuk

Share this post


Link to post
Share on other sites

You generate the form based on the contents of the XML? Wow, that's... interesting... it's sort of an approach that Microsoft's InfoPath takes, only on the next level (abstraction wise).

Share this post


Link to post
Share on other sites

Well, that's the sort of thing XSLT was intended for, is it not? :-DMind you, I don't do that yet - I have done some successful tests in that direction, but right now I want to be able to get the XForms -> XML problem dealt with, before I combine everything.I did some more research in the are of XPath and found out about the use of @ - so if I'd change my XML file like above and set my XForm input references so something like "/Data/option@Name" that should work, right? (I'm about to test it, so there's no need to say something like "What happens when you try it?" ^^)Still, I'd have to modify the XPath if I want to create a new tag in my XML file, right? How do I do that?Greetings,Nepomuk[edit]Ah, just realised my mistake... the @ may be part of the solution, but I'd need to compare the value of that attribute somehow... so for example, it would be something like

<input ref="/Data/option@name == FirstName">...</input>

I know, that that code won't work, but I think it explains what I want to do... Any ideas?[/edit]

Edited by Nepomuk

Share this post


Link to post
Share on other sites

Yes and no.... XSLT is intended to convert one XML dialect to another... but to construct an editing infrastructure around a dialect? That's possible I guess, but it's an esoteric and fragile approach.I still think the way you should edit the form's model is with a separate "edit the form" form... see my second last post... the edit part.Regardless of whether you use elements or attributes, altering the model must require separate intervention, so that the data and the application are in sync.To target attributes in XPath, you still need a slash. And the rest of your syntax is actually wrong too. Here's a correct example:

/Data/option/@name = 'FirstName'

Share this post


Link to post
Share on other sites

Well, XForms is a XML dialect, isn't it? ^^ And it may be an unusual approach, but then XForms isn't used much anyway, so any approach could be seen as "esoteric" :-D And about the "fragile" part... Why so? What makes it fragile and what results could that have?About your suggestion with the separate form - do I understand you correctly, that I should create a second form, which sends a request to eXist, asking eXist modify the original XML file and XForms document? That would be a possibility I guess, although I would prefer to have it in a separate instance but in the same document rather than completely separate, if that's possible. Hm, I'll think about it...Oh, and thanks for the syntax correction, that looks promising. I'm getting an error at the moment, but that may just be a mistake I made when changing the document... I'll check that tomorrow and get back to you.Greetings,Nepomuk

Share this post


Link to post
Share on other sites
And about the "fragile" part... Why so? What makes it fragile and what results could that have?
It's very easy to transform something when you're aware of it's form (or "grammer" if you will). As soon as you try to transform something for which the form is not clear, you either end up making assumptions about the grammar based on the current contents (which will ALWAYS be the first thing you need to change later on), or you end up creating something closely resembling a text editor, eliminating the whole initial idea.
About your suggestion with the separate form - do I understand you correctly, that I should create a second form, which sends a request to eXist, asking eXist modify the original XML file and XForms document? That would be a possibility I guess, although I would prefer to have it in a separate instance but in the same document rather than completely separate, if that's possible. Hm, I'll think about it...
Seriously... how often do you plan to do modifications on your model? If it's too often, the "model" is not a model to begin with. You need to switch to something which will work at its state. If you plan to rarely do modifications, what's the problem with having a separate editor? Why bother the user with something (s)he won't use most of the time?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...