tags:

views:

66

answers:

1

I have an XML file which is in this form:

<row>
  <field name="id">18</field>
  <field name="lat">40.7560540000</field>
  <field name="lng">-73.9869510000</field>
  <field name="body">Students arriving from Haiti say they’re having a hard time getting placed in city schools.</field>
</row>

I want these multiple tags to be combined and get an XML of this form (combining multiple tags to attributes of a single tag):

<row id="18" lat="40.7560540000" lng="-73.9869510000" body="Students arriving from Haiti say they’re having a hard time getting placed in city schools.">
</row>

Is it possible to do this? If yes, can someone suggest me how to make such a transformation? Thanks in advance.

A: 

Yes, such a change is possible. One easy way (depending how you look at it) is to apply an XML transformation to it:

<xsl:stylesheet 
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <!-- the document element gets copied -->
  <xsl:template match="/*">
    <xsl:copy>
      <!-- transform any child <row> elements -->
      <xsl:apply-templates select="row" />
    </xsl:copy>
  </xsl:template>

  <!-- <row> elements get copied, too -->
  <xsl:template match="row">
    <xsl:copy>
      <!-- transform any child <field> elements -->
      <xsl:apply-templates select="field" />
    </xsl:copy>
  </xsl:template>

  <!-- <field> elements become attributes -->
  <xsl:template match="field">
    <xsl:attribute name="{@name}">
      <!-- the value of the <field> becomes the attribute value -->
      <xsl:value-of select="." />
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

When applied to:

<rows>
  <row>
    <field name="id">18</field>
    <field name="lat">40.7560540000</field>
    <field name="lng">-73.9869510000</field>
    <field name="body">Students arriving from Haiti say they’re having a hard time getting placed in city schools.</field>
  </row>
</rows>

you get:

<rows>
  <row id="18" lat="40.7560540000" lng="-73.9869510000" body="Students arriving from Haiti say they’re having a hard time getting placed in city schools."></row>
</rows>
Tomalak
Thanks Tomalak. I had used a mysql database to store the records. I just converted the table to xml format using the following command:mysqldump --xml -u root -p [database-name] [tablename] > file.xmlSo now I have a plain xml file just consisting of the records. Where does this xslt come into picture? I'm very poor in this xslt related things. Kindly guide me.
mamatha
There are plenty of XSLT processors available. Standalone command line tools as well as programming language integrated ones. Choose ("Google for") a processor that works in your environment or fits into your processing chain, read a bit how it works and feed the XML file and the XSL stylesheet to it.
Tomalak
@mamatha: You can also wait and see if other suggestions come that fit your needs better. In the meantime, you can tell (best by editing the question) what tools you already use, and what programming languages you are prepared/able to use for this.
Tomalak