• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 60
  • Last Modified:

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>
0
Edson Fernandes
Asked:
Edson Fernandes
  • 4
2 Solutions
 
Geert BormansInformation 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
 
Geert BormansInformation 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 Specialist/DeveloperCommented:
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
 
Geert BormansInformation ArchitectCommented:
Question answered in full
0
 
Geert BormansInformation ArchitectCommented:
A "thank you" would have been so nice. Guess I am old fashioned
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now