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 January 28th, 2010, 01:28 AM
Authorized User
 
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT help

Input XML 1:
<Root>
<Book>
<ID>1</ID>
<ref>regh</ref>
</Book>

<Book>
<ID>2</ID>
<ref>regh</ref>
</Book>

<Book>
<ID>3</ID>
<ref>regh</ref>
</Book>

<Book>
<ID>4</ID>
<ref>regh</ref>
</Book>
</Root>


I want an output:

<Root>
<Book>
<ID>1</ID>
<ref>regh</ref>
<NewField1>a</NewField1>
<NewField2>b</NewField2>
<NewField3>c</NewField3>
</Book>

<Book>
<ID>2</ID>
<ref>regh</ref>
<NewField1>1</NewField1>
<NewField2>2</NewField2>
<NewField3>3</NewField3>
</Book>

<Book>
<ID>3</ID>
<ref>regh</ref>
<NewField1>i</NewField1>
<NewField2>ii</NewField2>
<NewField3>iii</NewField3>
</Book>

<Book>
<ID>4</ID>
<ref>regh</ref>
<NewField1>sa</NewField1>
<NewField2>sd</NewField2>
<NewField3>sd</NewField3>
</Book>
</Root>



and have these newField values in seperate XML

where:

input XML 2

<Root>
<Book>
<ID>1</ID>
<NewField1>a</NewField1>
<NewField2>b</NewField2>
<NewField3>c</NewField3>
</Book>

<Book>
<ID>2</ID>
<NewField1>1</NewField1>
<NewField2>2</NewField2>
<NewField3>3</NewField3>
</Book>

<Book>
<ID>3</ID>
<NewField1>i</NewField1>
<NewField2>ii</NewField2>
<NewField3>iii</NewField3>
</Book>

<Book>
<ID>4</ID>
<NewField1>sa</NewField1>
<NewField2>sd</NewField2>
<NewField3>sd</NewField3>
</Book>
</Root>


So basically, I would like to apply an XSLT to this two input XMLs and get the desired output as shown above.
 
Old January 28th, 2010, 04:12 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try the below:
Code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>

<xsl:variable name="doc" select="doc('file:///file-path-of-input2/2.xml')"/>

<xsl:template match="Root">
	<xsl:copy>
<xsl:for-each select="Book">

<xsl:copy>
<xsl:variable name="id" select="ID"/>
<xsl:copy-of select="ID"/>
<xsl:copy-of select="ref"/>
<xsl:copy-of select="$doc/Root/Book[ID = $id]//NewField1"></xsl:copy-of>
<xsl:copy-of select="$doc/Root/Book[ID = $id]//NewField2"></xsl:copy-of>
<xsl:copy-of select="$doc/Root/Book[ID = $id]//NewField3"></xsl:copy-of>
	</xsl:copy>

</xsl:for-each>
</xsl:copy>
</xsl:template>
__________________
Rummy
 
Old January 28th, 2010, 04:19 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you are using XSLT 2.0, you can do this with grouping:

Code:
<xsl:for-each select="doc('d1.xml')/Root/Book, doc('d2.xml')/Root/Book"
  group-by="ID">
  <Book>
     <xsl:copy-of select="current-group()/(ID[1], ref, NewField1, NewField2, NewField3)"/>
  </Book>
</xsl:for-each>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old January 28th, 2010, 05:04 AM
Authorized User
 
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry I am using XSLT 1.0 and Rummy's Solution did not work for me.
 
Old January 28th, 2010, 06:42 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try the below:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>

<xsl:variable name="doc" select="document('file:///file-path-of-input2/2.xml')"/>

<xsl:template match="Root">
	<xsl:copy>

<xsl:for-each select="Book">
<xsl:copy>
<xsl:variable name="id" select="ID"/>
<xsl:copy-of select="ID"/>
<xsl:copy-of select="ref"/>
<xsl:copy-of select="$doc/Root/Book[ID = $id]//NewField1"></xsl:copy-of>
<xsl:copy-of select="$doc/Root/Book[ID = $id]//NewField2"></xsl:copy-of>
<xsl:copy-of select="$doc/Root/Book[ID = $id]//NewField3"></xsl:copy-of>
	</xsl:copy>
</xsl:for-each>

</xsl:copy>
</xsl:template>
__________________
Rummy
 
Old January 28th, 2010, 09:31 AM
Authorized User
 
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi rummy

it did not work. the new fields are not eing copied in the output,
 
Old January 28th, 2010, 09:36 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

See the solution posted in http://forums.tizag.com/showthread.php?t=17937
If you still have problems then you will need to tell us enough details to allow us to reproduce the problem, such as the XSLT processor you use, how you run, the result you want and the result you get.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old January 28th, 2010, 12:21 PM
Authorized User
 
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Martin,

I am using a commandline tool, which takes three input parameters, the input xml , the stylesheet, and the output file name.

The parser is : MSXML2.DOMDocument.4.0


Please let me know ,
 
Old January 28th, 2010, 12:30 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Quote:
Originally Posted by cooltechie View Post
Hi Martin,

I am using a commandline tool, which takes three input parameters, the input xml , the stylesheet, and the output file name.

The parser is : MSXML2.DOMDocument.4.0


Please let me know ,
Does that command line tool allow you to set a stylesheet parameter? You will either need to do that or you will need to edit the stylesheet and set the default value of the parameter in the stylesheet before you run the transformation.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old January 28th, 2010, 12:40 PM
Authorized User
 
Join Date: Jan 2010
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The Result I am getting is the same Input document that I am providing.

I am not seeing the Newfield Elements being added to the resulting output.

I have my input XML, xsl and the document that I am pointing to use document function, and the commandline tool all saved in the same directory.

so I am using this:


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>

<xsl:output encoding="UTF-8" method="xml"/>

<xsl:param name="dataurl" select="'book.xml'"/>
<xsl:variable name="bookdata" select="document($dataurl)"/>


<xsl:param name="dataurl" select="'book.xml'"/>
<xsl:variable name="bookdata" select="document($dataurl)"/>

<xsl:output indent="yes"/>


<xsl:key name="k1" match="book" use="ID"/>

<xsl:template match="/|@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="book">
<xsl:variable name="id" select="ID"/>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:for-each select="$bookdata/book">
<xsl:apply-templates select="key('k1', $id)/*[starts-with(local-name(), 'NewField')]"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>



<xsl:template match="NewField">

<NewField>

<xsl:value-of select="."/>
</NewField>

</xsl:template>

</xsl:stylesheet>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Another question about generating XSLT with XSLT danblick XSLT 2 July 16th, 2009 08:40 PM
General XSLT Questions in the XSLT Forum jminatel BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 0 March 31st, 2008 07:50 PM
Can XSLT read DTD/schema and Generate XSLT.. ROCXY XSLT 1 November 6th, 2006 09:39 AM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM
xslt with an xslt outputfile alleycat XSLT 4 February 20th, 2006 09:56 AM





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