user3288815
user3288815

Reputation: 1

Problems with XSLT

I am having issues with an assignment in my XML class. We have been assigned to create an XSL document and format some pre-existing XML files. For some reason I'm not able to view my XML file and I think it has to do with an error in the XSL, but I'm not sure.

Here's the XSL File (flowers.xsl)

<?xml version="1.0" encoding="UTF-8" ?>
<!--
   New Perspectives on XML
   Tutorial 8
   Case Problem 2

   WebFlowers XSLT Style Sheet

   Filename:         flowers.xsl
   Supporting Files: customers.xml, flowers.css, items.xml
-->

<xsl:stylesheet version='1.0' xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable
name="items"
select="items.xml">
</xsl:variable>

<xsl:variable
name="customers"
select="customers.xml">
</xsl:variable>

<key id=ID name=itemID>
</key>

<key id=ID name=custID>
</key>

<key id=date name=date>
</key>

<xsl:template match="/">
   <html>
   <head>
      <title>Flower Orders</title>
      <link href="flowers.css" rel="stylesheet" type="text/css" />
   </head>
   <body>
      <h1>Flower Orders</h1>
      <table>
      <tr>
         <th>Date</th>
         <th>Customer</th>
         <th>Item</th>
         <th>Price</th>
         <th>Qty</th>
         <th>Total</th>
      </tr>
    <xsl:key name="order-by-date" match="date" use="key(date)" />
<xsl:template match="order">
    <xsl:for-each select="date[count(. | key('order-by-date', date)[1]) = 1]">
        <xsl:sort select="date" />
            </xsl:for-each>
</xsl:template>

      <tr>
         <td colspan="4" class="grand">Grand Total</td>
         <td class="grand"> sum(qty)</td>
         <td class="grand">
    <xsl:template match="totalCost" list="key(order)"
        </td>
      </tr>
      </table>
   </body>
   </html>
</xsl:template>

<xsl:template match="order">
<xsl:variable
    name="cID"
    select="customer">

</xsl:variable>

<xsl:variable
    name="iID"
    select="item">

</xsl:variable>

<xsl:variable
    name="qtyValue"
    select="qty">

</xsl:variable>


   <tr>
      <!-- Display order date -->
    <xsl:if test="position() == 1">
    <td rowspan="rows" class="date">
        date
        rows=count(<xsl:value-of select="current()"/>)
    </td>
    </xsl:if>


      <!-- Display customer information -->
    <xsl:for-each select="customers">
    <td>
    <xsl:key name="order-by-name" match="key(custID)\name" use="cID" /><br/>
    <xsl:key name="order-by-city" match="key(custID)\city" use="cID" /> <br/>,
    <xsl:key name="order-by-state" match="key(custID)\state" use="cID" /> &#160;&#160;
    <xsl:key name="order-by-zip" match="key(custID)\zip" use="cID" />
    </td>


    </xsl:for-each>

      <!-- Display item name and price-->

        <xsl:for-each select="items">
        <td>
        <xsl:key name="order-by-name" match="key(itemID)\name" use="iID" />
        </td>
        <td class="num">
        <xsl:key name="order-by-price" match="key(itemID)\price" use="iID" />
        </td>


    </xsl:for-each>


      <!-- Display item quantity -->
      <td class="num"><xsl:value-of select="@qty" /></td>  

      <!-- Display cost of items ordered -->

    <xsl:for-each select="items">

        <td class="num">
        item cost
        </td>


    </xsl:for-each>

   </tr>

   <!-- Display subtotals -->
   <xsl:if test="position()=last()">
      <tr><td colspan="5"><hr /></td></tr>
      <tr>
         <td colspan="3" class="sub">Subtotal</td>
         <td class="sub">sum(current(key(date)), current(key(qty)))</td>
         <td class="sub">
    <xsl:template match="node(key(date))" mode="rows">
    </xsl:template>
        </td>
      </tr>
      <tr><td colspan="6"><hr /></td></tr>
   </xsl:if>

</xsl:template>

