Home > Blockchain >  Create a csv from an xquery
Create a csv from an xquery

Time:07-24

I am trying to create 2 relational database tables for the below xml via xquery. I have created one table for the course listing as follows. Which formats perfectly for what I need but when I try and create the section listing, it doesn't seem to create the csv file correctly. The columns the data is exported in doesn't follow each component and I am usure why this is.

This is the undesired output enter image description here

    file:write("course.csv",
    for $x in doc("uwm.xml")//course_listing
    let $y := string-join($x/*[not(name()="section_listing")],"zzcommazz")
    let $y := concat($y,"
")
    let $y := replace($y,'zzcommazz', ',')
    let $y := replace($y,'"', '"')
    return $y
    )


file:write("section.csv",
for $x in doc("uwm.xml")//section_listing
let $y := string-join($x/*[not(name()="course_listing")],"zzcommazz")
let $y := concat($y,"
")
let $y := replace($y,'zzcommazz', ',')
let $y := replace($y,'"', '"')
return $y
)


  <?xml version='1.0' ?>
            <root>
            <course_listing>
              <note>#</note>
              <course>216-088</course>
              <title>NEW STUDENT ORIENTATION</title>
              <credits>0</credits>
              <level>U</level>
              <restrictions>; ; REQUIRED OF ALL NEW STUDENTS. PREREQ: NONE</restrictions>
               <section_listing>
                  <section_note></section_note>
                  <section>Se 001</section>
                  <days>W</days>
                  <hours>
                      <start>1:30pm</start>
                      <end></end>
                  </hours>
                  <bldg_and_rm>
                      <bldg>BUS</bldg>
                      <rm>S230</rm>
                  </bldg_and_rm>
                  <instructor>Gusavac</instructor>
                  <comments>9 WKS BEGINNING WEDNESDAY, 9/6/00 </comments>
               </section_listing>
               <section_listing>
                  <section_note></section_note>
                  <section>Se 002</section>
                  <days>F</days>
                  <hours>
                      <start>11:30am</start>
                      <end></end>
                  </hours>
                  <bldg_and_rm>
                      <bldg>BUS</bldg>
                      <rm>S171</rm>
                  </bldg_and_rm>
                  <instructor>Gusavac</instructor>
                  <comments>9 WKS BEGINNING FRIDAY, 9/8/00 </comments>
               </section_listing>
            </course_listing>
            
            <course_listing>
              <note>#</note>
              <course>216-293</course>
              <title>BUSINESS ETHICS</title>
              <credits>3</credits>
              <level>U</level>
              <restrictions>; ; PREREQ: NONE</restrictions>
               <section_listing>
                  <section_note></section_note>
                  <section>Se 001</section>
                  <days>R</days>
                  <hours>
                      <start>2:30pm</start>
                      <end>5:10pm</end>
                  </hours>
                  <bldg_and_rm>
                      <bldg>BUS</bldg>
                      <rm>S230</rm>
                  </bldg_and_rm>
                  <instructor>Silberg</instructor>
               </section_listing>
            </course_listing>
            </root>

CodePudding user response:

I haven't followed this in detail and you don't actually say what output you are getting and what output you want, but this is obviously wrong:

replace($y,'"', '&quot;')

because it replaces a character by itself: '"' and '"' are two different ways of writing the same thing. If you want to write out '"' as 6 characters, you need '&quot;'; but I'm not sure why you would want to do that, since '"' is only meaningful in XML, not in CSV.

  • Related