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 December 26th, 2004, 09:00 PM
Registered User
 
Join Date: Dec 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with formatting data using xslt...urgent!

I am trying to do some data formatting with my xml.

For eg ..my xml is as below

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="stylesheet/webtemplate.xsl"?>
<Brand>
 <Phones>
 <model>
  <name>9210i</name>
  <price>$300</price>
 </model>

 <model>
  <name>9210</name>
  <price>$250</price>
 </model>

 <model>
  <name>9110</name>
  <price>$50</price>
 </model>

 <model>
  <name>8910i/7600</name>
  <price>$300</price>
 </model>

 <model>
  <name>8910</name>
  <price>$170</price>
 </model>

 <model>
  <name>8855</name>
  <price>$110</price>
 </model>

 <model>
  <name>8850/8890</name>
  <price>$80</price>
 </model>

 <model>
  <name>7110/6210</name>
  <price>$25</price>
 </model>

 <model>
  <name>8310</name>
  <price>$110</price>
 </model>

 <model>
  <name>8210</name>
  <price>$60</price>
 </model>

 <model>
  <name>8250</name>
  <price>$80</price>
 </model>

 <model>
  <name>7650</name>
  <price>$200</price>
 </model>

 <model>
  <name>7610</name>
  <price>$550</price>
 </model>

 <model>
  <name>6820</name>
  <price>$240</price>
 </model>


 <model>
  <name>6800/5100</name>
  <price>$130</price>
 </model>

 <model>
  <name>6610i</name>
  <price>$220</price>
 </model>

 <model>
  <name>6610/6100</name>
  <price>$160</price>
 </model>

 <model>
  <name>6230</name>
  <price>$410</price>
 </model>

 <model>
  <name>6510</name>
  <price>$90</price>
 </model>

 <model>
  <name>6150/6110/5110</name>
  <price>$10</price>
 </model>

 <model>
  <name>6108/3300</name>
  <price>$170</price>
 </model>

 <model>
  <name>6220/5140</name>
  <price>$230</price>
 </model>

 <model>
  <name>5210/3610/1100</name>
  <price>$60</price>
 </model>

 <model>
  <name>3660</name>
  <price>$300</price>
 </model>

 <model>
  <name>3650</name>
  <price>$250</price>
 </model>

 <model>
  <name>3530</name>
  <price>$70</price>
 </model>

 <model>
  <name>3310/3330</name>
  <price>$45</price>
 </model>
 <model>
  <name>2100</name>
  <price>$70</price>
 </model>

 <model>
  <name>N-gage QD</name>
  <price>$220</price>
 </model>
 </Phones>
</Brand>



and xslt is as below:

<xsl:stylesheet xmlns:xsl =
"http://www.w3.org/1999/XSL/Transform" version =
"1.0" >
  <xsl:output method="html" indent="yes" />

  <xsl:template match="//Phones" >
    <html>
      <head>
       <title>Yaddayadda...</title>
      </head>
      <body >
        <table border="1">
          <xsl:apply-templates />
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="model[position() mod 2 = 1]">
    <tr>
      <td><xsl:value-of select="./name" /></td>
      <td><xsl:value-of select="./price" /></td>
      <td><xsl:value-of select="following-sibling::node()[position() = 1]/name" /></td>
      <td><xsl:value-of select="following-sibling::node()[position() = 1]/price" /></td>
    </tr>
  </xsl:template>

  <xsl:template match="name"/>
  <xsl:template match="price"/>

</xsl:stylesheet>

and my current output is as shown below
::: CURRENT OUTPUT:::
xsl transformation output:
9210i $300 9210 $250
9110 $50 8910i/7600 $300
8910 $170 8855 $110
8850/8890 $80 7110/6210 $25
8310 $110 8210 $60
8250 $80 7650 $200
7610 $550 7250i $220
which is similar to the following format..
<item1> <item2>
<item3> <item4>
<item5> <item6>
<item7><item8>
<item9> <item10>

I need to change the output format as below:
:::EXPECTED OUTPUT:::
<item1> <item6>
<item2> <item7>
<item3> <item8>
<item4><item9>
<item5> <item10>

How do i do that? anybody can help..?



 
Old December 27th, 2004, 02:43 PM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

Ok, then you'd have to count how many you have so you know where to wrap..
<xsl:variable name="numModels" select="count(model)"/>
<xsl:variable name="gap">
  <xsl:choose>
    <xsl:when test="$numModels mod 2 = 1">
      <xsl:value-of select="($numModels + 1) / 2"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$numModels / 2"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

Now we apply templates to the first half, instead of the odd ones.
<xsl:apply-templates select="position() &lt;= $gap>
  <xsl:with-param name="gap"/>
</xsl:apply-templates>

Change the model template to match.
<xsl:template match="model">
  <xsl:param name="gap"/>
  <tr>
    <td><xsl:value-of select="./name" /></td>
    <td><xsl:value-of select="./price" /></td>
    <td><xsl:value-of select="following-sibling::node()[position() = $gap]/name" /></td>
      <td><xsl:value-of select="following-sibling::node()[position() = $gap]/price" /></td>
  </tr>
</xsl:template>

does this work with your xslt processor?

 
Old December 27th, 2004, 04:59 PM
Registered User
 
Join Date: Dec 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Jk...thanks for ur feedback..
I tried to integrate my xslt with ur code...i got this error "This file is not well formed"
Integrated xslt is as below::

