homepage Welcome to WebmasterWorld Guest from 23.23.9.5
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
converting the structure of an XML file
Flatten an XML file - question
jvworp

5+ Year Member



 
Msg#: 3661906 posted 2:42 pm on May 29, 2008 (gmt 0)

Ola everbody,

Lately i am trying to rearange the structure of an XML file via XSLT. But i really have no clue what to do.

my source file (without the XML overhead) is:

<subscription>
<a_id>10</c_id>
<usage>
<rf_id>90000</rf_id>
<zrt>E200</zrt>
</usage>
<usage>
<rf_id>90000</rf_id>
<zrt>E300</zrt>
</usage>
<stand>
<rf_id>90000</rf_id>
<twa>test1</twa>
<zrt>E200</zrt>
</stand>
<stand>
<rf_id>90000</rf_id>
<twa>test2</twa>
<zrt>E300</zrt>
</stand>
</subscription>

BUT...
what i really want is to split the file into two subscriptions

<subscription>
<a_id>10</a_id>
<usage>
<rf_id>90000</rf_id>
<zrt>E200</zrt>
</usage>
<stand>
<rf_id>90000</rf_id>
<twa>test1</twa>
<zrt>E200</zrt>
</stand>
</subscription>
<subscription>
<a_id>20</a_id>
<usage>
<rf_id>90000</rf_id>
<zrt>E300</zrt>
</usage>
<stand>
<rf_id>90000</rf_id>
<twa>test1</twa>
<zrt>E300</zrt>
</stand>
</subscription>

Anyone of you can give me tips or source codes to manage this via XSLT and XML?

Thanks in advance guys and gals!

 

httpwebwitch

WebmasterWorld Administrator httpwebwitch us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3661906 posted 5:43 pm on May 29, 2008 (gmt 0)

I follow..
1) line 2: <a_id>10</c_id> is malformed. Probably a typo.
2) I don't know where you're getting the data for <a_id>20</a_id>

besides that, I think the following ought to do it:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>

<xsl:template match="subscription">
<xsl:for-each select="usage">
<subscription>
<xsl:variable name="pos" select="position()"></xsl:variable>
<a_id>10</a_id>

<usage>
<rf_id><xsl:value-of select="rf_id"/></rf_id>
<zrt><xsl:value-of select="zrt"/></zrt>
</usage>

<stand>
<rf_id><xsl:value-of select="../stand[$pos]/rf_id" /></rf_id>
<twa><xsl:value-of select="../stand[$pos]/twa" /></twa>
<zrt><xsl:value-of select="../stand[$pos]/zrt" /></zrt>
</stand>

</subscription>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

It uses for-each to iterate through <usage> nodes, assigns the position() to a variable, then uses that variable to target the corresponding <stand>.

httpwebwitch

WebmasterWorld Administrator httpwebwitch us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3661906 posted 5:44 pm on May 29, 2008 (gmt 0)

welcome to WebmasterWorld, jvworp!

jvworp

5+ Year Member



 
Msg#: 3661906 posted 8:38 am on Jun 2, 2008 (gmt 0)

well thanks alot.. it is indeed a typo.. the tags are fictive because the original source is company secret. But your solutions works!
thanks a lot i relaly enjoy this site!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved