Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 22nd, 2008, 01:12 AM
Registered User
 
Join Date: Oct 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default transform XML from one form to another

Source format:
<d1 name="a1">
   <d2 name="b1">
       <d3 name="c1"/>
   </d2>
   <d2 name="b2">
       <d3 name="c2"/>
   </d2>
</d1>
<d1 name="a2">
   <d2 name="b3">
       <d3 name="c3"/>
       <d3 name="c4"/>
   </d2>
</d1>


Output format:
<row d1="a1" d2="b1" d3="c1" />
<row d1="a1" d2="b2" d3="c2" />
<row d1="a2" d2="b3" d3="c3" />
<row d1="a2" d2="b3" d3="c4" />


The twist is that the source xml is created dynamic and any tag can be used to idetify a new row to be created in output xml.In above example you can see that first part of source xml the d2 tag was used to as seperation to create a new row and in second part of source xml he tag d3 act as deciding factor to create a new row.
As the source xml is dynamic any tag from d1,d2,d3,d(n)...... can be deciding factor for creation of new row in output xml.

Is it possible using XSLT...
if yes can you help we with some good examples approaching this issue.

Regards,
Aayam



 
Old October 22nd, 2008, 03:15 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<xsl:template match="/">
  <xsl:apply-templates select="//*[not(*)]"/>
</xsl:template>

<xsl:template match="*">
  <row>
    <xsl:for-each select="ancestor-or-self::*">
      <xsl:attribute name="{name()}">
        <xsl:value-of select="@name"/>
      </xsl:attribute>
    </xsl:for-each>
  </row>
</xsl:template>

Not tested

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old October 22nd, 2008, 09:01 PM
Authorized User
 
Join Date: Sep 2008
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi kay,

where to refer to understand the concepts of
ancestor-or-self::* and so...with some examples

Raj



 
Old October 23rd, 2008, 02:04 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Default

You can refer Michael Kay's "XSLT 2.0 and XPath 2.0 Programmer's Reference".

In the above input xml, the root element was not specified. To discard the root element attribute from the result try

 <xsl:template match="/">
  <xsl:apply-templates select="//*[not(*)]"/>
</xsl:template>

<xsl:template match="*">
  <row>
    <xsl:for-each select="(ancestor-or-self::*) except (ancestor::root-element-name)">
      <xsl:attribute name="{name()}">
        <xsl:value-of select="@name"/>
      </xsl:attribute>
    </xsl:for-each>
  </row>
</xsl:template>


------
Rummy
 
Old October 23rd, 2008, 03:04 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>where to refer to understand the concepts of
ancestor-or-self::* and so...with some examples

Page 609-613 of my book, XSLT 2.0 and XPath 2.0 Programmer's Reference, 4thy edition.



Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Transform xml to xml changing one tag. surfer97301 XSLT 2 April 21st, 2010 05:14 PM
XML to XML transform (Simplified) nmahesh567 XSLT 2 March 24th, 2007 07:57 AM
XSLT read through XML to transform another XML dendenx2 XSLT 8 July 7th, 2005 08:18 PM
Transform XML to XML using XSLT Mr.D XSLT 2 September 7th, 2004 02:13 PM
XML won't transform as desired albruan XSLT 3 April 14th, 2004 04:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.