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?
 
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
 
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
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.

All Courses

From novice to tech pro — start learning today.