Question

I'm working on a little project for school. This project asks me to show that I'm capable of using various processing techniques of XML. Now, in my project I work with the Sedna database manager in which I keep user records and I would like to update some of these user records through XQuery (I use the PHP API to send the queries to the database through the PHP Api provided by the Sedna Developers team). Imagine I have the following database content for the user records:

<?xml version="1.0" encoding="UTF-8"?>

<users>
    <user id="admin" admin="true">
        <email>admin@admin.com</email>
        <password>123456789</password>
        <firstname>The</firstname>
        <lastname>Stig</lastname>
        <gender>male</gender>
        <subscriptions>
        </subscriptions>
    </user>
    <user id="prosper" admin="false">
        <email>prosper@localhost.com</email>
        <password>123456789</password>
        <firstname>Strange</firstname>
        <lastname>Figure</lastname>
        <gender>male</gender>
        <subscriptions>
        </subscriptions>
    </user>
</users>

Now, I my intention here is to update, for example, prosper's userrecord. In this record I would like to, for example, change his firstname and email, but keeping the rest unchanged. I have tried to use the following query, but after sending the query, it changes the document order of the userrecord for some reason I don't understand (I think it inserts the attributes nodes of the user record as childnodes). This is the query I use to update the user:

UPDATE replace $user in doc("users")//user[@id="prosper"]
with
<user>{($user/@*)}
<email>strange.figure@localhost.com</email>
{($user/node()[password])}
<firstname>Familiar</firstname>
<lastname>Figure</lastname>
<gender>male</gender>
{($user/node()[subscriptions])}</user>

Now, the problem this query gives me, is that when I try to ask the user's information after the update, I rely on the same order of child nodes have from before the update, but this query changes the order.

Is there someone skilled enough to help me with this problem?

I thank you for your time.

Was it helpful?

Solution

Jan, if you want to get password node you should change your XPath (your current expression {($user/node()[password])} is not what you think):

{$user/password}

the same true for subscriptions:

{$user/subscriptions}

It can be written as one expression with predicate:

{$user/node()[local-name(.) = ('password', 'subscriptions')]}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top