DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   XML, XSLT and XPATH (http://www.daniweb.com/forums/forum134.html)
-   -   XML to XML make all elements attributes (http://www.daniweb.com/forums/thread123046.html)

nnobakht May 7th, 2008 4:28 pm
XML to XML make all elements attributes
 
Hey guys i have a xml file which looks like
  <?xml version="1.0" ?> 
- <Booking>
- <booking>
- <required>
  <VoyageID>navid</VoyageID>
  <BookingNumber>omid</BookingNumber>
  <LoadPort>nariman</LoadPort>
  <DischargePort>mojedeh</DischargePort>
  </required>
- <additional>
  <sepandar>sepandar</sepandar>
  <sepand>sepand</sepand>
  <manoucher>manoucher</manoucher>
  <nasrin>nasrin</nasrin>
  <homa>homa</homa>
  <parviz>parviz</parviz>
  <farhad>farhad</farhad>
  <giti>giti</giti>
  <marzbani>marzbani</marzbani>
  </additional>
  </booking>
- <booking>
- <required>
  <VoyageID>2</VoyageID>
  <BookingNumber>141</BookingNumber>
  <LoadPort>280</LoadPort>
  <DischargePort>419</DischargePort>
  </required>
- <additional>
  <sepandar>558</sepandar>
  <sepand>697</sepand>
  <manoucher>836</manoucher>
  <nasrin>975</nasrin>
  <homa>1114</homa>
  <parviz>1253</parviz>
  <farhad>1392</farhad>
  <giti>1531</giti>
  <marzbani>1670</marzbani>
  </additional>
  </Booking>

i have an xslt that is supposed to take all the elements and make them attributes.
the final looks like
  <?xml version="1.0" encoding="utf-8" ?> 
- <Booking>
- <booking>
  <required VoyageID="2" BookingNumber="21" LoadPort="30" DischargePort="49" />
  <additional sepandar="68" sepand="87" manoucher="106" nasrin="125" homa="144" parviz="163" farhad="182" giti="201" marzbani="220" />
  </booking>
- <booking>
  <required VoyageID="3" BookingNumber="12" LoadPort="31" DischargePort="50" />
  <additional sepandar="69" sepand="88" manoucher="107" nasrin="126" homa="145" parviz="164" farhad="183" giti="202" marzbani="221" />
  </booking>
  </Booking>
but i want the <required.....> not to have required anmore but just move up to <booking....> and then the additional still there as a element under booking
i dont know where im going wrong can some one help
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:import href="copy.xslt"/>

  <xsl:output method="xml" indent="yes" version="1.0" encoding="UTF-8"/>

  <!-- Match elements that are parents -->
  <xsl:template match="*[*]">
    <xsl:choose>
      <!-- Only convert children if this element has no attributes -->
      <!-- of its own -->
      <xsl:when test="not(@*)">
        <xsl:copy>
          <!-- Convert children to attributes if the child has -->
          <!-- no children or attributes and has a unique name -->
          <!-- amoung its siblings -->
          <xsl:for-each select="*">
            <xsl:choose>
              <xsl:when test="not(*) and not(@*) and
                            not(preceding-sibling::*[name(  ) =
                                                    name(current(  ))])
                            and
                            not(following-sibling::*[name(  ) =
                                                    name(current(  ))])">
                <xsl:attribute name="{local-name(.)}">
                  <xsl:value-of select="."/>
                </xsl:attribute>
              </xsl:when>
              <xsl:otherwise>
                <xsl:apply-templates select="."/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:for-each>
        </xsl:copy>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>


All times are GMT -4. The time now is 1:29 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC