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 April 23rd, 2009, 11:39 AM
Authorized User
 
Join Date: Apr 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Swap the element

Hi all,

I need to swap the element like <date> element moved to <volume-nr> before, Is possible to do in xslt.....

Sample xml:

Code:
<issue>
<series>
<volume-nr>37</volume-nr>
</series>
<issue-nr>2</issue-nr>
<date>1975</date>
</issue>
Sample Change Format:

Code:
<issue>
<series>
<date>1975</date>
<volume-nr>37</volume-nr>
</series>
<issue-nr>2</issue-nr>
</issue>
I am using the xsl as below,

Code:
<xsl:template match="issue">
    <xsl:copy>
      <xsl:apply-templates select="@* | node() except (volume-nr, date)"/>
      <xsl:apply-templates select="date, volume-nr"/>
    </xsl:copy>
  </xsl:template>
It not able to swap the element exactly. Please help me ........

Thanks,
Nagaraj
 
Old April 24th, 2009, 03:54 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Default

Try this:

Code:
 
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="date"></xsl:template>

<xsl:template match="series">
<xsl:copy>
<xsl:copy-of select="../date"></xsl:copy-of>
<xsl:apply-templates></xsl:apply-templates>
</xsl:copy>
</xsl:template>

__________________
Rummy
 
Old April 25th, 2009, 11:42 AM
Authorized User
 
Join Date: Apr 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks you so much,

in this style sheet don't work this structure of xml,

Code:
<issue>
<series>
<title>
<maintitle>The author</maintitle>
</title>
<volume-nr>37</volume-nr>
</series>
<issue-nr>2</issue-nr>
<date>1975</date>
</issue>
Duplicate value of <date>1975></date> present in the out xml file when using the xslt before you posted.

Please guide me!!!!!!

Last edited by Nagaraj; April 25th, 2009 at 12:14 PM..
 
Old April 27th, 2009, 01:12 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

For the above input xml, the above code [xslt 2.0], gives the below output.

Code:
<issue>
<series>
<date>1975</date>
<title>
<maintitle>The author</maintitle>
</title>
<volume-nr>37</volume-nr>
</series>
<issue-nr>2</issue-nr>

</issue>


If this is not what you want, then post the needed output.
__________________
Rummy
 
Old April 27th, 2009, 01:35 AM
Authorized User
 
Join Date: Apr 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sorry for missing to mentioned the details,

need the out put like below coding.

<issue>
<series>
<title>
<maintitle>The author</maintitle>
</title>
<date>1975</date>
<volume-nr>37</volume-nr>
</series>
<issue-nr>2</issue-nr>

</issue>

After title element the <date> tag should come in the out put xml ........
 
Old April 27th, 2009, 01:44 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try this:

Code:
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="date"></xsl:template>

<xsl:template match="series">
<xsl:copy>
<xsl:apply-templates></xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="volume-nr">
<xsl:copy-of select="../../date"></xsl:copy-of>
<xsl:copy-of select="."></xsl:copy-of>
</xsl:template>

__________________
Rummy
 
Old April 27th, 2009, 02:13 AM
Authorized User
 
Join Date: Apr 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your codings....

Your xsl working with the copy of (to make the duplicate in the out xml), I need to move the <date> element and traverse the <date> element to add the tag afer commas..

I use your above xsl code as below,
<xsl:template match="volume-nr">
<xsl:copy-of select="../../date"></xsl:copy-of>
<xsl:copy-of select="."></xsl:copy-of>
</xsl:template>
the output xml code as below,

Code:
<issue>
<series>
<title>
<maintitle>Cognition</maintitle>
</title>
<date>1975</date><volume-nr>37</volume-nr>
</series>
(<issue-nr>2</issue-nr>): 
<date>1975</date>
</issue>
Any method to avoid the duplicate element creation?????
 
Old April 27th, 2009, 02:28 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

With the below input

Code:
<issue>
<series>
<title>
<maintitle>The author</maintitle>
</title>
<volume-nr>37</volume-nr>
</series>
<issue-nr>2</issue-nr>
<date>1975</date>
</issue>
the output produced is

Code:
<issue>
<series>
<title>
<maintitle>The author</maintitle>
</title>
<date>1975</date><volume-nr>37</volume-nr>
</series>
<issue-nr>2</issue-nr>
</issue>
using the code I posted. If it is not what you expected, then post the exact input xml and xslt code you are using.
__________________
Rummy
 
Old April 28th, 2009, 04:30 AM
Authorized User
 
Join Date: Apr 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your help,

Your code is working fine. my problem is use the below code the <date> tag making to duplicate in the out xml.

Code:
<xsl:template match="date">
<xsl:next-match/>
<xsl:text> </xsl:text>
</xsl:template>
i found the solution as the below code:

Code:
<xsl:template match="volume-nr">
<xsl:copy-of select="../../date"/>
<xsl:text> </xsl:text>
<xsl:copy-of select="."/>
</xsl:template>
Again thanks for you help.





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to swap the table column value shahidmca SQL Language 2 July 31st, 2007 06:43 PM
Swap Images Mike707 Dreamweaver (all versions) 3 December 18th, 2006 01:05 PM
swap image onclick The Beginner Javascript How-To 2 November 8th, 2006 08:42 AM
Problem to Swap two number in C# furqanms C# 1 June 5th, 2006 05:12 PM
How to Swap two integers without temp variable ramk_1978 C# 2 February 28th, 2006 08:03 PM





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