User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the XML, XSLT and XPATH section within the Software Development category of DaniWeb, a massive community of 363,482 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,529 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our XML, XSLT and XPATH advertiser:
Views: 464 | Replies: 0
Reply
Join Date: Nov 2006
Posts: 36
Reputation: nnobakht is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
nnobakht nnobakht is offline Offline
Light Poster

XML to XML make all elements attributes

  #1  
May 7th, 2008
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>
AddThis Social Bookmark Button
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb XML, XSLT and XPATH Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the XML, XSLT and XPATH Forum

All times are GMT -4. The time now is 3:16 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC