Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

XML Attribures to Elements

Posted on 2011-02-22
6
Medium Priority
?
353 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 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

916 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