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 15th, 2010, 12:36 AM
Authorized User
 
Join Date: Apr 2008
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default xsl:apply-templates usage

Hi,

I am new to XSLT and I have requirement where in I have to copy the values of one variable to another. One variable has the source xml and other variable has the xml in which the values of the first xml should be placed. Can anybody help me in coding this?

Below are the complete details:

first variable with sourcedata
<xsl:variable name="datasource">
<Author>
<name>authorname</name>
<place>authorplace</place>
<book>bookname</book>
<author>
</xsl:variable>

second xml with template that needs to be filled in with first xml
<variable name="template">
<Authordetails>
<author/>
<place/>
<book name=""/> //here the value should go in attribute
</authordetails>
</variable>

The output should look something like this:

<Authordetails>
<author>authorname</author>
<place>authorplace</place>
<book name="bookname"/>
</Authordetails

Is it possible to achieve this with <xsl:apply-templates> or something similar?
It would be great help if I can get some approach to resolve this.

Thanks,
irs
 
Old April 15th, 2010, 01:36 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try the below stylesheet:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common"
                extension-element-prefixes="exsl">
 
<xsl:output method="xml" indent="yes"/>
 
<xsl:template match="/">
 
<xsl:variable name="datasource">
<author>
<name>authorname</name>
<place>authorplace</place>
<book>bookname</book>
</author>
</xsl:variable>
 
<xsl:variable name="template">
<authordetails>
<author><xsl:value-of select="exsl:node-set($datasource)/author/name"/></author>
<place><xsl:value-of select="exsl:node-set($datasource)/author/place"/></place>
<book name="{exsl:node-set($datasource)/author/book}"/>
</authordetails>
</xsl:variable>
 
<xsl:copy-of select="$template"/>
 
</xsl:template>
__________________
Rummy

Last edited by mrame; April 15th, 2010 at 02:13 AM..
 
Old April 15th, 2010, 03:00 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I think I might tackle this by first transforming your second XML document to create a stylesheet, and then running this stylesheet with the first XML document as input. So you task becomes one of transforming

Code:
<variable name="template">
<Authordetails>
<author/>
<place/>
<book name=""/> //here the value should go in attribute
</authordetails>
</variable>
to something like

Code:
<xsl:template match="/"
  <Authordetails>
    <author><xsl:value-of select="author/name"/></author>
    <place><xsl:value-of select="author/name"/></place>
    <book name="{author/book}"/> 
  </Authordetails>
</xsl:template>
What isn't clear to me is how you know the intended mapping, for example how do you know that <author> in the output should contain the value tagged as <name> in the input?
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old April 15th, 2010, 05:54 AM
Authorized User
 
Join Date: Apr 2008
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank u for the suggestions. I think I missed some information in posting. The second XML is just a variable and I cannot edit it. during run time I have to map the fields of XMl2 with xml1. but I will be constructing the variable1 elements in the same order and naming convention as XML2. is it possible to map them during run time. Please suggest.
 
Old April 15th, 2010, 06:18 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Did you try with the solution I gave.
__________________
Rummy
 
Old April 15th, 2010, 06:19 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Well, I've told you how I would do it: I would transform the second XML to produce a stylesheet, and then execute the stylesheet. If you want to do it a different way, that's up to you.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old April 16th, 2010, 03:20 PM
Authorized User
 
Join Date: Apr 2008
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank you got the solution..!!





Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl:param and xsl:apply-templates' "select" newbieboobers XSLT 1 March 25th, 2008 07:23 PM
xsl:apply-templates select problem harapraveen XSLT 2 October 22nd, 2007 08:05 AM
differnce between xsl:apply-templates and xsl:call chandu.mca007 XSLT 2 June 12th, 2007 04:12 AM
xsl:apply-templates on Variable containing xml nexus5 XSLT 9 November 4th, 2004 02:55 PM
xsl:apply-templates - Pls help me out imme Avinash XSLT 1 November 13th, 2003 03:18 AM





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