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

msg:3661908 | 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

msg:3662107 | 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

msg:3662108 | 5:44 pm on May 29, 2008 (gmt 0) | welcome to WebmasterWorld, jvworp!
|
jvworp

msg:3664662 | 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!
|
|
|