XML Extract child elements as elements

I have the following XML from a Rest API call:

<opens type="array">
 <open>
  <account-id>123</account-id>
  <campaign-id type="integer">10000</campaign-id>
  <contact-id type="integer">302315</contact-id>
  <browser>Other</browser>
  <recorded-at type="dateTime">2016-03-12T12:52:07-05:00</recorded-at>
  <contact>
     <id type="integer">302315</id>
     <email>someone@mailnoone.com</email>
     <memberid>1255252</memberid>
  </contact>
 </open>
 <open>
  <account-id>123</account-id>
  <campaign-id type="integer">10000</campaign-id>
  <contact-id type="integer">302326</contact-id>
  <browser>Other</browser>
  <recorded-at type="dateTime">2016-03-13T12:52:07-05:00</recorded-at>
  <contact>
     <id type="integer">302326</id>
     <email>secondopen@mailnoone.com</email>
     <memberid>1255248</memberid>
  </contact>
 </open>
</opens>

and I am trying to get this as an output

<opens type="array">
 <open>
  <account-id>487</account-id>
  <campaign-id type="integer">504084</campaign-id>
  <contact-id type="integer">396056515</contact-id>
  <browser>Other</browser>
  <recorded-at type="dateTime">2016-03-12T12:52:07-05:00</recorded-at>
  <contact-id type="integer">396056515</id>
  <email>ldrmtrl@mac.com</email>
  <memberid>145773617</memberid>
 </open>
 <open>
  <account-id>123</account-id>
  <campaign-id type="integer">10000</campaign-id>
  <contact-id type="integer">302326</contact-id>
  <browser>Other</browser>
  <recorded-at type="dateTime">2016-03-13T12:52:07-05:00</recorded-at>
  <contact-id type="integer">302326</id>
  <email>secondopen@mailnoone.com</email>
  <memberid>1255248</memberid>
 </open>
</opens>

What should my XSLT look like? The contact element will appear only once inside each Open element. Sometimes there may not be any open element. I see some examples in this site that are similar but couldn't exactly figure this out.

Thanks, Ranchi

Answers


I think I found the answer. When I use the following XSLT it loads it correctly.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
 <xsl:template match="*">
  <xsl:element name="{name()}">
   <xsl:copy-of select="@*"/>
   <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>
 <xsl:template match="//contact">
  <xsl:apply-templates/>
 </xsl:template>
</xsl:stylesheet>

Please provide any better or faster performing solution if applies.

Thanks, Ranchi


Need Your Help

Adobe After Effects: Keep "Expression-Relations" when duplicating multiple layers

duplicates expression adobe after-effects

just wanted to ask, whether there is a way to keep the relations of expressions going when duplicating layers.

Offline websites using Google Gears or HTML5

asp.net-mvc-2 html5 google-gears offline-browsing

I have been reading about creating offline websites using Gears and using HTML 5. Im curious as to advantages and disadvantages of the two in regards to an ASP.Net MVC2 applcation with a SQL Datab...