Jump to content
lithium

Converting XML files, modifying Tags etc

Recommended Posts

Hi everyone.I have not done any work with XML yet but I have to solve a taks now and it would be very nice if you could point me to fitting documentation and tools.What I need to do is the transformation of an old XML file into a new version. Both are specified with a DTD. Scanning through the DTD tells me that I can remove most of the Tags that have changed, but I also need to insert one or two and change the Value of some Attributes.So this is not really a hard thing, it could be done using traditional text replacement systems, but I'm wondering what XML tools exist to do just that- reading XML file- removing obsolete Tags somewhere based on some definition- adding some Tags somewhere based on some definition- changing the Value of some Attributes based on some definition- writing the new XML fileI'm sure there are nice Tools for this. Any suggestions?

Share this post


Link to post
Share on other sites

XSLT is the answer to all of your "listed" questions except the last one. When opened with a browser you see the new XML, but you don't actually get the file itself. You must use a serer side scripting language or some specialized application to execute the XSLT transformation and generate the new XML file.Note that there is some way (not exactly sure what, but there is) to generate a new file based on a schema, but there isn't one for DTD. You must "manually" write the complete rules the XSLT would follow.

Share this post


Link to post
Share on other sites
XSLT is the answer to all of your "listed" questions except the last one.
I quickly read some of the XSLT ducumentation here on w3schools... As for "writing" the file, that is no problem anymore :) But a general question: do I see this correctly, I have to write a XSLT stylesheet with a definition for every tag used in my XML? What about tags that I don't want to change in any way, is there a way to first "copy" everything and then modify that copy, or at least, is there something like "if you find this tag, take it as it is for the output document"?

Share this post


Link to post
Share on other sites

It would be a lot easier if you know the structure of the document. For example, if a certain element is only renamed in the different versions, you would do:

<xsl:template match="OldElement"><NewElement><xsl:apply-templates /></NewElement></xsl:template>

If it's removed, then I think you might be able to do something like this:

<xsl:template match="RemovedElement1|RemovedElement2|RemovedElementN"><xsl:apply-templates /></xsl:template>

If there is more complex stuff such as same elements on different places, then it will get harder.As for "copying and then modifying"... that's not exactly an option. It's more like "modify and copy the rest" :) . To just apply everthing as it is, I think you should do something like:

<xsl:template match="/"><xsl:apply-templates/></xsl:template>

But then again, this means you have to declare the change of every element I think. I'm not exactly sure to be honest. I haven't tryed such "massive" convertions.There's also the <xsl:copy-of> but that doesn't allow for futher modification.

Share this post


Link to post
Share on other sites

Thanks, I'm slowly starting to get somewhere =)As for "modify and copy the rest", if I do what you said, I end up with just the values, but I also need the tags. For example, the old XML has the following:... <network name="net0"/> <network name="net1"/> <network name="net2"/>...This has not changed in the new XML. How can I get all the <network> tags and corresponding attributes to the new file? Also, how can this be done with something like... <ip if="eth0"/>1.1.1.1</ip>...Again, I want that same line in the new XML file.

Share this post


Link to post
Share on other sites

I'm not exactly sure what you mean with that <ip> thing...As for unchanged elements, I think they would be copyed directly. If not, then I think you should create another template for them and call it when needed. Something like this might do it:

<xsl:template name="same"><xsl:param name="current" select="."/><xsl:element name="{string($current)}">  <xsl:if test="@*">    <xsl:for-each select="@*">    <xsl:attribute name="{local-name(@*)}">    <xsl:value-of select="@*"/>    </xsl:attribute>    </xsl:for-each>  </xsl:if><xsl:apply-templates/></xsl:element></xsl:template>

And when in situation where you're sure the elements are the same just use:

<xsl:call-template name="same"/>

Note: I haven't tryed any of this stuff.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...