Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XML Attribures to Elements

Posted on 2011-02-22
6
Medium Priority
?
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

661 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