Monday, July 27, 2009

Two Input XML files as to an XSLT

/*If you want to combine two xml Files as input to XSLT file then u can use the document keyword

For example : if u have xml1 as this

*/

XML1.xml

<Persons>
<Person>
<id>1</id>
<name>Kavita</name>
<lname>Khandhadia</lname>
</Person>
</Persons>

and xml2 as

XML2.xml

<Business>
<person>
<personid>1</personid>
<BusinessName>Outsourcing</BusinessName>
<income>50k</income>
</person>
<person>
<personid>2</personid>
<BusinessName>CA</BusinessName>
<income>50k</income>
</person>
</Business>


Now if u write an transfomation like this

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<root>
<xsl:variable name="vvr_personid">
<xsl:for-each select="document('XML2.xml')/Business/person[1]">
<xsl:value-of select="personid"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="vvr_Bus">
<xsl:for-each select="document('XML2.xml')/Business/person[1]">
<xsl:value-of select="BusinessName"/>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="Persons/Person">
<xsl:variable name="vvr_Pers" select="id"/>
<xsl:if test="string($vvr_personid) = (id)">
<PersonBusiness>
<PersonName><xsl:value-of select="name"/></PersonName>
<BusinessName><xsl:value-of select="$vvr_Bus"/></BusinessName>
</PersonBusiness>
</xsl:if>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>

where the input xml is the XML1.xml

u can get output like this.


<root>
<PersonBusiness>
<PersonName>Kavita</PersonName>
<BusinessName>Outsourcing</BusinessName>
</PersonBusiness>
</root>

No comments:

Post a Comment