inserting xml nodes based upon an expression
-
Hi folks, I bind textboxes to nodes of an XmlDocument and refer to these nodes via an xpath statement. Updating existing content is really easy this way. My problems arise, when I try my xpath statement does not find the node in question and subsequentially I want to create it. Say, my xpath is "/def:Version/def:Entry[lang('en')]/Title" and the second element does not exist. At first I thought, I'd split my path into pieces, ininerate it backwards, reach the first existing node and then recreate the requested structure. This could work with very easy queries, but something like "def:Entry[lang('en')]" already would require my program to "understand" that it needs to create a from the xPath lang() function. I would end up writing a strange kind of xpath parser. And the idea isn't so good either, because I'd have to make my xpath super verbose, so that no elements (attributes!) are left out (and therefore could be created at need), which doubtlessly would decrease performance. I stumbled across a xupdate implementation at http://www.openvue.net/projects.php?menu=4 It's nunit testcases don't work 100% so I would have to invest some more time into this. That's why I wanted to ask you guys, what do you think: Xupdate is the way to go for me, or do you have any other good ideas? Thanks in advance, Jan
-
Hi folks, I bind textboxes to nodes of an XmlDocument and refer to these nodes via an xpath statement. Updating existing content is really easy this way. My problems arise, when I try my xpath statement does not find the node in question and subsequentially I want to create it. Say, my xpath is "/def:Version/def:Entry[lang('en')]/Title" and the second element does not exist. At first I thought, I'd split my path into pieces, ininerate it backwards, reach the first existing node and then recreate the requested structure. This could work with very easy queries, but something like "def:Entry[lang('en')]" already would require my program to "understand" that it needs to create a from the xPath lang() function. I would end up writing a strange kind of xpath parser. And the idea isn't so good either, because I'd have to make my xpath super verbose, so that no elements (attributes!) are left out (and therefore could be created at need), which doubtlessly would decrease performance. I stumbled across a xupdate implementation at http://www.openvue.net/projects.php?menu=4 It's nunit testcases don't work 100% so I would have to invest some more time into this. That's why I wanted to ask you guys, what do you think: Xupdate is the way to go for me, or do you have any other good ideas? Thanks in advance, Jan
I am confused on what you are attempting to do. I use a key to find nodes that need updating. If a key is not present, I find nodes via its parents or search for nodes that are missing certain child nodes or attributes. You may need to just determine if the structure of your XML document is right for what you are trying to do.
-
I am confused on what you are attempting to do. I use a key to find nodes that need updating. If a key is not present, I find nodes via its parents or search for nodes that are missing certain child nodes or attributes. You may need to just determine if the structure of your XML document is right for what you are trying to do.
Imagine the following xml: xxx yyy zzz I query: /foo/bar[action='1']/element I get "xxx" and I can update it. Good. I query: /foo/bar[action='4']/element Now, this node does not exist, so it should be created (and it's predecessor bar with the attribute action=4). foo exists, so nothing needs to be done for it. Same aplies for /foo/bar[4]/element, just the attribute has to come from somewhere else (I can provide for this). /foo/bar[5]/element should fail This is not for a special xml file, but a generic solution, so it does not depend on any certain structure... Does this make things a bit clearer? -- modified at 16:53 Thursday 21st September, 2006
-
Imagine the following xml: xxx yyy zzz I query: /foo/bar[action='1']/element I get "xxx" and I can update it. Good. I query: /foo/bar[action='4']/element Now, this node does not exist, so it should be created (and it's predecessor bar with the attribute action=4). foo exists, so nothing needs to be done for it. Same aplies for /foo/bar[4]/element, just the attribute has to come from somewhere else (I can provide for this). /foo/bar[5]/element should fail This is not for a special xml file, but a generic solution, so it does not depend on any certain structure... Does this make things a bit clearer? -- modified at 16:53 Thursday 21st September, 2006
I cannot see your XML. Use "Ignore HTML tags in this message ...".
-
I cannot see your XML. Use "Ignore HTML tags in this message ...".
this should be better, now
-
Imagine the following xml: xxx yyy zzz I query: /foo/bar[action='1']/element I get "xxx" and I can update it. Good. I query: /foo/bar[action='4']/element Now, this node does not exist, so it should be created (and it's predecessor bar with the attribute action=4). foo exists, so nothing needs to be done for it. Same aplies for /foo/bar[4]/element, just the attribute has to come from somewhere else (I can provide for this). /foo/bar[5]/element should fail This is not for a special xml file, but a generic solution, so it does not depend on any certain structure... Does this make things a bit clearer? -- modified at 16:53 Thursday 21st September, 2006
:confused: I lose it right after "Now, this node does not exist, so it should be created". If you are trying to write generic code to act on any XML structure, you probably have to determine its schema beforehand.
-
:confused: I lose it right after "Now, this node does not exist, so it should be created". If you are trying to write generic code to act on any XML structure, you probably have to determine its schema beforehand.
Probably, looking up the schema is a good idea and I am not at all against it. The problem is, that mingling xpath with schema to create missing nodes via dom is something extremely complicated. I wouldn't even know where to start. That's why I thought, that XUpdate would help me there, as it is an developed language for more or less this purpose. At least I am thinking it is. I just looked it up a few days ago and haven't found anything better since. But I have no idea, if XUpdate does not have any pitfalls I am not aware of, if the implementation, I found, is ok and most importantly, if there isn't some other, more straight forward way. As for explaining my somewhat confusing (I admit) text: I wrote a provider that allows me to update and delete nodes via xpath. That's something quite easy. Now I needed to insert nodes as well and as the xpath was something I already had in my classes, I tried to implement the insert as well via xpath. I failed at that and I am pretty sure that xpath just is not the right expression for the job. Even though others at cp have tried something quite similar: http://www.codeproject.com/csharp/xpathstore.asp So my examples have to be read with that bias: to use an xpath as an more or less equivalent to an sql insert statement.
I query: /foo/bar[action='4']/element Now, this node does not exist, so it should be created (and it's predecessor bar with the attribute action=4). foo exists, so nothing needs to be done for it. Same aplies for /foo/bar[4]/element, just the attribute has to come from somewhere else (I can provide for this). /foo/bar[5]/element should fail (because there is no element bar[4] for it to be inserted after)
Thanks for sharing your thoughts! Jan