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

  • Home
  • SEARCH
  • 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 521485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:15:19+00:00 2026-05-13T08:15:19+00:00

I want to transform a large xml file to sql statements with xslt. For

  • 0

I want to transform a large xml file to sql statements with xslt. For example I have the author tag.

<author>Evans, Jim; Henderson, Mike; Coyier, Alan</author>

I have a column for last_name and first_name, so Evans, Henderson and Coyier should go to last_name and so on.

How can I pick them out of the tag and put it into sql statements!

Thanks in advance!

  • 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-05-13T08:15:19+00:00Added an answer on May 13, 2026 at 8:15 am

    You can do it with xslt, but it’s not very pretty as you need to parse out the lastname/firstname pairs, and then the lastname – firstname yourself. This is done through recursion.

    In the same xslt you can generate SQL statements from it, but again this is not painless as you have to escape any literal string delimiters, for example, O'Hanlon must become the SQL string literal 'O''Hanlon'.

    Again, this is done with recursion.

    This is an example that is fully functional:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:output method="text"/>
    
      <!-- match the eelement to extract data from -->
    
      <xsl:template match="/author">
        <xsl:call-template name="authors">
          <xsl:with-param name="authors" select="text()"/>
        </xsl:call-template>
      </xsl:template>
    
      <!-- recursively extract individual authors -->
      <xsl:template name="authors">
        <xsl:param name="authors"/>
        <xsl:variable name="author" select="substring-before($authors,';')"/>
        <xsl:choose>
          <xsl:when test="string-length($author)=0">
            <xsl:call-template name="author">
              <xsl:with-param name="author" select="$authors"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="author">
              <xsl:with-param name="author" select="$author"/>
            </xsl:call-template>
            <xsl:call-template name="authors">
              <xsl:with-param name="authors" select="substring-after($authors,';')"/>
            </xsl:call-template>        
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    
      <!-- extract firstname, lastname, escape single quote, and generate SQL -->
      <xsl:template name="author">
        <xsl:param name="author"/>
          <xsl:variable name="last-name" select="normalize-space(substring-before($author, ','))"/>
          <xsl:variable name="first-name" select="normalize-space(substring-after($author, ','))"/>
          INSERT INTO author (first_name, last_name) VALUES (
            '<xsl:call-template name="replace">
               <xsl:with-param name="text" select="$first-name"/>
               <xsl:with-param name="search">&apos;</xsl:with-param>
               <xsl:with-param name="replace">&apos;&apos;</xsl:with-param>
             </xsl:call-template>'
          ,
            '<xsl:call-template name="replace">
               <xsl:with-param name="text" select="$last-name"/>
               <xsl:with-param name="search">&apos;</xsl:with-param>
               <xsl:with-param name="replace">&apos;&apos;</xsl:with-param>
             </xsl:call-template>'
          );
      </xsl:template>
    
      <!-- recursive search and replace -->
      <xsl:template name="replace">
        <xsl:param name="text"/>
        <xsl:param name="search"/>
        <xsl:param name="replace"/>
        <xsl:value-of select="$text"/>
        <xsl:variable name="tail">
          <xsl:if test="contains($text, $search)">
            <xsl:call-template name="replace">
              <xsl:with-param name="text" select="substring-after($text, $search)"/>
              <xsl:with-param name="search" select="$search"/>
              <xsl:with-param name="replace" select="$replace"/>
            </xsl:call-template>
          </xsl:if>
        </xsl:variable>
        <xsl:value-of select="concat(substring-before($text, $search), $tail)"/>
      </xsl:template>
    </xsl:stylesheet>
    

    with your input:

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="author.xslt"?>
    <author>Evans, Jim; Henderson, Mike; Coyier, Alan</author>
    

    This gives me this output:

    INSERT INTO author (first_name, last_name) VALUES ( 'Jim' , 'Evans' );
    INSERT INTO author (first_name, last_name) VALUES ( 'Mike' , 'Henderson' );
    INSERT INTO author (first_name, last_name) VALUES ( 'Alan' , 'Coyier' ); 
    

    If you need to do a lot of XML munging and inserting it into databases, I can recommend using a tool like kettle, aka. pentaho data integration. It has many steps that you can use to process data, and out-of-the-box connectivity for more than 30 databases. It’s free, and easy to install. get it here:
    http://sourceforge.net/projects/pentaho/files/Data%20Integration/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.