<xsl:template match="/">
<xsl:param name="list"/>
<xsl:param name="total" select="0"/>

<xsl:choose>
  <xsl:when test="$list == NULL">
    <xsl:variable
name="first"
select="list">
</xsl:variable>

<xsl:variable
name="iID"
select="item">
</xsl:variable>

<xsl:variable
name="itemQty"
select="qty">
</xsl:variable>


</xsl:when>
<xsl:otherwise>
 <xsl:value-of select='format-number (total, "$#,#00.00)' />
</xsl:otherwise>
</xsl:choose>

 <xsl:for-each select="items">
    <xsl:variable
    name="itemPrice"
    select="key(itemID, iID)">
    </xsl:variable>


    </xsl:for-each>
<xsl:call-template name="totalCost">
    select= ($itemQty * $itemPrice + $total)
</xsl:call-template>
</xsl:stylesheet>

And here's one of the XML files that this stylesheet is attached to:

<?xml version="1.0" encoding="UTF-8" ?>
<!--
   New Perspectives on XML
   Tutorial 8
   Case Problem 2

   WebFlowers Items Data

   Filename:         items.xml
   Supporting Files: 
-->
<?xml-stylesheet type="text/xsl" href="flowers.xsl"?>
<items>
   <item id="F101">
      <name>Large Flower Vase Arrangement</name>
      <price>74.99</price>
   </item>
   <item id="F102">
      <name>Flowers Galore: Orchids and Roses</name>
      <price>89.99</price>
   </item>
   <item id="F103">
      <name>Bear with Dozen Roses</name>
      <price>82.99</price>
   </item>
   <item id="F104">
      <name>Medium Ivy Flower Bowl</name>
      <price>49.99</price>
   </item>
   <item id="F105">
      <name>Flower Bowl</name>
      <price>54.99</price>
   </item>
   <item id="F106">
      <name>One Dozen Red Roses</name>
      <price>64.99</price>
   </item>
   <item id="F107">
      <name>One Dozen White Roses</name>
      <price>62.99</price>
   </item>
   <item id="F108">
      <name>One Dozen Pink Roses</name>
      <price>62.99</price>
   </item>
   <item id="F109">
      <name>Fragrant Garden Basket</name>
      <price>49.99</price>
   </item>
   <item id="F110">
      <name>Classic Vase</name>
      <price>64.99</price>
   </item>
   <item id="F111">
      <name>White Flower Elegance</name>
      <price>84.99</price>
   </item>
   <item id="F112">
      <name>Flowers to Go</name>
      <price>44.99</price>
   </item>
   <item id="F113">
      <name>Bright Autumn Flowers</name>
      <price>49.99</price>
   </item>
   <item id="F114">
      <name>Pedestal Bowl</name>
      <price>49.99</price>
   </item>
   <item id="F115">
      <name>Summer Vase</name>
      <price>69.99</price>
   </item>
   <item id="F116">
      <name>Dizzy with Daisies</name>
      <price>44.99</price>
   </item>
   <item id="F117">
      <name>Fragrant Pink Lillies</name>
      <price>72.99</price>
   </item>
   <item id="F118">
      <name>Medley Basket</name>
      <price>54.99</price>
   </item>
   <item id="F119">
      <name>Romantic Vase</name>
      <price>67.99</price>
   </item>
   <item id="F120">
      <name>Spring Fling Flowers</name>
      <price>59.99</price>
   </item>
   <item id="F121">
      <name>Fragrant Lillies</name>
      <price>74.99</price>
   </item>
   <item id="F122">
      <name>Spring Flower Basket</name>
      <price>54.99</price>
   </item>
   <item id="F123">
      <name>Enchanted Roses</name>
      <price>59.99</price>
   </item>
   <item id="F124">
      <name>Large Flower Vase Arrangement</name>
      <price>159.99</price>
   </item>
   <item id="F125">
      <name>Spring Blooms</name>
      <price>54.99</price>
   </item>
   <item id="F126">
      <name>White Bear Bouquet</name>
      <price>49.99</price>
   </item>
   <item id="F127">
      <name>Golden Basket</name>
      <price>69.99</price>
   </item>
   <item id="F128">
      <name>Regal Iris Vase</name>
      <price>57.99</price>
   </item>
   <item id="F129">
      <name>Flowers of Europe</name>
      <price>69.99</price>
   </item>
   <item id="F130">
      <name>Fragrant Blooms</name>
      <price>59.99</price>
   </item>
   <item id="F131">
      <name>Pretty in Pink</name>
      <price>49.99</price>
   </item>
   <item id="F132">
      <name>Spray of Roses</name>
      <price>69.99</price>
   </item>
   <item id="F133">
      <name>Yellow Sunflowers</name>
      <price>59.99</price>
   </item>
   <item id="F134">
      <name>Bright Spring Vase</name>
      <price>43.99</price>
   </item>
   <item id="F135">
      <name>Yellow Daisy Vase</name>
      <price>49.99</price>
   </item>
   <item id="F136">
      <name>Spring Compote</name>
      <price>109.99</price>
   </item>
   <item id="F137">
      <name>Country Garden Basket</name>
      <price>59.99</price>
   </item>
   <item id="F138">
      <name>Sunflower Basket</name>
      <price>56.99</price>
   </item>
   <item id="F139">
      <name>Medium Ginger Jar</name>
      <price>44.99</price>
   </item>
   <item id="F140">
      <name>Pleasant Basket</name>
      <price>99.99</price>
   </item>
