aspose file tools*
The moose likes XML and Related Technologies and the fly likes Need help with XSLT, modifying XML config files. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Need help with XSLT, modifying XML config files." Watch "Need help with XSLT, modifying XML config files." New topic
Author

Need help with XSLT, modifying XML config files.

Cole Groff
Greenhorn

Joined: Jun 24, 2008
Posts: 7
I am trying to modify XML config files using XSLT.

I have two goals at the moment:
1) To copy all tags and attributes from the config file, maintaining the hierarchy of the config file, to the new XML config file.
2) Being able to add new tags and attributes to the new XML config file.

Does anyone know how to do this? I've been trying the <xsl:element> element, but it isn't working so well. It has been overwriting config tags. How can I add new tags under the root tag?
Cole Groff
Greenhorn

Joined: Jun 24, 2008
Posts: 7
Here is an example of what I need to do:

Input XML file:
<configuration>
<appsettings>
<add key="111" value="222" />
<add key="111" value="222" />
</appsettings>
</configuration>

Then, I would need the output XML file to be something like this:
<configuration>
<appsettings>
<add key="111" value="222" />
<add key="111" value="222" />
</appsettings>
<other>
<add key="111" value="222" />
<add key="111" value="222" />
</other>
</configuration>

Does this make sense?

What I'm working on, is a master config file. I'm working on a project where a lot of people are working with different config files. I need to be able to have them use XSLT to transform their config file to match the master config file.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

The <xsl:element> element doesn't overwrite anything. Chances are you aren't writing out something that you should be writing out, and you're misinterpreting the result. But that's just a guess. It would be easier to discuss your code if we could see it...
Cole Groff
Greenhorn

Joined: Jun 24, 2008
Posts: 7
I am very new to this. Right now my code looks like this:
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" >

<xslutput method = "xml" indent = "yes" />

<!-- @* matches any attribute node -->
<!-- node() matches any node of any kind -->

<!-- copies all tags and their attributes under configuration -->
<xsl:template match = "node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>

<!-- maintains the short form tags -->
<xsl:template match="add[not(node())]|rar[not(node())]">
<xsl:element name="{name()}">
<xsl:apply-templates select="@*"/>
</xsl:element>
</xsl:template>

</xsl:stylesheet>

This pretty much just copies an existing XML file to a new XML file. It also has a little bit of code to keep the short hand tags consistent (keeping them shorthand).

But... I ran into a book called the XSLT Cookbook. It has an example in it called "Merging Documents with Unlike Schema." I'm thinking this may be what I need.

Does anyone have experience with combining XML files?
[ June 25, 2008: Message edited by: Cole Groff ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

First you started with the identity transformation. That's a good start. Then you overrode it for elements whose names are "add" or "rar" and which don't have any children. (???) So far so good. But what you do for an "add" element, for example, is to write out an "add" element with the same attributes as the original. This is exactly the same as what would have happened if you had left that template out entirely.

From your original post it looks like you want a rule something like this: "After writing an <appsettings> element, write out an <other> element and copy all the children of the <appsettings> element to be children of the <other> element."

Is that correct? I don't see any code anywhere where you write out an <other> element, and I had assumed that's what you were using the <xsl:element> element to do. You don't need <xsl:element> for that, though, you can just write an <other> element directly.

PS: You mentioned "combining" files. There's nothing in your code which looks at any file other than the single input file that you're transforming.
[ June 25, 2008: Message edited by: Paul Clapham ]
Cole Groff
Greenhorn

Joined: Jun 24, 2008
Posts: 7
Hey Paul,

My code has changed. This is the input file I'll be working with:
<configuration>
<configSections>
</configSections>
<appSettings>
</appSettings>
</configuration>

Here is the translation file code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="xml" indent="yes"/>


<!-- creates new info to be added to the output -->
<xsl:variable name="source">

<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

</xsl:variable>



<!-- merge input with "source" -->
<xsl:template match="configuration">
<configuration>
<xsl:copy-of select="*" />
<xsl:copy-of select="$source" />
</configuration>
</xsl:template>


</xsl:stylesheet>


There is a group of people who will be creating their own configuration tags. These are configurations for different aspects of the project. They will need to be able to hard code their configurations tags into the translation file. All of their configuration tags will need to be inserted inside the <configuration> tag, under the correct "second" tag.

Like in the translation code above, the tag <section .... /> will need to be added into a <configSections></configSections> tag, which is also under the root <configuration></configuration> tag.

Is there much I would need to change to my translation code to achieve this?
[ June 25, 2008: Message edited by: Cole Groff ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

There's something I'm missing. Is the input file you refer to the standard set of properties, or is it the overriding set of properties that the individuals are providing?
Cole Groff
Greenhorn

Joined: Jun 24, 2008
Posts: 7
Hey Paul,

Sorry, I'm being a little unclear.

I'm making progress today, and when I'm finished for the day, I'll update the topic on what I've done and make things more clear.
Cole Groff
Greenhorn

Joined: Jun 24, 2008
Posts: 7
Finally got a solution. This is used to add new XML to config files. If what is being added already exists, it is not added. The result is a new config file.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

I don't think you get it. You're supposed to be asking questions here.

But seriously, good work. Glad you got the problem sorted out.
Cole Groff
Greenhorn

Joined: Jun 24, 2008
Posts: 7
Yeah, sorry if I did this wrong. I wasn't exactly sure how to phrase my question. Then I was able to solve to problem. I didn't want to leave this forum hanging, without concluding this somehow. I do appreciate the help, though.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with XSLT, modifying XML config files.
 
Similar Threads
tidy config.txt
Pagination xml
Help loading XML config file?
Newbie Question
xsl variables to core set