p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   How to process multiple input xml files with a single xslt ? (http://p2p.wrox.com/showthread.php?t=79311)

Divya May 5th, 2010 09:10 AM

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

Martin Honnen May 5th, 2010 09:30 AM

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>


Divya May 5th, 2010 09:50 AM

Thanks Martin.
I am using the version 1.0

Thanks,
divya

Martin Honnen May 5th, 2010 09:57 AM

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>


Divya May 5th, 2010 10:34 AM

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

Martin Honnen May 5th, 2010 12:00 PM

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>


Divya May 6th, 2010 04:41 AM

Thanks Martin

sarutis4 April 24th, 2014 08:28 AM

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?

mhkay April 24th, 2014 09:28 AM

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.

arshad May 31st, 2015 02:11 AM

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?


All times are GMT -4. The time now is 07:50 PM.

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