Question

How to modify all wanted attribute values in the way that is expressed in the XML examples with Xquery?

I'm using Basex 7.6 Xquery and XML database engine. My purpose so far has been only to process XML files with it.

That structure I have already:

       <address ref="file1" title="title1">
        <address ref="file1.1" title="title1.1">
          <address ref="file1.1.1" title="title1.1.1"/>
        </address>
        <address ref="file1.2" title="title1.2">
          <address ref="file1.2.1" title="title1.2.1"/>
          <address ref="file1.2.2" title="title1.2.2">
            <address ref="file1.2.2.1" title="title1.2.2.1"/>
            <address ref="file1.2.2.2" title="title1.2.2.2"/>
          </address>
        </address>
      </address>
      <address ref="file2" title="title2"/>

But I need to have like following:

       <address ref="mydir/file1" title="title1">
        <address ref="mydir/file1.1" title="title1.1">
          <address ref="mydir/file1.1.1" title="title1.1.1"/>
        </address>
        <address ref="mydir/file1.2" title="title1.2">
          <address ref="mydir/file1.2.1" title="title1.2.1"/>
          <address ref="mydir/file1.2.2" title="title1.2.2">
            <address ref="mydir/file1.2.2.1" title="title1.2.2.1"/>
            <address ref="mydir/file1.2.2.2" title="title1.2.2.2"/>
          </address>
        </address>
      </address>
      <address ref="mydir/file2" title="title2"/>

I have managed to change the first element in the way I have wanted, but still can't get it right in way the Xquery engine accepts.

I have changed the first element with this piece of code in BaseX 7.6 and I haven't figured out how to apply that to all elements.

        copy $c :=
               (<address ref="file1" title="title1">
                    <address ref="file1.1" title="title1.1">
                      <address ref="file1.1.1" title="title1.1.1"/>
                    </address>
                    <address ref="file1.2" title="title1.2">
                      <address ref="file1.2.1" title="title1.2.1"/>
                      <address ref="file1.2.2" title="title1.2.2">
                        <address ref="file1.2.2.1" title="title1.2.2.1"/>
                        <address ref="file1.2.2.2" title="title1.2.2.2"/>
                      </address>
                    </address>
                  </address>
              (:<address ref="file2" title="title2"/> WHY IS THIS NOT BY THE WAY ACCEPTED?:)

                )
                modify (
                  replace value of node $c/@ref with concat('mydir/', $c/@ref)
                )
                return $c

Info about the way I have tried to figure this problem can be found here: http://docs.basex.org/wiki/XQuery_Update#Non-Updating_Expressions

I have feeling that BaseX is good tool for doing this. I just can't figure out how to do that. I think recursion somehow is also answer in this case. Still I can't figure it out because of my little inexperience.

Thanks in advance!

Was it helpful?

Solution

This will prepend mydir/ to each ref attribute in your XML data

  copy $c := (<root>
      <address ref="file1" title="title1">
        <address ref="file1.1" title="title1.1">
          <address ref="file1.1.1" title="title1.1.1"/>
        </address>
        <address ref="file1.2" title="title1.2">
          <address ref="file1.2.1" title="title1.2.1"/>
          <address ref="file1.2.2" title="title1.2.2">
            <address ref="file1.2.2.1" title="title1.2.2.1"/>
            <address ref="file1.2.2.2" title="title1.2.2.2"/>
          </address>
        </address>
      </address>
      <address ref="file2" title="title2"/>
      </root>)
  modify (
    for $e in $c//@ref
    return replace value of node $e with concat('mydir/', $e)
  )
  return $c

The second address node does not work because in XML you have to have exactly one root node. If you like you can use a sequence, i.e. (<address />, <address2 />)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top