Question: How can I Transform one XML file format to another XML file format using XSLT

Question: How can I Transform one XML file format to another XML file format using XSLT. I am new to XML/XSLT so I need help. I need to spit a string inside the xml.

XML1
<?xml version="1.0" encoding="utf-8"?>
<root type="array">
<persons>
<person_id>_:genid1</person_id>
<type>http://www.w3.org/2000/01/rdf-schema#Datatype</type>
<oneofs>
<oneof>This is a very long string</oneof>
</oneofs>
</persons>
<persons>
<person_id>_:genid108</person_id>
<type>http://www.w3.org/2000/01/rdf-schema#Datatype</type>
<oneofs>
<oneof>This is  another very long string</oneof>
</oneofs>
</persons>
</root>
XML2
<?xml version="1.0" encoding="utf-8"?>
<root type="array">
<persons>
<person_id>_:genid1</person_id>
<type>http://www.w3.org/2000/01/rdf-schema#Datatype</type>
<oneofs>
<oneof>This is </oneof>
<oneofagain>a very long string</oneofagain>
</oneofs>
</persons>
<persons>
<person_id>_:genid108</person_id>
<type>http://www.w3.org/2000/01/rdf-schema#Datatype</type>
<oneofs>
<oneof>This is </oneof>
<oneofagain>another very long string</oneofagain>
</oneofs>
</persons>
</root>
Edson FernandesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gertone (Geert Bormans)Information ArchitectCommented:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
 
    <xsl:template match="oneof">
        <xsl:copy>
            <xsl:value-of select="substring(., 1,8)"/>
        </xsl:copy>
        <xsl:element name="oneofagain">
            <xsl:value-of select="substring(., 9)"/>
        </xsl:element>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gertone (Geert Bormans)Information ArchitectCommented:
OK if you are learning.....

Asking for code does not teach you anything.
At least  try to work on this yourself and ask help when you get stuck
That is how you learn

Just dumping an in and output xml does not tell us anything about the requirements such as when and why to split the string,
I made a guess, I thing you can try to work from this example yourself

Please note that in the XSLT there is no use of for-each nor xsl:if
that is a best practice pattern, so try to adopt that

The pattern I use is an identity transform with one extra template
<xsl:template match="node()">
works for all nodes and makes a deeper copy in a generic way

 <xsl:template match="oneof">
has a more specific match attribute, so this template has a higher priority for the oneof element so it is active instead of the more generic template

            <xsl:apply-templates select="node()"/>
tells the processor to continu processing the content of the current context

Try to really understand this pattern
It is the most important step you need to take to learn XSLT
Please put some effort in yourself,
EE is not a code shitting machine
0
Shaun VermaakTechnical SpecialistCommented:
I use BizTalk to write transforms from one XML to another. Nothing stops you from spinning up a trial instance in Azure and to run the transform wizard.
https://azure.microsoft.com/en-us/services/biztalk-services/
0
Gertone (Geert Bormans)Information ArchitectCommented:
Question answered in full
0
Gertone (Geert Bormans)Information ArchitectCommented:
A "thank you" would have been so nice. Guess I am old fashioned
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.