Solved

Using XSLT to convert XML configuration files

Posted on 2012-03-26
6
25 Views
Last Modified: 2016-06-23
Hello

I have XML configuration files with the following (simplified) flat structure :
<MyEthernet Activity="true" DefaultGateway="192.168.0.253" PortNumber="1" EthId="IDa2"/>             
<MyEthernet Activity="true" DefaultGateway="193.168.0.253" PortNumber="2" EthId="IDa4"/>
<MyStreamer Activity="true" DestIpAddress="239.0.0.1" RtpId="IDa3" EthRef="IDa2"/>
...

Open in new window

I would like to convert like this:
<MyEthernet Activity="true" DefaultGateway="192.168.0.253" PortNumber="1" EthId="IDa2">             
   <MyStreamer Activity="true" DestIpAddress="239.0.0.1" RtpId="IDa3"/>
</MyEthernet>
<MyEthernet Activity="true" DefaultGateway="193.168.0.253" PortNumber="2" EthId="IDa4"/>

Open in new window


Can it be done using xslt and template matching? I used it before to match/correct single values, but never to move around tags.
0
Comment
Question by:FabClement
  • 3
6 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 37765199
You can easily do this using XSLT.

Just use a key that indexes the MyStreamer elements by their @EthRef

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:key name="str" match="MyStreamer" use="@EthRef"/>
    
    <xsl:template match="MyEthernet">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="key('str', @EthId)" mode="inner"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="MyStreamer"/>

    <xsl:template match="MyStreamer" mode="inner">
        <xsl:copy>
            <xsl:copy-of select="@*[not(name() = 'EthRef')]"/>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window


You might need to add a template for the root element
(and you might hit an issue with a default namespace on the root element)
0
 
LVL 1

Expert Comment

by:StarbucksDrinker
ID: 38869650
XSLT will not process multiple root elements.  If you can wrap your config nodes inside one root (called anything, as long as it's different from your existing nodes), then wrap your <xsl:template match="MyEthernet"> inside <xsl:template match="/"> and you should be good to go!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 38870067
Hi Starbucksdrinker.
I think you simply did not have enough Starbucks today :-)
You can not wrap an xsl:template inside another xsl:template
that simply is illegal XSLT

You are right, in order to have wellformed XML there needs to be a container root element.
But you are wrong again in the "called anything but... " There is no reason at all why the root element needs to have a different name. As long as the match statement in the xsl:template is specific enough to differentiate between match="/MyEthernet" for the root

You can have a template match="/" or match="/MyEthernet"
and inside there call out for matching nodes against the other templates
by using xsl:apply-templates inside a template.
But my example shows everything the original poster needs to get started

Brings me to the point... when will this question be closed?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 41666104
This is an excellent example of adding hierarchy to a flat XML, based on ID and reference IDs (prim key foreign key relation so to speak)
The solution is relevant in a general sense
Please accept https:#a37765199
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

789 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