<xsl:stylesheet xmlns:xsl =
"http://www.w3.org/1999/XSL/Transform" version = "1.0" >
  <xsl:output method="html" indent="yes" />

  <xsl:template match="//Phones" >
    <html>
      <head>
       <title>Yaddayadda...</title>
      </head>
      <body >
        <table border="1">
          <xsl:apply-templates />
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:variable name="numModels" select="count(model)"/>
    <xsl:variable name="gap">
   <xsl:choose>
    <xsl:when test="$numModels mod 2 = 1">
      <xsl:value-of select="($numModels + 1) / 2"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$numModels / 2"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<xsl:apply-templates select="position() &lt;= $gap>
  <xsl:with-param name="gap"/>
</xsl:apply-templates>

<xsl:template match="model">
  <xsl:param name="gap"/>
  <tr>
    <td><xsl:value-of select="./name" /></td>
    <td><xsl:value-of select="./price" /></td>
    <td><xsl:value-of select="following-sibling::node()[position() = $gap]/name" /></td>
      <td><xsl:value-of select="following-sibling::node()[position() = $gap]/price" /></td>
  </tr>
</xsl:template>

  <xsl:template match="name"/>
  <xsl:template match="price"/>

</xsl:stylesheet>

Could u plz help me to figure out what's wrong?

Your help is much appreciated..

Thanks,
Nayana





 
Old December 28th, 2004, 02:55 AM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

I guess I should have said where to insert the code... made a few small changes..

<xsl:stylesheet xmlns:xsl =
"http://www.w3.org/1999/XSL/Transform" version = "1.0" >
  <xsl:output method="html" indent="yes" />

  <xsl:template match="//Phones" >
    <html>
      <head>
       <title>Yaddayadda...</title>
      </head>
      <body >
        <table border="1">
         <xsl:variable name="numModels" select="count(model)"/>
         <xsl:variable name="gap">
           <xsl:choose>
             <xsl:when test="$numModels mod 2 = 1">
               <xsl:value-of select="($numModels + 1) div 2"/>
             </xsl:when>
             <xsl:otherwise>
               <xsl:value-of select="$numModels div 2"/>
             </xsl:otherwise>
           </xsl:choose>
         </xsl:variable>
         <xsl:apply-templates select="*[position() &lt;= $gap]">
           <xsl:with-param name="gap" select="$gap"/>
         </xsl:apply-templates>
       </table>
      </body>
    </html>
  </xsl:template>

<xsl:template match="model">
  <xsl:param name="gap"/>
  <tr>
    <td><xsl:value-of select="./name" /></td>
    <td><xsl:value-of select="./price" /></td>
    <td><xsl:value-of select="following-sibling::node()[position() = $gap]/name" /></td>
    <td><xsl:value-of select="following-sibling::node()[position() = $gap]/price" /></td>
  </tr>
</xsl:template>

<xsl:template match="name"/>
<xsl:template match="price"/>
</xsl:stylesheet>

 
Old December 29th, 2004, 05:09 PM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

Ivan's solution at StylusStudio is better than mine; he has changed the template into a for-each which is slightly faster as well, but may be a little less manageable in your case.

<xsl:variable name="mid" select="round(count(contentBlock/contentBlock[data/@value='top'][data/@value='new']/contentBlock) div 2)"/>

Now the next statement depends on the layout of your xml file. Are all the matching contentBlocks contained within the same parent?
<xsl:apply-templates select="contentBlock/contentBlock[data/@value='top'][data/@value='new']/contentBlock[position() &lt;= $mid]" />

--
<a href="#nf{position()}"><xsl:value-of select="." /></a>
what does this line do?? specifically <a href="#nf{position()}">???
Creates a link to #nf9 if this is the 9th element, #nf1 is this is the 1st element, etc... everything in {} is replaced with the actual number. The text displayed for this link is the same as at that element.

 
Old December 30th, 2004, 04:24 PM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

Stylus Studio forums: http://www.stylusstudio.com/SSDN/default.asp

 
Old January 4th, 2005, 06:30 PM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

I don't see any 'top' values anywhere, is that a typo??
[data/@value='top']

You need the $mid variable only if you want to display the data in 2 columns.

Best way is to try it out, what works here for me might not work for you for any number of reasons, the biggest being processor. Are you using MSXML, Saxon, Xalan?

 
Old January 5th, 2005, 03:14 PM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

That xml still doesn't match your template:
<xsl:template match="contentBlock/contentBlock[data/@value='top'][data/@value='new']/contentBlock/head">

Do you have any that have both a data element with value top, and a data element with value new?

Looks like one of the conditions is in the wrong place,

<xsl:template match="contentBlock/contentBlock[data/@value='top']/contentBlock[data/@value='new']/head"> seems more appropriate.






Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT Function very urgent alapati.sasi XSLT 3 May 23rd, 2007 03:45 AM
doubt in XSLT -- very urgent subbukns XSLT 1 May 21st, 2007 05:10 PM
Query About XSLT-Urgent harshalchoksi XSLT 6 February 23rd, 2007 06:43 AM
urgent help with xslt sonya9879 XSLT 1 August 18th, 2005 09:17 AM
XSLT HELP URGENT! pkg XSLT 3 June 22nd, 2005 01:46 AM





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