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 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
 
Old May 5th, 2010, 09:30 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 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
 
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
 
Old May 5th, 2010, 09:57 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 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
 
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
 
Old May 5th, 2010, 12:00 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 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
 
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
 
Old April 24th, 2014, 08:28 AM
Registered User
 
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?
 
Old April 24th, 2014, 09:28 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
 
Old May 31st, 2015, 02:11 AM
Registered User
 
Join Date: May 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to give two responses using single xslt.

Response:
<sear:searchCustomerSummaryWithContactResponse xmlns:sear="http://www.example.org/SearchCustomerSummaryWithContact/">
<returnedCustomers>
<externalID>S2640296D</externalID>
<idType>2</idType>
<GeographicAddress>
<locationDesc>string</locationDesc>
<floorLvlUnitDesc>string</floorLvlUnitDesc>
<floor>#11-07</floor>
<unitNumber>#11-07</unitNumber>
<block>1</block>
<blockNumber>93</blockNumber>
<buildingName>raam</buildingName>
<devName>ORCHID PARK CONDO</devName>
<postalCode>768451</postalCode>
<street>YISHUN ST 81</street>
<streetNumber>YISHUN ST 81</streetNumber>
<country>SGP</country>
<state/>
<localityNm/>
</GeographicAddress>
<contactMean>
<locationDesc>string</locationDesc>
<floorLvlUnitDesc>string</floorLvlUnitDesc>
<floor>#11-07</floor>
<unitNumber>#11-07</unitNumber>
<block>1</block>
<blockNumber>93</blockNumber>
<buildingName>raam</buildingName>
<devName>ORCHID PARK CONDO</devName>
<postalCode>768451</postalCode>
<street>YISHUN ST 81</street>
<streetNumber>YISHUN ST 81</streetNumber>
<country>SGP</country>
<state/>
<localityNm/>
</contactMean>
<birthdate>24/03/1952</birthdate>
<formattedName>BEK CHEE YIANG</formattedName>
<familyName>Adapa</familyName>
<givenName>Ram</givenName>
<emailAddress>adapacmram@gmail.com</emailAddress>
<gender>M</gender>
<nationality>Indian</nationality>
<salutation>MR</salutation>
<race>A</race>
</returnedCustomers>
</sear:searchCustomerSummaryWithContactResponse>


XSLT for the above one is:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:v2="http://group.singtel.com/app/mcash/mcashcustomerprofilesearch/v2_0" xmlns:v21="http://group.singtel.com/app/mcash/mcashcustomerprofilesearchschema/v2_0">
<xsl:template match="/">

<v2:getCustomerInfoResponse>
<xsl:for-each select="sear:searchCustomerSummaryWithContactRespo nse" xmlns:sear="http://www.example.org/SearchCustomerSummaryWithContact/">

<v21:customerDetails>
<xsl:for-each select="returnedCustomers">

<v21:acctNo><xsl:value-of select="externalID"/></v21:acctNo>
<v21:acctNoType><xsl:value-of select="idType"/></v21:acctNoType>

<v21:customerAddress>
<xsl:for-each select="GeographicAddress">
<v21:locationDesc><xsl:value-of select="locationDesc"/></v21:locationDesc>
<v21:floorLvlUnitDesc><xsl:value-of select="floorLvlUnitDesc"/></v21:floorLvlUnitDesc>
<v21:floorLvlUnitNo><xsl:value-of select="floorunitNumber"/></v21:floorLvlUnitNo>
<v21:blkHseDesc><xsl:value-of select="block"/></v21:blkHseDesc>
<v21:blkHseNo><xsl:value-of select="blockNumber"/></v21:blkHseNo>
<v21:bldgNm><xsl:value-of select="buildingName"/></v21:bldgNm>
<v21:devNm><xsl:value-of select="devName"/></v21:devNm>
<v21:postalCd><xsl:value-of select="postalCode"/></v21:postalCd>
<v21:streetNm><xsl:value-of select="streetName"/></v21:streetNm>
<v21:country><xsl:value-of select="country"/></v21:country>
<v21:state><xsl:value-of select="state"/></v21:state>
<v21:localityNm><xsl:value-of select="localityNm"/></v21:localityNm>
</xsl:for-each>
</v21:customerAddress>

<v21:billingAddress>
<xsl:for-each select="contactMean" >
<v21:locationDesc><xsl:value-of select="locationDesc"/></v21:locationDesc>
<v21:floorLvlUnitDesc><xsl:value-of select="floorLvlUnitDesc"/></v21:floorLvlUnitDesc>
<v21:floorLvlUnitNo><xsl:value-of select="floorunitNumber"/></v21:floorLvlUnitNo>
<v21:blkHseDesc><xsl:value-of select="block"/></v21:blkHseDesc>
<v21:blkHseNo><xsl:value-of select="blockNumber"/></v21:blkHseNo>
<v21:bldgNm><xsl:value-of select="uildingName"/></v21:bldgNm>
<v21:devNm><xsl:value-of select="devName"/></v21:devNm>
<v21:postalCd><xsl:value-of select="postalCode"/></v21:postalCd>
<v21:streetNm><xsl:value-of select="treetName"/></v21:streetNm>
<v21:country><xsl:value-of select="country"/></v21:country>
<v21:state><xsl:value-of select="state"/></v21:state>
<v21:localityNm><xsl:value-of select="localityNm"/></v21:localityNm>
</xsl:for-each>
</v21:billingAddress>

<v21:dateOfBirth><xsl:value-of select="birthdate"/> </v21:dateOfBirth>
<v21:nm><xsl:value-of select="formattedName"/> </v21:nm>
<v21:lastNm><xsl:value-of select="familyName"/> </v21:lastNm>
<v21:firstName><xsl:value-of select="givenName"/> </v21:firstName>
<v21:emailAddr><xsl:value-of select="emailAddress"/> </v21:emailAddr>
<v21:gender><xsl:value-of select="gender"/> </v21:gender>
<v21:nationality><xsl:value-of select="nationality"/> </v21:nationality>
<v21:salutation><xsl:value-of select="salutation"/> </v21:salutation>
<v21:race><xsl:value-of select="race"/> </v21:race>

</xsl:for-each>
</v21:customerDetails>

</xsl:for-each>
</v2:getCustomerInfoResponse>

</xsl:template>
</xsl:stylesheet>

but i need to include fault response in it:
<sear:searchCustomerSummaryWithContactResponse xmlns:sear="http://www.example.org/SearchCustomerSummaryWithContact/">
<status>false</status>
<errCode>40015</errCode>
<errMessage>Generic exception encountered</errMessage>
</sear:searchCustomerSummaryWithContactResponse>



how to include it in the same xslt?





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 01: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





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