Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9053011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:19:14+00:00 2026-06-16T13:19:14+00:00

I have an XML file and I need to convert it into XQuery. consider

  • 0

I have an XML file and I need to convert it into XQuery. consider a simple set of XML:

books[book]
book[@isbn, title, descrption]

eg:

<books>
    <book isbn="1590593049">
        <title>Extending Flash MX 2004</title>
        <description>
        Using javascript alongwith actionscript 3.0 and mxml.</description>
    </book>
    <book isbn="0132149184">
        <title>Java Software Solutions</title>
        <description>
            Complete book full of case studies on business solutions and design concepts while building mission critical
            business applications.
        </description>
    </book>

How to convert it to CSV format using XQuery? The CSV is used by Microsoft excel,

so it would be delimited by comma (,) character and special characters should be escaped.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:19:15+00:00Added an answer on June 16, 2026 at 1:19 pm

    A pure XPath 2.0 expression:

    for $b in /*/book
        return
          concat(escape-html-uri(string-join(($b/@isbn,
                                              $b/title,
                                              $b/description
                                              )
                                               /normalize-space(),
                                            ",")
                                 ),
                 codepoints-to-string(10))
    

    XSLT 2 – based verification:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/">
      <xsl:sequence select=
       "for $b in /*/book
           return
             concat(escape-html-uri(string-join(($b/@isbn,
                                                 $b/title,
                                                 $b/description
                                                 )
                                                  /normalize-space(),
                                               ',')
                                    ),
                    codepoints-to-string(10))"/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document (corrected from its malformedness):

    <books>
        <book isbn="1590593049">
            <title>Extending Flash MX 2004</title>
            <description>
            Using javascript alongwith actionscript 3.0 and mxml.</description>
        </book>
        <book isbn="0132149184">
            <title>Java Software Solutions</title>
            <description>
                Complete book full of case studies on business solutions and design concepts while building mission critical
                business applications.
            </description>
        </book>
    </books>
    

    the wanted, correct result is produced:

    1590593049,Extending Flash MX 2004,Using javascript alongwith actionscript 3.0 and mxml.
     0132149184,Java Software Solutions,Complete book full of case studies on business solutions and design concepts while building mission critical business applications.
    

    Update:

    In a comment the OP has requested that any in-text comma be surrounded by a quote and that (after that) any quote be replaced by two quotes, and, finally, if the wholw result contains a quote, it must be surrounded by (single) quotes.

    Here is a pure XPath 2.0 expression that produces this:

    for $b in /*/book,
        $q in codepoints-to-string(34),
        $NL in codepoints-to-string(10),
        $isbn in normalize-space(replace($b/@isbn, ',', concat($q,',',$q))),
        $t in normalize-space(replace($b/title, ',', concat($q,',',$q))),
        $d in normalize-space(replace($b/description, ',', concat($q,',',$q))),
        $res in
         escape-html-uri(string-join(($isbn,$t,$d), ',')),
        $res2 in replace($res, $q, concat($q,$q))
       return
        if(contains($res2, $q))
           then concat($q, $res2, $q, $NL)
           else concat($res2, $NL)
    

    When this XPath expression is evaluated against this (extended with a new test-case) XML document:

    <books>
        <book isbn="1590593049">
            <title>Extending Flash MX 2004</title>
            <description>
            Using javascript alongwith actionscript 3.0 and mxml.</description>
        </book>
        <book isbn="0132149184">
            <title>Java Software Solutions</title>
            <description>
                Complete book full of case studies on business solutions and design concepts while building mission critical
                business applications.
            </description>
        </book>
        <book isbn="XX1234567">
            <title>Quotes and comma</title>
            <description>
                Hello, World from "Ms-Excel"
            </description>
        </book>
    </books>
    

    the wanted, correct result is produced:

    1590593049,Extending Flash MX 2004,Using javascript alongwith actionscript 3.0 and mxml.
    0132149184,Java Software Solutions,Complete book full of case studies on business solutions and design concepts while building mission critical business applications.
    "XX1234567,Quotes and comma,Hello"","" World from ""Ms-Excel"""
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the xml file, i need to convert into the json. the json
I need to Convert a CSV into an XML document. The examples I have
I have a Perl script to convert the XML file below into a hash:
I have the following XML file that I need to convert to JSON in
I need to convert .ass subtitles file into .xml file. So far I did
I have xml file and I need to convert the text that I get
I need to load an XML file and convert the contents into an object-oriented
I have an XML file that I need to deserialize into an object similar
I have an xml file I need to modify: ... </web-app> I want to
I have a xml file which I need to open with Microsoft Word 2007.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.