DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: XML and XSL prasing issue

  1. #1
    Join Date
    Sep 2010
    Posts
    4

    XML and XSL prasing issue

    Here is my xml file
    Code:
    <ProductResponse xmlns="http://www.mywebsearch.com/services/catalog">
    <Products totalResults="2000" includedResults="10">
    <Product id="1539555448" categoryId="499">
    <title>Apple iPod touch 8GB (3rd Generation)</title>
    <description>
    Up to 1,750 songs  UP to 10 hours video  Audio Playback: Up to 30 hours  Video Playback: Up to 6 hours  Earphones, dock adapter, USB 2.0 cable  4.3 x 2.4 x 0.33 inches
    </description>
    <manufacturer>Apple</manufacturer>
    <url>
    http://www.mywebsearch.com/appleipodtouch8gb3rdgenerationpid1539555448/compareprices__rfaf1__af_assettype_id10__af_creative_id6__af_id37427__af_placement_id1.html
    </url>
    <Images>
    <Image ysize="60" xsize="60">http://mysearch.com/resize?sq=60&uid=1539555448</Image>
    <Image ysize="100" xsize="100">
    http://mysearch.com/resize?sq=100&uid=1539555448
    </Image>
    <Image ysize="160" xsize="160">
    http://mysearch.com/resize?sq=160&uid=1539555448
    </Image>
    
    <Image ysize="400" xsize="400">
    http://mysearch.com/resize?sq=400&uid=1539555448
    </Image>
    </Images>
    <Skus>
    <sku>0885909313013</sku>
    <sku>12510109</sku>
    <sku>885909313013</sku>
    <sku>MC086LL/A</sku>
    <sku>MC086LLA</sku>
    </Skus>
    <relevancy>2412893567975424.000000</relevancy>
    <Rating value="4.28">
    <Image ysize="13" xsize="80">
    http://mysearch.com/site/rating_4_and_half_star_80x13.gif
    </Image>
    </Rating>
    <PriceSet>
    <minPrice integral="14299">$142.99</minPrice>
    <maxPrice integral="24165">$241.65</maxPrice>
    <stores>17</stores>
    </PriceSet>
    </Product>
    </Products>
    </ProductResponse>
    Here is my XSL file
    Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt">
        <xsl:output method="html"/>
        <xsl:template match="/">
    	<html>
                <body>
                    <xsl:apply-templates select="ProductResponse/Products/Product"/>
                </body>
            </html>
        </xsl:template>
    	
    	    <xsl:template match="ProductResponse/Products/Product">
    		<table>
    			<tr>
     				<td><xsl:value-of select="title"/></td>
          			<td><xsl:value-of select="description"/></td>
    			</tr>
    		</table>
        </xsl:template>
        
    </xsl:stylesheet>
    </body>
    </html>
    result is not render properly can anyone please identify and correct my mistake.
    thank you in advance
    Last edited by hamanchi; 10-16-2010 at 11:29 AM.

  2. #2
    Join Date
    Oct 2008
    Posts
    142
    hi there,

    there is a problem with your xml source.

    for example,

    Code:
    <url>
    http://www.mywebsearch.com/appleipodtouch8gb3rdgenerationpid1539555448/compareprices__rfaf1__af_assettype_id10__af_creative_id6__af_id37427__af_placement_id1.html
    </url>
    which means you want to store an url as text of an xml element, and of course this is a good idea.

    however, you must keep in mind these rules:

    Entity References

    Some characters have a special meaning in XML.

    If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element.

    This will generate an XML error:

    <message>if salary < 1000 then</message>
    To avoid this error, replace the "<" character with an entity reference:

    <message>if salary &lt; 1000 then</message>
    There are 5 predefined entity references in XML:

    &lt; < less than
    &gt; > greater than
    &amp; & ampersand
    &apos; ' apostrophe
    &quot; " quotation mark
    Note: Only the characters "<" and "&" are strictly illegal in XML. The greater than character is legal, but it is a good habit to replace it.
    or better reading through entity references at --> http://www.w3schools.com/xml/xml_syntax.asp

    a good start is correcting your xml source.

    good luck,

    tonci korsano

  3. #3
    Join Date
    Sep 2010
    Posts
    4
    thank for reply but believe me i don't have any access to change anything in xml file, actually this is 3rd party xml web service.
    Their is any other way to transform.
    Waiting for you expert advice.

  4. #4
    Join Date
    May 2009
    Location
    United Kingdom
    Posts
    49
    Firstly, I agree the XML is invalid. Basically the Image URL's are invalid as '&' should be represented as '&amp;'.

    The issue with you XSLT is to do with namespaces. The root element of the specified XML declares its namespace as:
    Code:
    xmlns="http://www.mywebsearch.com/services/catalog"
    Therefore, the XPath used within your XSLT should also be aware that it is in the specified namespace.

    For the XSLT the namespace needs to be declared on the stylesheet element, something along the lines of:
    Code:
    xmlns:catalog="http://www.mywebsearch.com/services/catalog"
    Then when you are writing you XPath in the match or select statements that need to reference the chosen prefix for the declare namespace.
    e.g.
    Code:
    <xsl:apply-templates select="catalog:ProductResponse/catalog:Products/catalog:Product" />
    Finally, the XSLT file should finish with a close of the root element 'stylesheeet', not with closing tags to the body or html elements.

    So, with the following corrected and valid XML:

    Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <ProductResponse xmlns="http://www.mywebsearch.com/services/catalog">
      <Products totalResults="2000" includedResults="10">
        <Product id="1539555448" categoryId="499">
          <title>Apple iPod touch 8GB (3rd Generation)</title>
          <description>Up to 1,750 songs  UP to 10 hours video  Audio Playback: Up to 30 hours  Video Playback: Up to 6 hours  Earphones, dock adapter, USB 2.0 cable  4.3 x 2.4 x 0.33 inches</description>
          <manufacturer>Apple</manufacturer>
          <url>http://www.mywebsearch.com/appleipodtouch8gb3rdgenerationpid1539555448/compareprices__rfaf1__af_assettype_id10__af_creative_id6__af_id37427__af_placement_id1.html</url>
          <Images>
            <Image ysize="60" xsize="60">http://mysearch.com/resize?sq=60&amp;uid=1539555448</Image>
            <Image ysize="100" xsize="100">http://mysearch.com/resize?sq=100&amp;uid=1539555448</Image>
            <Image ysize="160" xsize="160">http://mysearch.com/resize?sq=160&amp;uid=1539555448</Image>
            <Image ysize="400" xsize="400">http://mysearch.com/resize?sq=400&amp;uid=1539555448</Image>
          </Images>
          <Skus>
            <sku>0885909313013</sku>
            <sku>12510109</sku>
            <sku>885909313013</sku>
            <sku>MC086LL/A</sku>
            <sku>MC086LLA</sku>
          </Skus>
          <relevancy>2412893567975424.000000</relevancy>
          <Rating value="4.28">
            <Image ysize="13" xsize="80">http://mysearch.com/site/rating_4_and_half_star_80x13.gif</Image>
          </Rating>
          <PriceSet>
            <minPrice integral="14299">$142.99</minPrice>
            <maxPrice integral="24165">$241.65</maxPrice>
            <stores>17</stores>
          </PriceSet>
        </Product>
      </Products>
    </ProductResponse>
    And the following XSLT:

    Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <xsl:stylesheet version="1.0"
      xmlns:catalog="http://www.mywebsearch.com/services/catalog"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      exclude-result-prefixes="catalog xsl msxsl">
      
      <xsl:output method="html"/>
      
      <xsl:template match="/">
        <html >
          <body>
            <xsl:apply-templates select="catalog:ProductResponse/catalog:Products/catalog:Product" />
          </body>
        </html>
      </xsl:template>
    
      <xsl:template match="catalog:ProductResponse/catalog:Products/catalog:Product">
        <table>
          <tr>
            <td>
              <xsl:value-of select="catalog:title" />
            </td>
            <td>
              <xsl:value-of select="catalog:description" />
            </td>
          </tr>
        </table>
      </xsl:template>
    
    </xsl:stylesheet>
    You will generate the following HTML output:

    Code:
    <html>
      <body>
        <table>
          <tr>
            <td>Apple iPod touch 8GB (3rd Generation)</td>
            <td>Up to 1,750 songs  UP to 10 hours video  Audio Playback: Up to 30 hours  Video Playback: Up to 6 hours  Earphones, dock adapter, USB 2.0 cable  4.3 x 2.4 x 0.33 inches</td>
          </tr>
        </table>
      </body>
    </html>
    Last edited by reedy837; 10-18-2010 at 06:18 AM.

  5. #5
    Join Date
    Oct 2008
    Posts
    142
    hi hamanchi,

    as reedy said, you will have to correct your xml source before loading it. a simple text replace that reedy pointed out will load the right xml file.
    to answer your question, using the same xpath expressions in your xslt file, you could use system.xml namespace from dotnet to make your transformations. we do it here a lot, and just like you, output is always html or embed and object tags.

    greetings from panama,

    tonci korsano

Similar Threads

  1. Xml + xsl = html 4 email ??
    By Complete in forum XML
    Replies: 0
    Last Post: 12-12-2008, 09:18 AM
  2. Why use XML?
    By Brian in forum XML
    Replies: 5
    Last Post: 01-10-2003, 09:39 PM
  3. XML and XSL transforming
    By grothlander in forum XML
    Replies: 0
    Last Post: 12-03-2001, 11:06 AM
  4. Replies: 0
    Last Post: 03-20-2001, 02:11 PM
  5. Replies: 2
    Last Post: 03-20-2001, 02:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center