Solved

XML Attribures to Elements

Posted on 2011-02-22
6
343 Views
Last Modified: 2012-06-27
I have a pretty large XML file which looks like this:

<App action="A" id="2">
          <BaseVehicle id="30218"/>  
          <Note>My Note</Note>
          <Qty>1</Qty>
          <PartType id="1684"/>  
          <MfrLabel>some label</MfrLabel>
          <Position id="30"/>  
          <Part>12345</Part>
     </App>


There may be more elements in the <app></app> but just giving an example here.

I am trying to convert this to:


<App action="A" id="2">
          <BaseVehicle>30218</BaseVehicle>  
          <Note>My Note</Note>
          <Qty>1</Qty>
          <PartType>1684</PartType>
          <MfrLabel>some label</MfrLabel>
          <Position>30</Position>
          <Part>12345</Part>
     </App>

So basically I am trying to convert attributes to elements.  I have a lot of these XML files and they are pretty big.  Anyone know of a simple way to do this? Maybe some software?  
0
Comment
Question by:Kamilek0617
  • 4
6 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 34956444
You can do this using a very simple XSLT
<?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="*[count(@*) = 1][not(node())]">
        <xsl:copy>
            <xsl:value-of select="@*"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:Kamilek0617
ID: 34956486
What's XSLT?  how do I use this?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 34956497
If you want a batch process to run over all your files and perform the XSLT,
I recommend that you download saxon
www.saxonica.com, home edition will do
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 34956557
XSLT is a programming language designed for transforming XML.

Maybe the simpliest is to download here
http://sourceforge.net/projects/saxon/files/Saxon-HE/9.3/saxonhe9-3-0-4j.zip/download

put the saxon9he.jar somewhere on your machine.
you need java installed

you can then execute the transform like this

java net.sf.saxon.Transform -s:source -xsl:stylesheet -o:output
(make sure you put the .jar on the classpath)

more detail can be found here
http://www.saxonica.com/documentation9.2/using-xsl/commandline.html
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 35142372
The answer is correct and tested
and all follow up questions have been fully answered
You have no reason to delete this question
and your argument is plain false
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now