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 9230813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:54:42+00:00 2026-06-18T05:54:42+00:00

I am converting an XML file to another XML file using XSLT. XML file

  • 0

I am converting an XML file to another XML file using XSLT. XML file has element like this:

<Course>
      <ID>1001</ID>
      <Seats>10</Seats>
      <Description>Department: CS , Faculty: XYZ</Description>
</Course>

Now is there any way in XSLT by which when I generate new XML file that looks like this:

<Course>
      <ID>1001</ID>
      <Seats>10</Seats>
      <Department> CS </Department> 
      <Faculty> XYZ</Faculty>
</Course>

That is I want to split the Description element into two different elements Department and Faculty which are basically its content separated by comma. I used XMLspy to wrote my XSLT.

Thank you 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-06-18T05:54:43+00:00Added an answer on June 18, 2026 at 5:54 am

    Here’s one possible XSLT2 solution based on the identity transform and the tokenize string function in a template specific to the Description element.

    The general idea is to first split the Description string on “,” and then split each of the resulting substrings on “:”, picking only the last part.

    <?xml version="1.0"?>
    
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="Description">
            <xsl:variable name="tokens" select="fn:tokenize(text(),',')"/>
            <xsl:element name="Department"><xsl:value-of select="fn:normalize-space(fn:tokenize($tokens[1],':')[2])"/></xsl:element>
            <xsl:element name="Faculty"><xsl:value-of select="fn:normalize-space(fn:tokenize($tokens[2],':')[2])"/></xsl:element>
        </xsl:template>
    
    </xsl:stylesheet>
    

    The normalize-space function is invoked as a last step, to strip leading/trailing spaces; if this is not necessary, just leave that bit out.

    Caveat emptor: The assumtion here being that for format of the Description text is fixed (i.e. Department and Faculty always present in same order.) Also, it is assumed that neither “:” nor “,” occurs in the Description element text.

    The transformation above yields the expected result:

    <?xml version="1.0" encoding="UTF-8"?><Course>
          <ID>1001</ID>
          <Seats>10</Seats>
          <Department>CS</Department>
          <Faculty>XYZ</Faculty>
    </Course>
    

    Note that having structured information inside a run of plain text is not exactly making the best use of XML, which is all about structure, but I guess that format is not something you have control over.

    Update based on comments:

    A more robust alternative solution based on regular expression matching is listed below. In this case, only Description elements matching the Department, Faculty pattern are rewritten; otherwise the original Description element is passed through:

    <?xml version="1.0"?>
    
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="Description">
            <xsl:analyze-string select="." regex="\s*Department:\s*(.+)\s*,\s*Faculty:\s*(.+)\s*">
                <xsl:matching-substring>
                    <xsl:element name="Department"><xsl:value-of select="regex-group(1)"/></xsl:element>
                    <xsl:element name="Faculty"><xsl:value-of select="regex-group(2)"/></xsl:element>
                </xsl:matching-substring>
                <xsl:non-matching-substring>
                    <xsl:element name="Description"><xsl:copy/></xsl:element>
                </xsl:non-matching-substring>
            </xsl:analyze-string>
        </xsl:template>
    
    </xsl:stylesheet>
    

    The key idea here is to use xsl:analyze-string to test via an XSLT regular expression if the expected pattern is found, and capture the respective values in that case. If no match is found, the original contents of the Description element are copied through.

    Note: Integrating this with the root element is left as an exercise for the reader (since the OP example does not show where the Course elements fit).

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

Sidebar

Related Questions

I'm converting from one XML to another XML by using XSLT. By applying the
I am converting a xml using xslt. Original XML is <Content> <book> <customData> <CustomDataElement>
I am have trouble converting my xml using XSLT back to xml in the
So I have this huge XML file that I am converting to an object
I have problems with converting a xml file to a xml with XSLT. I
I have an assignment on converting the input xml using xslt. My input xml
I am converting xml to a flat file using BizTalk. There is a limitation
need to convert an xml file having a tag(dynamicVariable) that has an attribute(name).This xml
I am converting my XML file to CSV using XML2CSV The XML format can
I have an xml file like this : <products> <cars> <car> <brand>Audi</brand> <type>S3</type> <attribs>

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.