[xsd-users] About using XPath Query
Boris Kolpackov
boris at codesynthesis.com
Mon Oct 5 03:47:28 EDT 2009
Hi,
sun-rise <sun.rise.ma at gmail.com> writes:
> I want to execute a XPath query for C++/Tree object model that
> created from scratch.
This is not possible at the moment since XPath is executed on the
underlying DOMDocument which can only be associated with the object
model during parsing (more precisely, you could create corresponding
DOM nodes and manually associate them with the object model nodes
but this is very tedious so I wouldn't recommend it).
The somewhat ugly work around would to serialize and re-parse the
object model to get the DOM association. Also, we are working on
a feature for the next release of XSD which will allow you to run
XPath/XQuery queries natively on the object model. With this feature
you will be able to do it on any object model.
> 2. Can I add a new element to XPath result of C++/Tree object model?
>
> Example
> -------
> <employee>
> <person id="001">
> <first-name>John</first-name>
> <first-name>Doe</first-name>
> <gender>male</gender>
> <age>32</age>
> <role>member</role>
> </person>
>
> <person id="002">
> ...
> </person>
> </employee>
> -------
>
> Evalate a following XPath expression for C++/Tree object.
> /employee/person[@id='001']
>
> Then, add a new <role> element to /employee/person[@id='001'].
>
> -------
> <employee>
> <person id="001">
> <first-name>John</first-name>
> <first-name>Doe</first-name>
> <gender>male</gender>
> <age>32</age>
> <role>member</role>
> <role>admin</role>
> </person>
>
> <person id="002">
> ...
> </person>
> </employee>
> -------
Yes, you can do that. You can evaluate the expression as shown in
the example and then get back from the DOM node to the corresponding
object model node (person, in this case). Then you can use the
standard object model API to add another role element. The only
limitation of this approach is that when you add another role
element, the underlying DOM document will not be automatically
updated. This means that if you try to execute another XPath
expression that need to examine this new data, it won't be able to
find anything. This shortcoming will also be addressed by the native
XPath feature.
Boris
More information about the xsd-users
mailing list