Solved

XML Attribures to Elements

Posted on 2011-02-22
6
349 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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