Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 5th, 2010, 09:10 AM
Authorized User
 
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default How to process multiple input xml files with a single xslt ?

Hi,
1.I have three xmls.typeid in the first XML must be matched with multiple occurances of id field of second xml. if it matches we have to retrieve 'rfs' attribute and put it in output xml.
2.valueid in the first xml must be matched with multiple occurances of id field of third xml. if any of the id field matches with valueid we have retrieve 'code' attribute and put it in output xml.

First XML:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<Response xmlns="http://test.com">
<Result>
<List>
<typeid>1</typeid>
<valueid>7</valueid>
<agentname>Ajay</agentname>
<Addess1>NewStreet</Addess1>
</List>
</Result>
</Response>
</soap:Body>
</soap:Envelope>

Second XML:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<Response xmlns="http://test.com">
<Result>
<info>
<id>1</id>
<rfs>Accumulation</rfs>
</info>
<info>
<id>0</id>
<rfs>Distribution</rfs>
</info>
</Result>
</Response>
</soap:Body>
</soap:Envelope>


Third XML:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<Response xmlns="http://test.com">
<Result>
<info>
<id>1</id>
<code>U</code>
</info>
<info>
<id>2</id>
<code>C</code>
</info>
<info>
<id>3</id>
<code>A</code>
</info>
<info>
<id>7</id>
<code>E</code>
</info>
</Result>
</Response>
</soap:Body>
</soap:Envelope>


Output xml should be like :

<Row>
<rfs>Accumulation</rfs>
<code>E</code>
<agentname>Ajay</agentname>
<Addess1>NewStreet</Addess1>
</Row>

Could any one please help in writing the xslt for this scenario.

Thanks,
Divya
Reply With Quote
  #2 (permalink)  
Old May 5th, 2010, 09:30 AM
Friend of Wrox
Points: 6,420, Level: 34
Points: 6,420, Level: 34 Points: 6,420, Level: 34 Points: 6,420, Level: 34
Activity: 20%
Activity: 20% Activity: 20% Activity: 20%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,194
Thanks: 0
Thanked 236 Times in 235 Posts
Default

With XSLT 2.0 and 1.0 you can use the document function http://www.w3.org/TR/xslt#document to pull in XML documents.
With XSLT 2.0 you can also use the doc function.
Here is an XSLT 2.0 stylesheet that uses the document function but makes use of the third parameter of the key function that is only available in XSLT 2.0:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xpath-default-namespace="http://test.com"
  version="2.0">
  
  <xsl:output method="xml" indent="yes"/>
  
  <xsl:param name="f2" select="'test2010050503.xml'"/>
  <xsl:param name="f3" select="'test2010050504.xml'"/>
  
  <xsl:variable name="doc2" select="document($f2)"/>
  <xsl:variable name="doc3" select="document($f3)"/>
  
  <xsl:key name="k1" match="Response/Result/info" use="id"/>
  
  <xsl:template match="Response/Result/List">
    <Row>
      <xsl:apply-templates select="key('k1', typeid, $doc2)/rfs, key('k1', typeid, $doc3)/code"/>
      <xsl:apply-templates select="agentname, Addess1"/>
    </Row>
  </xsl:template>
  
  <xsl:template match="Response/Result/info/* | Response/Result/List/*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #3 (permalink)  
Old May 5th, 2010, 09:50 AM
Authorized User
 
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks Martin.
I am using the version 1.0

Thanks,
divya
Reply With Quote
  #4 (permalink)  
Old May 5th, 2010, 09:57 AM
Friend of Wrox
Points: 6,420, Level: 34
Points: 6,420, Level: 34 Points: 6,420, Level: 34 Points: 6,420, Level: 34
Activity: 20%
Activity: 20% Activity: 20% Activity: 20%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,194
Thanks: 0
Thanked 236 Times in 235 Posts
Default

Then you need to use a prefix for the namespace and write some more code:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:t="http://test.com"
  exclude-result-prefixes="t"
  version="1.0">
  
  <xsl:output method="xml" indent="yes"/>
  
  <xsl:param name="f2" select="'test2010050503.xml'"/>
  <xsl:param name="f3" select="'test2010050504.xml'"/>
  
  <xsl:variable name="doc2" select="document($f2)"/>
  <xsl:variable name="doc3" select="document($f3)"/>
  
  <xsl:key name="k1" match="t:Response/t:Result/t:info" use="t:id"/>
  
  <xsl:template match="t:Response/t:Result/t:List">
    <Row>
      <xsl:variable name="tid" select="t:typeid"/>
      <xsl:for-each select="$doc2">
        <xsl:apply-templates select="key('k1', $tid)/t:rfs"/>
      </xsl:for-each>
      <xsl:for-each select="$doc3">
        <xsl:apply-templates select="key('k1', $tid)/t:code"/>
      </xsl:for-each>
      <xsl:apply-templates select="t:agentname | t:Addess1"/>
    </Row>
  </xsl:template>
  
  <xsl:template match="t:Response/t:Result/t:info/* | t:Response/t:Result/t:List/*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #5 (permalink)  
