p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Problem to create an xml file from two xml files (http://p2p.wrox.com/showthread.php?t=41157)

saurabh_inblore April 12th, 2006 01:17 AM

Problem to create an xml file from two xml files
 
Hi,

Please ignore my previous posting. Data was incomplete there. Following is the updated one.


I have following 2 xml files:

booklist.xml
--------------------------

  <booklist>
  <book>
  <title>C++</title>
  <author>AAAA</author>
  </book>
  <book>
  <title>XML</title>
  <author>BBBB</author>
  </book>
  <book>
  <title>Linux</title>
  <author>CCCC</author>
  </book>
  <book>
  <title>Oracle</title>
  <author>PPPP</author>
  </book>
  <book>
  <title>Java</title>
  <author>MMMM</author>
  </book>
 </booklist>

  books.xml
  -------------------
  <books>
  <book>
  <b_title>XML</b_title>
  <price>600.00</price>
  </book>
  <book>
  <b_title>C++</b_title>
  <price>300.00</price>
  </book>
  <book>
  <b_title>PHP</b_title>
  <price>500.00</price>
  </book>
  <book>
  <b_title>Python</b_title>
  <price>600.00</price>
  </book>
  <book>
  <b_title>Oracle</b_title>
  <price>1500.00</price>
  </book>


  </books>

 When the name of the title will be matched between 2 xml files
Output xml file will contain <title> from booklist.xml and <price> from
books.xml.

Output xml file will be like following:
-------------------------------------------------------------

<bookinfo>
<title>C++</title>
<price>300.00</price>
<title>XML</title>
<price>600.00</price>
<title>Oracle</title>
<price>1500.00</price>
</bookinfo>

Following xsl file I have written, but it is not working.

booklist.xsl (1st method)
------------------------------------------

  <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:variable name="btitle"
select="document('books.xml')/books/book"/>
  <xsl:template match="booklist/book">
  <bookinfo>
  <xsl:for-each select="$btitle">
  <xsl:choose>
  <xsl:when test="b_title = title">
  <title>
  <xsl:value-of select="title"/>
  </title>
  <price>
  <xsl:value-of select="$btitle/price"/>
  </price>
  </xsl:when>
  </xsl:choose>
  </xsl:for-each>
  </bookinfo>
  </xsl:template>
  </xsl:stylesheet>


  booklist.xsl (2nd method)
------------------------------------------

    <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:variable name="btitle"
select="document('books.xml')/books/book"/>
  <xsl:variable name="temp" select="document('booklist.xml')"/>
  <xsl:variable name="temp1" select="$temp/booklist/book"/>
  <xsl:template match="/">
  <bookinfo>
  <xsl:for-each select="$temp1">
  <xsl:choose>
  <xsl:when test="title = $btitle/b_title">
  <title>
  <xsl:value-of select="$temp1/title"/>
  </title>
  <price>
  <xsl:value-of select="$btitle/price"/>
  </price>
  </xsl:when>
  </xsl:choose>
  </xsl:for-each>
  </bookinfo>
  </xsl:template>

    </xsl:stylesheet>


  Any help will be highly appreciated.

  Thanks,
  Saurabh




mhkay April 12th, 2006 02:58 AM

Please don't raise the same request simultaneously on more than one forum.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference


All times are GMT -4. The time now is 10:09 PM.

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