Jump to content

How can i archieve?


ohayo85

Recommended Posts

Hi, I'm student which newbie to XML and XSLT. I have a problem in displaying the data in a proper way in table format. Currently i try to achieve the <price> can be display beside the <description>.Suppose i have a XML file:..<item> <description type="1">SOME DATA<description> <description type="2">SOME DATA<description> <description type="3">SOME DATA<description></item><prices> <price type="1">PRICE FOR DESCRIPTION TYPE 1</price> <price type="2">PRICE FOR DESCRIPTION TYPE 2</price> <price type="3">PRICE FOR DESCRIPTION TYPE 3</price></prices>..And my XSLT stylesheet is:<table border="1"><tr> <td> <ul> <xsl:for-each select="item"> <xsl:for-each select="*"> <tr> <td> <li><xsl:value-of select="."/></li> <----------- list all the child element in item </td> <td> (what should i write here?) <----As i know the prices should be child of item element because item element is looping here but if i insert <prices> into item element, it will show together with description. </td> </tr> </xsl:for-each> </xsl:for-each> </ul> </td></tr></table>And the output i trying to achieve is:-----------------------------------------------------------------------* SOME DATA (description type="1") | DATA (price type="1")|-----------------------------------------------------------------------* SOME DATA (description type="2") | DATA (price type="2")| -----------------------------------------------------------------------* SOME DATA (description type="3") | DATA (price type="3")|-----------------------------------------------------------------------Is there any better way to store the <price> in XML? boen_robot have recommended me use <xsl:for-each select="*[starts-with(local-name(),'description')]">to list all the child element in <item> which is better way but i don't understand how to use this statement. I'm stuck here.Anyone can help by providing some example? so i enable to understand it :)

Link to comment
Share on other sites

You have adjusted your XML as per my earlier suggestion. You don't need what I suggested below it, which was in case you can't change it.Now that you have this flexible strcutre, you can simply do:

<table border="1"><xsl:for-each select="item/description"><tr> <td> <xsl:value-of select="."/></td> <td> <xsl:value-of select="../../prices/price[@type = current()/@type]"/></td></tr></xsl:for-each></table>

This would have been (almost) impossible with your initial XML.

Link to comment
Share on other sites

I had tried your suggestion:<table border="1"><xsl:for-each select="item/description"><tr> <td> <xsl:value-of select="."/></td> <td> <xsl:value-of select="../../prices/price[@type = current()/@type]"/></td></tr></xsl:for-each></table>but the price still not showing :) Perhaps i show you mine XML and XSLT stylesheet so you can suggest what's wrong in my coding.<root> <computer> <picture>data.jpg</picture> <recommend>data</recommend> <productName>data</productName> <description>DATA DATAdescription> <promos> <promo type="a">DATA</promo> <promo type="b">DATA</promo> <promo type="c">DATA</promo> <promo type="d">DATA</promo> </promos> <item_spec> <item type="AA">DATA</item> <item type="BB">DATA</item> <item type="CC">DATA</item> <item type="DD"> DATA</item> <item type="EE">DATA</item> <item type="FF">DATA</item> </item_spec> <upgrades><upgrade type="1">DATA</upgrade><upgrade type="2">DATA</upgrade><upgrade type="3">DATA</upgrade> </upgrades> <prices><price type="1">191</price><price type="2">111</price><price type="3">110</price> </prices><rm>DATA</rm><overall_price>4,879</overall_price></computer> </root>...continue on with others.and my XSLT stylesheet<xsl:for-each select="computer"> <xsl:if test="position() mod 2= 1"> <table border="1"> <colgroup span="3"> <col width="150"/> <col width="500"/> <col width="300"/> </colgroup> <tr> <td> <xsl:element name="img"> <xsl:attribute name="src"> <xsl:value-of select="picture"/> </xsl:attribute> </xsl:element> </td> <td> <span style="font-weight:bold;font-size:14pt;color:green"><xsl:value-of select="productName"/></span> <br/> <span style="font-size:10pt;color:green"><xsl:value-of select="description"/></span> <xsl:for-each select="item_spec"> <ul> <xsl:for-each select="*"> <li><xsl:value-of select="."/></li> </xsl:for-each> </ul> </xsl:for-each> </td> <td> <table border="1"> <xsl:for-each select="promos"> <td align="center"> <span style="font-size:8pt"><xsl:value-of select="."/></span> <xsl:text> </xsl:text> </td> </xsl:for-each> </table> <span style="font-weight:bold;color:green"><xsl:value-of select="rm"/></span> <span tyle="position:relative;top:1ex;font-weight:bold;font-size:24pt;color:green"> <xsl:value-of select="overall_price"/> </span> <br/> <br/> <span style="font-weight:bold;font-size:8pt;color:green"><xsl:value-of select="recommend"/></span> <table border="1"> <tr> <td> <ul> <xsl:for-each select="upgrades/upgrade"> <tr> <td> <li><span style="font-size:10pt"><xsl:value-of select="."/></span></li> </td> <td> <xsl:value-of select="root/computer/prices/price[@type=current()/@type]"/> </td> </tr> </xsl:for-each> </ul> </td> </tr> </table> </td> </tr> </table></xsl:if>...continue on with same but change with <xsl:if test="position() mod 2= 1">The code with highlight color is my part which isn't showing the price value. As i know it can't show the price because is inside the "upgrades/upgrade" looping and my prices is not child element of the upgrades element. The <prices> is the price of the <upgrades> [for example, upgrade type=1 with price type=1].Anyone can help? I'm stuck :) Thanks boen_robot for replying.Thanks for advance.

Link to comment
Share on other sites

By the way, another questionI validate my xml files and it's gime my error like this: "Can not find declaration of element 'root'." . I follow others example and refer to w3school. What's the problem? Anyone help would be appreciated. :) Thanks in advance.

Link to comment
Share on other sites

Oh. What I showed doesn't work, and won't with this type of XML. I thought your descriptions are associated with the prices by having a number to label each of them (the "type" attribute), but you seem to also have litteral values, so that won't work.The technique can easily be applied to the "upgrade" elements instead of the "description" elements, because they have the same numbers.Now, unless you provide some sort of explicitly typed reference like the type attribute (i.e. a "key"), how exactly can YOU know what description is associated with a certain price? If you can't tell, there's no way to tell a machine either.As for the validation, do you have the <root/> element declared in the schema? If not, take it out, and use <computer/> as your root element.Last, but not least, please stop taking everything I say literally. Try to undestand the hows. In my example before, I used <root/> because I didn't know what was the root element you are using. The fact I chosed it for that example doesn't mean that's the only element that may occur at that spot.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...