</items>

Any help would really be appreciated, be it references to the correct way to do this or just pointing out errors. Thank you in advance!

Upvotes: 0

Views: 1007

Answers (3)

Michael Kay
Michael Kay

Reputation: 163262

I think you need to look carefully at your approach to writing code and solving problems when it doesn't work.

Your errors here are very basic. That's not a problem, we all make basic errors when we start. The difference between good programmers and bad programmers is in how efficient they are at discovering the cause of the errors and fixing them.

A lot of these errors would not have happened if you were using the right tools; like a spell-checker when creating documents, a development tool that understands XSLT will flag your errors as you make them, and will offer you automatic content completion that effectively corrects the basic errors as you type. Because I'm on Mac, I use oXygen, but there are other tools available.

Also, a lot of the errors would not have happened if you had spent a few hours reading a good introduction to the language, and working through its examples, before starting to write code. I've probably written in 20 different programming languages in my time, and I would never write code in a new language without first spending an evening reading a book on the subject.

Finally, diagnostics are important! I always wonder about people who post questions saying that something didn't work, without actually saying what went wrong (e.g. error messages). Perhaps you're not seeing the error messages because you're using an inappropriate environment (e.g. running XSLT in the browser without first testing it somewhere else). Perhaps the error messages are there, but you're ignoring them because you don't understand them (and you don't understand them because you haven't spent time learning the concepts and the vocabulary by reading up about the language before you start). Or perhaps you just haven't learnt the methodology of problem solving from the evidence, of which the error messages are the most important part.

The one plus point is that you're prepared to ask for help. I would suggest, however, that asking your teachers and fellow students for help at this stage is more productive than asking the internet.

Upvotes: 1

EmileKumfa
EmileKumfa

Reputation: 414

Run your XSLT/XML through a validator to get more information on this but from my initial run, the first issue is on the key tag. Ensure you enclose the values of your attributes in quotes as shown below:

<key id="ID" name="itemID" />

<key id="ID" name="custID" />

<key id="date" name="date" />

XML validator: http://bit.ly/1nl0BFa

Upvotes: 2

user3282534
user3282534

Reputation:

The line which is referring to the xml document should be

<xsl:variable name="items" select="document(items.xml)">

instead of

<xsl:variable name="items" select="items.xml">

In case if you have kept ur xml file somewhere in other location, in that case you need to specify the full path Ex:

<xsl:variable name='ref_file' select="document('C:/../../Desktop/ref.xml')"/>

Note that you are supposed to use forward slash

/

Apart from this your code is having few more errors, so please have a look before moving further.

Upvotes: 1

Related Questions