Convert an XML file to another XML file using XSLT

How do I transform XML 1 to XML 2 using XSLT? I am new to XSLT.

XML 1 - This is the first XML
<?xml version="1.0" encoding="utf-8"?>
<root type="array">
    <item type="object">
        <a:item xmlns:a="item" item="@id" type="string">_:genid1</a:item>
        <a:item xmlns:a="item" item="@type" type="array">
            <item type="string">http://www.w3.org/2000/01/rdf-schema#Datatype</item>
        </a:item>
        <a:item xmlns:a="item" item="http://www.w3.org/2002/07/owl#oneOf" type="array">
            <item type="object">
                <a:item xmlns:a="item" item="@list" type="array">
                    <item type="object">
                        <a:item xmlns:a="item" item="@value" type="string">L</a:item>
                    </item>
                    <item type="object">
                        <a:item xmlns:a="item" item="@value" type="string">Q</a:item>
                    </item>
                    <item type="object">
                        <a:item xmlns:a="item" item="@value" type="string">R</a:item>
                    </item>
                </a:item>   <!-- added by edit -->
            </item>
        </a:item>
    </item>
</root>

XML 2 - This is the second XML that I need to convert the first XML to.

<?xml version="1.0" encoding="utf-8"?>
<root type="array">
    <persons>
        <person person_id = "_genid1"></person>
        <type>
            http//www.w3.org/2000/01/rdf-schema#Datatype
        </type>
        <oneofs>
            <oneof>
                L|Q|R
            </oneof>
        </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.

zc2Commented:
The simplest way is as the following. If you need more generalization, then it would be more complex.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:a="item" exclude-result-prefixes="a">
	<xsl:output method="xml" indent="yes"/>
		
	<xsl:template match="root">
		<root type="array">
			<xsl:apply-templates select="item"/>
		</root>
 	</xsl:template>

	<xsl:template match="item">
		<persons>
			<person person_id="{a:item[@item='@id']}"></person>
			<type><xsl:value-of select="a:item[@item='@type']/item"/></type>
			<oneofs>
				<oneof>
					<xsl:for-each 
						select="a:item[@item='http://www.w3.org/2002/07/owl#oneOf']/item/a:item/item">
						<xsl:if test="position()&gt;1">|</xsl:if>
						<xsl:value-of select="a:item"/>
     				</xsl:for-each>
				</oneof>
			</oneofs>
		</persons>
	</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
Edson FernandesAuthor Commented:
Hello,

Thank You for your help. I tried to take my xml and your stylesheet are process it on the w3schools parser and I got an error . How can I test it?

https://www.w3schools.com/xml/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog

Thank You,
EF
0
zc2Commented:
That online tool is good only for XSLT which produces an HTML.
In your case, the output is another XML, that page is not able to show it properly.
To play with XSLT I recommend this utility.
Also, there a command line utility you could try.
0
Edson FernandesAuthor Commented:
Thank You, That worked on my test file and  have sent you another file. Let me know.
0
Edson FernandesAuthor Commented:
Can you please make this small change in the style sheet so that the output  is

<person_id>_genid1</person_id>

instead of

       <person person_id = "_genid1"></person>
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.