Page 1 of 1

Problems with Automatic editing of endtags

Posted: Mon Mar 22, 2010 7:52 pm
by mrfritz
Hello,

I am trying to convert an xml document element tags.

My original XML consists of the following:
<element name='File' >CrimeReport_010609-011209.txt</element>
<element name='CrimeNumber' >10032009.201720.309.CFC</element>
...

I would like to change it to the following:

<File>CrimeReport_010609-011209.txt</File>
<CrimeNumber>10032009.201720.309.CFC</CrimeNumber>


I am able to change the <element name = ' '> tag using the Find/Replace however the endtag is not automatcally changing.

IS there some way to automatically change the endtags throughout the document?

Or use Find/Replace in a different manner?

Thanks,
Mike

Re: Problems with Automatic editing of endtags

Posted: Mon Mar 22, 2010 10:03 pm
by george
The best tool to make such processing is XSLT. For example you can start with a recursive copy template and add a rule that matches element elements and creates a new element with the name specified in the name attribute:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="element">
<xsl:element name="{@name}"><xsl:apply-templates/></xsl:element>
</xsl:template>
</xsl:stylesheet>
on an input like

Code: Select all


<test>
<element name="File">CrimeReport_010609-011209.txt</element>
<element name="CrimeNumber">10032009.201720.309.CFC</element>
</test>
gives

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<test>
<File>CrimeReport_010609-011209.txt</File>
<CrimeNumber>10032009.201720.309.CFC</CrimeNumber>
</test>
If you still need a solution based on find and replace let us know.

Best Regards,
George