Solved

Automatic XSL Generator

Posted on 2003-11-17
11
686 Views
Last Modified: 2012-08-14
I'm using JAXB to store the configuration of my program. Ofcourse the XML Schema's I use change my application grows. I want to have a XSL transformation that transforms the XML in the old format to the new format. I have XML Schema's describing both formats and there are only elements added, never removed.
Is there a program that creates the XSL automatically?
I have looked at XSLerator, but it does not do what I want.
0
Comment
Question by:FesterWim
  • 5
  • 4
11 Comments
 
LVL 2

Expert Comment

by:SuperKarateMonkey
ID: 9764088
It seems that you wouldn't actually need a separate program, but rather just an intelligent XSL Stylesheet.

My first guess would be to take the 3 XML files, (the source XML file, as well as the two schemas,) and merge them into one uber-DOM.  From there you'd be able to move back and forth between the three, reading from the two schemas and modifying the source to match.

I worked with XSL Stylesheets at my previous job, and you can pretty much use them to do just about anything to a DOM that you might like.  Gimme an example of what you're trying to do, with the 2 sample Schemas, and I think I might be able to help.

0
 
LVL 1

Author Comment

by:FesterWim
ID: 9764298
Yes, I just need a XSL stylesheet, that is true. But I want a program that creates this stylesheet for me. So I can apply the XSL transform to the original XML file and it should be converted to conform to the new XSD. Should I mail you the XML files because they might be a bit big to post here (about 150 lines each)
0
 
LVL 2

Expert Comment

by:SuperKarateMonkey
ID: 9764359
Yeah, see, that's my point:  You won't need a program to write XSL Stylesheets.  You just want a Stylesheet that's lot more intelligent than you think.  See, what I'm imagining is that you could take your three XML files, and mash them together into one big DOM.  Nothing fancy, just create one root node and append each of them to it.  Then for each node in the old schema, your stylesheet could move into the new schema, find the changes that needed to be made, and then finally move into the source xml and apply said changes.

You don't need to send me the whole files.  Just post up here a snippet of code from each of the three files that are relevant:

1.  From the old schema a snippet that has the definition of a particular node.
2.  From the new schema a snippet that REDEFINES said node.
3.  From the source xml an example of the OLD node.
4.  Optionally, what the example node should look like post-transformation.

Though the fourth snippet isn't essential, (it should be implied by the second one,) it'd be nice for verification.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 1

Author Comment

by:FesterWim
ID: 9764830
Ok, got the idea now. That would be very nice if that would be possible. But I don't know much about XSL, so that's why I thought that a program would be easier for me...

SOURCE XSD:
<xs:element name="doelstellingenlijst">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="doelstelling" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

TARGET XSD:
<xs:element name="doelstellingenlijst">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="doelstelling" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<xs:element name="doelstelling">
   <xs:complexType>
     <xs:sequence>
      <xs:element name="doelstelling" type="xs:string" minOccurs="1" maxOccurs="1"/>
      <xs:element name="linkedlesverloop" type="xs:string" minOccurs="0" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

SOURCE XML:
<doelstellingenlijst>
  <doelstelling>nieuwe doelstelling2</doelstelling>
  <doelstelling>nieuwe doelstelling1</doelstelling>
<doelstellinglijst>

RESULTING XML:
<doelstellingenlijst>
  <doelstelling>
    <doelstelling>nieuwe doelstelling2</doelstelling>
    <linkedlesverloop></linkedlesverloop>
  </doelstelling>
  <doelstelling>
    <doelstelling>nieuwe doelstelling1</doelstelling>
    <linkedlesverloop></linkedlesverloop>
  </doelstelling>
</doelstellingenlijst>
0
 
LVL 2

Accepted Solution

by:
SuperKarateMonkey earned 50 total points
ID: 9765239
OK, this isn't impossible.  What you're going to need to do is identify the cases you can face with modifying the DOM.  There probably aren't going to be an unbounded series of them.  For example the cases would be the "AddChild" case, where you essentially add a child and then output the next node(s), and "CreateChild," where you just create an empty child node.  In this one you'd need XSL applied ot the source XML that looked like this:

<xsl:template match="doelstelling">
  <doelstelling>
    <xsl:apply-templates select="text()[normalize-space(.) != ''][1]" mode="AddChild">
      <xsl:with-param name="NodeName">doelstelling</xsl:with-param>
    </xsl:apply-templates>
    <xsl:call-template name="createChild">
      <xsl:with-param name="NodeName">linkedlesverloop</xsl:with-param>
    </xsl:call_template>
  </doelstelling>
</xsl:template>

<xsl:template match="*" mode="AddChild">
  <xsl:element name="{$NodeName}">
    <xsl:value-of select="normalize-space(.)" />
  </xsl:element>
</xsl:template>

<xsl:template name="CreateChild">
  <xsl:element name="{$NodeName}"/>
</xsl:template>

Now, the tricky bit here is having the stylesheet RECOGNIZE which mode it's going to have to alter the source xml with.  That I can't help you with, since you've got the whole span of what and how things can be changed, but basically instead of just calling these templates, the stylesheet will need to recognize a node called xs:element name="doelstelling"> and go out to the new schema to find the corrolary element, interrogate the new child elements of that node, (if any,) and then call/apply the appropriate templates against the source document.

It's not an easy problem, but coding a java program to dynamically write XSL is probably harder, and more time consuming.  At my old job I did both and the former was definitely the better way to go, I think.
0
 
LVL 2

Expert Comment

by:SuperKarateMonkey
ID: 9765256
P.S. - You might be able to get more assistance in the XSL area of this site, as this is rapidly becoming an XSL question, not a java question.

The xml area can be found at:

http://www.experts-exchange.com/Web/Web_Languages/XML/
0
 
LVL 1

Author Comment

by:FesterWim
ID: 9769419
Hmm.. I still think I'm going to have do to way to much for the purpose I need it for. I have 2 beta testers and I want convert their configuration to the new format. Probably the easiest would be for me to spend a weekend learning XSL and writing something myself. Those AddChild and CreateChild templates will probably be very handy.

After a little search I found that what I probably really want is this:
http://www.xmlspy.com/features_xml2xml_mapforce.html

I will try out the trial and see if it does what I want
0
 
LVL 1

Author Comment

by:FesterWim
ID: 9785917
This Mapforce thing defenitely takes the work out of my hands. It generates a XSL file from the 2 XSD files I give. I just needed to tweak the generated XSL file a bit, and I could convert my old configuration without problems.
0
 
LVL 2

Expert Comment

by:SuperKarateMonkey
ID: 9788257
I suppose that's fine.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simple java question 3 56
Apps blocked by Java 9 84
Notify sent to other threads in Java 9 16
Selenium docs api java index 3 20
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

832 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