如何使用eXist数据库中使用的XQuery文件将CSV数据添加到已有的XML [英] How to add CSV data to already available XML using XQuery file used in eXist Database

查看:169
本文介绍了如何使用eXist数据库中使用的XQuery文件将CSV数据添加到已有的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用eXist数据库,我有一个新的想法,我必须使用 XQuery 实现XML文件。



将CSV文件转换为XML (已在数据库集合中)。此XML文件仅包含必需的标记和信息。



XML如下:,其名称为 createXML.xml'

 <?xml version =1.0encoding =UTF-8?& 
< records>
//来自CSV文件的所有记录想要放在这里.....之间的记录标签
< / records>

现在的CSV文件如下:

 名称,主题,标记//标题行
krunal,maths,95
abc,sub1,87
def, sub2,67
...

输出如下 / p>

 <?xml version =1.0encoding =UTF-8?& 
< records>
< user>
< name> krunal< / name>
< subject> maths< / subject>
< marks> 95< / marks>
< / user>
< user>
< name> abc< / name>
< subject> sub1< / subject>
< marks> 87< / marks>
< / user>
< user>
< name> def< / name>
< subject> sub2< / subject>
< marks> 67< / marks>
< / user>



< / records>

任何人都可以提供如何在eXist数据库中使用XQuery将CSV数据添加到已有的XML

对于读取文件,请查看您的XQuery实现文档,这里为eXist阅读文件



Wikibooks有一个很好的例子说明如何解析CSV:

  let $ csv:='name,faculty 
alice,anthropology
bob,biology'

let $ lines: = tokenize($ csv,'\\\
')
let $ head:= tokenize($ lines [1],',')
let $ body:= remove($ lines,1)
return
< people>
{
for $ line in $ body
let $ fields:= tokenize($ line,',')
return
< person>
{
for $ key $ on $ head
let $ value:= $​​ fields [$ pos]
return
元素{$ key} {$ value }
}
< / person>
}
< / people>另一种可能性是使用另一个具有内置csv导入支持的XQuery引擎,例如 ://www.zorba-xquery.com/doc/zorba-latest/zorba/xqdoc/xhtml/www.zorba-xquery.com_modules_csv2xml.htmlrel =nofollow> Zorba BaseX


I am working on eXist database, I am have a new Idea that I have to implement XML file using XQuery.

I want to convert CSV file to XML which is already in the database collection. And this XML file contains only necessary tags and information. And this converted data will be saved into XML in eXist Database.

XML like this: its name is 'createXML.xml'

<?xml version="1.0" encoding="UTF-8"?>
<records>
// All the Records from CSV file want to put here..... between Records tags
</records>

now the CSV file like this:

name,subject,marks    //header lines
krunal,maths,95
abc,sub1,87
def,sub2,67
...

Output like this

<?xml version="1.0" encoding="UTF-8"?>
<records>
   <user>
       <name>krunal</name>
       <subject>maths</subject>
       <marks>95</marks>
   </user>
   <user>
       <name>abc</name>
       <subject>sub1</subject>
       <marks>87</marks>
   </user>
   <user>
       <name>def</name>
       <subject>sub2</subject>
       <marks>67</marks>
   </user>
      .
      .
      .
</records>

Can anyone provide me how to add CSV data to already available XML using XQuery in eXist database and performs this function.

解决方案

For reading a file look at your XQuery implementations documentations, here reading a file for eXist.

Wikibooks has an excellent example on how to parse CSV:

let $csv := 'name,faculty
alice,anthropology
bob,biology'

let $lines := tokenize($csv, '\n')
let $head := tokenize($lines[1], ',')
let $body := remove($lines, 1)
return
    <people>
        {
            for $line in $body
            let $fields := tokenize($line, ',')
            return
                <person>
                    {
                        for $key at $pos in $head
                        let $value := $fields[$pos]
                        return
                            element { $key } { $value }
                    }
                </person>
        }
    </people>

Another possibility would be to use another XQuery engine with builtin csv import support like Zorba or BaseX.

这篇关于如何使用eXist数据库中使用的XQuery文件将CSV数据添加到已有的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