سؤال

Following is the XML structure -

<Documents>
 <Doc n="Name1" year="2010">
 .....
 </Doc>
 <Doc n="Name2" year="2010">
 .....
 </Doc>
 <Doc n="Name3" year="2011">
 .....
 </Doc>
 <Doc n="Name4" year="2011">
 .....
 </Doc>

I am using BaseX to store this document. I need to extract some data from this and store it as a xml file. Say, I need a separate xml file for years.

How to get this done ?

هل كانت مفيدة؟

المحلول

As you are using BaseX, please visit this link -

http://docs.basex.org/wiki/File_Module#file:append

for $x in doc('DBNAME')//Doc
  where $x/@year eq '2010'
  return file:append("C:\2010.xml", $x)

This query will extract the required data in file named 2010.xml in C drive. File name and location can be changed as per you requirement.

Using above query you will have change the year value 2010 as required. If you have year with different values and in order, as I see from the structure, you can use following query. This query will create multiple files, for all the years.

for $year in (2010 to 2011)
  (: Year are specified :)
  let $fName := concat("C:\", $year, "B.xml")
  (: The path is created and stored in $fName variable :)
  for $x in doc('docs')//Doc
    where $x/@year eq xs:string($year)
    return file:append($fName, $x)

I hope both the examples are quite self explanatory.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top