Old May 5th, 2010, 10:34 AM
Authorized User
 
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Martin,

Thank u.
Here in the first xml valueid is 7 .it must be matched with id field of Third xml then it should return Code as E but with xslt its returning 'U'.

A slight modification to the output file
<Row>
<fundcategory>Accumulation</fundcategory>
<type>E</type>
<agentname>Ajay</agentname>
<Addess1>NewStreet</Addess1>
</Row>

only the tag names changed to fund category and type instead of rfs and code respectively
Reply With Quote
  #6 (permalink)  
Old May 5th, 2010, 12:00 PM
Friend of Wrox
Points: 6,420, Level: 34
Points: 6,420, Level: 34 Points: 6,420, Level: 34 Points: 6,420, Level: 34
Activity: 20%
Activity: 20% Activity: 20% Activity: 20%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,194
Thanks: 0
Thanked 236 Times in 235 Posts
Default

Here is an adaption of the stylesheet:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:t="http://test.com"
  exclude-result-prefixes="t"
  version="1.0">
  
  <xsl:output method="xml" indent="yes"/>
  
  <xsl:param name="f2" select="'test2010050503.xml'"/>
  <xsl:param name="f3" select="'test2010050504.xml'"/>
  
  <xsl:variable name="doc2" select="document($f2)"/>
  <xsl:variable name="doc3" select="document($f3)"/>
  
  <xsl:key name="k1" match="t:Response/t:Result/t:info" use="t:id"/>
  
  <xsl:template match="t:Response/t:Result/t:List">
    <Row>
      <xsl:variable name="tid" select="t:typeid"/>
      <xsl:for-each select="$doc2">
        <xsl:apply-templates select="key('k1', $tid)/t:rfs"/>
      </xsl:for-each>
      <xsl:variable name="vid" select="t:valueid"/>
      <xsl:for-each select="$doc3">
        <xsl:apply-templates select="key('k1', $vid)/t:code"/>
      </xsl:for-each>
      <xsl:apply-templates select="t:agentname | t:Addess1"/>
    </Row>
  </xsl:template>
  
  <xsl:template match="t:Response/t:Result/t:info/t:rfs">
    <fundcategory>
      <xsl:apply-templates/>
    </fundcategory>
  </xsl:template>

  <xsl:template match="t:Response/t:Result/t:info/t:code">
    <type>
      <xsl:apply-templates/>
    </type>
  </xsl:template>
  
  <xsl:template match="t:Response/t:Result/t:List/*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #7 (permalink)  
Old May 6th, 2010, 04:41 AM
Authorized User
 
Join Date: Apr 2010
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks Martin
Reply With Quote
  #8 (permalink)  
Old April 24th, 2014, 08:28 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have similar problem, I also use 1.0 version but i don't known prefix because it shouldn't show in xml and i use local-name() function for all node does it still will work?
Reply With Quote
  #9 (permalink)  
Old April 24th, 2014, 09:28 AM
mhkay's Avatar
Wrox Author
Points: 17,773, Level: 58
Points: 17,773, Level: 58 Points: 17,773, Level: 58 Points: 17,773, Level: 58
Activity: 60%
Activity: 60% Activity: 60% Activity: 60%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,839
Thanks: 0
Thanked 267 Times in 262 Posts
Default

Start a new thread explaining your problem from first principles. You can't expect us to read a four-year-old thread describing a problem which is similar to (but presumably not the same as) your own. You'll get better help if you take some trouble over asking your questions.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple inputs: process all files in directory stolte XSLT 2 March 22nd, 2010 02:41 PM
Multiple FOP files to single PDF using XSL bhavanimachani XML 7 March 28th, 2008 05:59 AM
process all xml files in folder lsantos2000 XSLT 4 November 8th, 2007 12:41 PM
Multiple Input files? edubble XSLT 5 July 31st, 2007 03:42 AM
Save (output to) multiple files from single click moo_desanta Classic ASP Basics 2 June 23rd, 2006 02:00 AM



All times are GMT -4. The time now is 02:50 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.