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 February 5th, 2011, 10:24 AM
Registered User
 
Join Date: Feb 2011
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default Extract details from another xml file in XSLT

Hi

I'm parsing a xml file through Saxon which generates the structure of my website in that it produces the relevant pages, however currently all my xsl:result-document code achieves is to output the title of the page and provide a link back to my index.

What I now need to achieve is to extract the details of my products from a seperate xml file as relevant, a simplified copy of that file is below the real thing contains hundreds of records (please tell me its set out OK!).

A few comments in terms of the schema, each product:

Must have:
<productid><title><price><image><desc><stock><incl ude>

May not have:
<imagelarge> (If missing don't show a link)
<features>

May have numerous:
<features> (Display all in a list>
<include> (Display in multiple categories)

The nodes to display on the page are:
Title
Image (with link if <imagelarge> is available)
Title
Price
Stock (if in stock, a link/button 'buy')
Description
Features (if available)

Please help me my shop could be making me money! Anyone like toy cars by the way? ;) I know I know I'm doomed! ANY advice greatly appreciated.

Code:
<?xml version = "1.0" encoding = "UTF-8"?>

<catalog>
     <product>
          <productid>0001</productid>
          <title>Product 1</title>
          <price>2.99</price>
          <image>prod1.jpg</image>
          <imagelarge>prod1x.jpg</imagelarge>
          <desc>A description of product 1.</desc>
          <features>Prod1 feature</features>
          <features>Prod1 another feature</features>
          <stock>In Stock</stock>
          <include>
                    <category>This Category</category>
                    <subcategory>This Subcategory 1</subcategory>
          </include>
          <include>
                    <category>This Category</category>
                    <subcategory>This Subcategory 4</subcategory>
          </include>
     </product>
     <product>
          <productid>0002</productid>
          <title>Product 2</title>
          <price>1.99</price>
          <image>prod2.jpg</image>
          <imagelarge>prod2x.jpg</imagelarge>
          <desc>A description of product 2.</desc>
          <features>Prod2 only one feature</features>
          <stock>Out of Stock</stock>
          <include>
                    <category>That Category</category>
                    <subcategory>That Subcategory 3</subcategory>
          </include>
     </product>
</catalog>
 
Old February 5th, 2011, 01:33 PM
Registered User
 
Join Date: Feb 2011
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default

All products are listed within the same xml file, so I've been working with the document() function and produced the following code, I'm trying to look into the xml file to search through all products and pull out information for those products which match the current subcategory webpage being generated.

Code:
<xsl:template match="/">

<html>
<body>

<xsl:for-each select="document('products.xml')/catalog/product/include">
      <xsl:if test="subcategory='Diecast Cars'">
      <table>
        <tr>
           <td><xsl:value-of select="/catalog/product/title"/></td>
        </tr>
      </table>
      </xsl:if>
</xsl:for-each>
      
</body>
</html>

</xsl:template>
This is returning:

Code:
<html>
   <body>
      <table>
         <tr>
            <td>Title1 Title2 Title3</td>
         </tr>
      </table>
      <table>
         <tr>
            <td>Title1 Title2 Title3</td>
         </tr>
      </table>
      <table>
         <tr>
            <td>Title1 Title2 Title3</td>
         </tr>
      </table>
   </body>
</html>
I'm testing with just 3 products, each of them are to be included in the specified subcategory but as you can see all 3 product titles are being returned in each run.

I'm reading about xsl:key, to return unique keys but struggling to apply it, is this the best option and if so how is it set, please help?
 
Old February 5th, 2011, 03:53 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You've got to consider the context changes when you use <xsl:for-each>. So you should try something like this:

Code:
<xsl:template match="/">

<html>
<body>

<xsl:for-each select="document('products.xml')/catalog/product">
      <xsl:if test="include/subcategory='Diecast Cars'">
      <table>
        <tr>
           <td><xsl:value-of select="title"/></td>
        </tr>
      </table>
      </xsl:if>
</xsl:for-each>
      
</body>
</html>

</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
tombliboo (February 9th, 2011)
 
Old February 9th, 2011, 07:53 PM
Registered User
 
Join Date: Feb 2011
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default

This was exactly the issue thank you for your response.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Modifiying XML File using XSLT brianlan XSLT 1 October 2nd, 2007 03:14 PM
importing XML file in to xslt ashyabhi_hp XSLT 2 September 22nd, 2007 11:15 PM
Extract the XML file from CDATA haixia XSLT 2 August 3rd, 2006 02:01 PM
merge two xml file and make new xml using xslt ketan XSLT 0 September 21st, 2004 08:48 AM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM





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