Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 16th, 2003, 02:39 PM
enT enT is offline
Registered User
 
Join Date: Sep 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default help with xsl template match

hello,
i have the problem that i have an xml that contains many records. each record has a asset_name element with different values.

the xml is as follows
<table>
  <overflowed>false</overflowed>
  <maxrows>-1</maxrows>
  <record>
    <asset_name>Konto</asset_name>
    <asset_type_cd>0</asset_type_cd>
    <asset_id/>
    <asset_type/>
    <asset_value_date/>
    <asset_value/>
    <asset_value_curr_id/>
    <asset_value_curr_mnemonic/>
    <asset_value_c_curr>36004.7</asset_value_c_curr>
    <asset_curr_exchange_rate/>
    <asset_percent_of_total_value>34.78</asset_percent_of_total_value>
    <asset_children>
      <table>
        <overflowed>false</overflowed>
        <maxrows>-1</maxrows>
        <record>
          <asset_name>87598.46</asset_name>
        </record>
        <record>
          <asset_name>87598.80</asset_name>
        </record>
      </table>
    </asset_children>
  </record>
  <record>
    <asset_name>Depot</asset_name>
    <asset_type_cd>0</asset_type_cd>
    <asset_id/>
    <asset_type/>
    <asset_value_date/>
    <asset_value/>
    <asset_value_curr_id/>
    <asset_value_curr_mnemonic/>
    <asset_value_c_curr>36004.7</asset_value_c_curr>
    <asset_curr_exchange_rate/>
    <asset_percent_of_total_value>34.78</asset_percent_of_total_value>
    <asset_children>
      <table>
        <overflowed>false</overflowed>
        <maxrows>-1</maxrows>
        <record>
          <asset_name>14572.46</asset_name>
        </record>
        <record>
          <asset_name>75918.80</asset_name>
        </record>
      </table>
    </asset_children>
  </record>
  <record>
    <asset_name>Festgeld</asset_name>
    <asset_type_cd>0</asset_type_cd>
    <asset_id/>
    <asset_type/>
    <asset_value_date/>
    <asset_value/>
    <asset_value_curr_id/>
    <asset_value_curr_mnemonic/>
    <asset_value_c_curr>36004.7</asset_value_c_curr>
    <asset_curr_exchange_rate/>
    <asset_percent_of_total_value>34.78</asset_percent_of_total_value>
    <asset_children>
      <table>
        <overflowed>false</overflowed>
        <maxrows>-1</maxrows>
        <record>
          <asset_name>12345.46</asset_name>
        </record>
        <record>
          <asset_name>78945.80</asset_name>
        </record>
      </table>
    </asset_children>
  </record>
</table>

i would like to create a template where i can achieve to following result:

Konto 87598.46 87598.80
Depot 14572.46 75918.80

Total 36004.7

Festgeld 12345.46 78945.80

i have been able to create a template that matches on the table/record and print out the konto, depot and festgeld details, however i would like to know how i can go through all records and when i encounter the last record of the asset_children node to print out the total and then continue with printing out the rest of the table/record details.

this is quite urgent and the help would be very much appreciated.

kind regards

t.

Reply With Quote
  #2 (permalink)  
Old September 17th, 2003, 01:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Quote:
quote:
 i have been able to create a template that matches on the table/record and print out the konto, depot and festgeld details, however i would like to know how i can go through all records and when i encounter the last record of the asset_children node to print out the total and then continue with printing out the rest of the table/record details.
I'm confused by the output you want to get and the description of the goal you want to achieve... I see some contradiction here. When you say "the last record of the asset_children node", I understand that you mean all such nodes identified by XPath /table/record/asset_children/table/record, each of which is the last child of its parent. Then, I suppose that probably you want to print the details for each record (e.g., Konto 87598.46 87598.80) and immediately followed by the total (the content of the element "asset_value_c_curr"). Therefore, I guess you want to get something like this:

Konto 87598.46 87598.80
Total 36004.7

Depot 14572.46 75918.80
Total 36004.7

Festgeld 12345.46 78945.80
Total 36004.7


Or if you want just the output you described, then what is the value of a total? Is it the content of the "asset_value_c_curr" element, which is the child of the last /table/record? Or the total is the sum of some values but we can't see it because you put experimental values in your XML?
In any case, there is no complicated thing you want here, but you should give us a description of your goal with no ambiguity.
Try to clarify the problem, please. It's ambiguous.

Regards,
Armen
Reply With Quote
  #3 (permalink)  
Old September 17th, 2003, 02:42 AM
enT enT is offline
Registered User
 
Join Date: Sep 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Armen,
What i want is the output I posted initially, the total comes from another part of the xml namely under the <table><asset_value_curr>some value</asset_value_curr></table>. I may have put it incorrectly. I want to be able to go through all table/records and print out:
Konto 87598.46 87598.80
Depot 14572.46 75918.80

But when i reach the end of the table/record[Depot]/asset_children/table/record i want to output the total value of the two account types and then proceed to output the rest of the account type details.

Hope that clarifies things up.

Thanks

T.


Reply With Quote
  #4 (permalink)  
Old September 17th, 2003, 03:34 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Yes, it's clearer now.

Here is the stylesheet:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="all-records" select="table/record"/>
    <xsl:variable name="number-of-all-records" select="count($all-records)"/>

    <xsl:template match="/">

        <html>
            <head>
                <title>...</title>
            </head>
            <body>
                <table border="1">
                    <xsl:for-each select="$all-records">
                        <xsl:apply-templates select="."/>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="/table/record">
        <tr>
            <td>
                <xsl:value-of select="asset_name"/>
            </td>
            <td>
                <xsl:value-of select="asset_children/table/record[1]/asset_name"/>
            </td>
            <td>
                <xsl:value-of select="asset_children/table/record[2]/asset_name"/>
            </td>
        </tr>        

        <xsl:if test="count(preceding-sibling::record) = $number-of-all-records - 2">
            <tr>
                <td>
                    Total
                </td>
                <td colspan="2" align="left">
                    <xsl:value-of select="asset_value_c_curr"/>
                </td>
            </tr>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>
I've supposed that you want to put the total when you reach the table/record element which is a preceding sibling of the last table/record element. Or you can identify it by name (Depot), or something else.

Regards,
Armen
Reply With Quote
  #5 (permalink)  
Old September 18th, 2003, 01:03 AM
enT enT is offline
Registered User
 
Join Date: Sep 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Armen,
Thanks for the help. I tried your code and it works fine, however when I have more than say three parent table/records the code does not work anymore. Does the preceding-sibling::record refer to the parent table/records or the table/record/asset_children/table/record?

I dont know how many table/record or how many asset_children/table/record elements the xml will contain - it is not fixed, dynamically filled based on a db query.

I hope my question is clear.

Thank you.
T.

Reply With Quote
  #6 (permalink)  
Old September 18th, 2003, 07:11 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Ok ;)

First of all, if you don't know the exact number of table/record/asset_children/table/record elements, you have to change the second template in the stylesheet with this one:
Code:
    <xsl:template match="/table/record">
        <tr>
            <td>
                <xsl:value-of select="asset_name"/>
            </td>
            <xsl:for-each select="asset_children/table/record">
                <td>
                    <xsl:value-of select="asset_name"/>
                </td>
            </xsl:for-each>
        </tr>        

        <xsl:if test="count(preceding-sibling::record) = $number-of-all-records - 2">
            <tr>
                <td>
                    Total
                </td>
                <td colspan="2" align="left">
                    <xsl:value-of select="asset_value_c_curr"/>
                </td>
            </tr>
        </xsl:if>
    </xsl:template>
Quote:
quote:
I tried your code and it works fine, however when I have more than say three parent table/records the code does not work anymore. Does the preceding-sibling::record refer to the parent table/records or the table/record/asset_children/table/record?
Here preceding-sibling::record refers to table/record elements.
Ok, let's do the following:
I present here the XML doc which has 5 table/record elements:
what is the output you want to get? (I put distinct values in some element contents.)
Code:
<?xml version="1.0" encoding="UTF-8"?>
<table>
    <overflowed>false</overflowed>
    <maxrows>-1</maxrows>
    <record>
        <asset_name>Konto</asset_name>
        <asset_type_cd>0</asset_type_cd>
        <asset_id/>
        <asset_type/>
        <asset_value_date/>
        <asset_value/>
        <asset_value_curr_id/>
        <asset_value_curr_mnemonic/>
        <asset_value_c_curr>1001.00</asset_value_c_curr>
        <asset_curr_exchange_rate/>
        <asset_percent_of_total_value>34.78</asset_percent_of_total_value>
        <asset_children>
            <table>
                <overflowed>false</overflowed>
                <maxrows>-1</maxrows>
                <record>
                    <asset_name>87598.46</asset_name>
                </record>
                <record>
                    <asset_name>87598.80</asset_name>
                </record>
            </table>
        </asset_children>
    </record>
    <record>
        <asset_name>Depot</asset_name>
        <asset_type_cd>0</asset_type_cd>
        <asset_id/>
        <asset_type/>
        <asset_value_date/>
        <asset_value/>
        <asset_value_curr_id/>
        <asset_value_curr_mnemonic/>
        <asset_value_c_curr>1002.00</asset_value_c_curr>
        <asset_curr_exchange_rate/>
        <asset_percent_of_total_value>34.78</asset_percent_of_total_value>
        <asset_children>
            <table>
                <overflowed>false</overflowed>
                <maxrows>-1</maxrows>
                <record>
                    <asset_name>14572.46</asset_name>
                </record>
                <record>
                    <asset_name>75918.80</asset_name>
                </record>
            </table>
        </asset_children>
    </record>
    <record>
        <asset_name>Festgeld</asset_name>
        <asset_type_cd>0</asset_type_cd>
        <asset_id/>
        <asset_type/>
        <asset_value_date/>
        <asset_value/>
        <asset_value_curr_id/>
        <asset_value_curr_mnemonic/>
        <asset_value_c_curr>1003.00</asset_value_c_curr>
        <asset_curr_exchange_rate/>
        <asset_percent_of_total_value>34.78</asset_percent_of_total_value>
        <asset_children>
            <table>
                <overflowed>false</overflowed>
                <maxrows>-1</maxrows>
                <record>
                    <asset_name>12345.46</asset_name>
                </record>
                <record>
                    <asset_name>78945.80</asset_name>
                </record>
            </table>
        </asset_children>
    </record>
    <record>
        <asset_name>Poghos</asset_name>
        <asset_type_cd>0</asset_type_cd>
        <asset_id/>
        <asset_type/>
        <asset_value_date/>
        <asset_value/>
        <asset_value_curr_id/>
        <asset_value_curr_mnemonic/>
        <asset_value_c_curr>1004.00</asset_value_c_curr>
        <asset_curr_exchange_rate/>
        <asset_percent_of_total_value>34.78</asset_percent_of_total_value>
        <asset_children>
            <table>
                <overflowed>false</overflowed>
                <maxrows>-1</maxrows>
                <record>
                    <asset_name>2222.22</asset_name>
                </record>
                <record>
                    <asset_name>3333.33</asset_name>
                </record>
                <record>
                    <asset_name>4444.44</asset_name>
                </record>
            </table>
        </asset_children>
    </record>    
    <record>
        <asset_name>Petros</asset_name>
        <asset_type_cd>0</asset_type_cd>
        <asset_id/>
        <asset_type/>
        <asset_value_date/>
        <asset_value/>
        <asset_value_curr_id/>
        <asset_value_curr_mnemonic/>
        <asset_value_c_curr>1005.00</asset_value_c_curr>
        <asset_curr_exchange_rate/>
        <asset_percent_of_total_value>34.78</asset_percent_of_total_value>
        <asset_children>
            <table>
                <overflowed>false</overflowed>
                <maxrows>-1</maxrows>
                <record>
                    <asset_name>5555.22</asset_name>
                </record>
                <record>
                    <asset_name>6666.33</asset_name>
                </record>
                <record>
                    <asset_name>7777.44</asset_name>
                </record>
            </table>
        </asset_children>
    </record>    
</table>
Regards,
Armen
Reply With Quote
  #7 (permalink)  
Old September 18th, 2003, 08:01 AM
enT enT is offline
Registered User
 
Join Date: Sep 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Armen,
The output i want is as follows:

Konto 87598.46 87598.80
Depot 14572.46 75918.8
Total: some value
Festgeld 12345.46 78945.80
Poghos 2222.22 4444.44 4444.44
Petros 5555.22 6666.33 7777.44

I will always have the elements Konto and Depot within the node table/record, but when i only have two the stylesheet provided doesnt output the total. What I am trying to say is no matter how many parent table/record elements there are i want to output the total. i tested with three parent table/record elements but not total appears, i tested with 5 parent table/record elements and the total appears.

Thank you
T


Reply With Quote
  #8 (permalink)  
Old September 18th, 2003, 09:26 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Ok,

replace the second template in the stylesheet by this one:
Code:
    <xsl:template match="/table/record">
        <tr>
            <td>
                <xsl:value-of select="asset_name"/>
            </td>
            <xsl:for-each select="asset_children/table/record">
                <td>
                    <xsl:value-of select="asset_name"/>
                </td>
            </xsl:for-each>
        </tr>        
        <xsl:choose>
            <xsl:when test=" $number-of-all-records &lt; 2">
                <xsl:message terminate="yes">Error: there must be at least 2 table/record element in the XML doc</xsl:message>
            </xsl:when>
            <xsl:otherwise>


                <xsl:if test="count(preceding-sibling::record) = 1">
                    <tr>
                        <td>
                            Total
                        </td>
                        <td colspan="2" align="left">
                            <xsl:value-of select="asset_value_c_curr"/>
                        </td>
                    </tr>
                </xsl:if>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
As you say, the XML source must contain at least 2 table/record elements. The template requires this and forbids any XML doc with < 2 table/record elements.

Regards,
Armen
Reply With Quote
  #9 (permalink)  
Old September 24th, 2003, 05:06 AM
enT enT is offline
Registered User
 
Join Date: Sep 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Armen,
I always like to thank people for their help, sorry for not doing it earlier.
So once again thank you very much for your help! :o)

Kind regards
T.

Reply With Quote
  #10 (permalink)  
Old September 24th, 2003, 06:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Nor Hachin, Kotayk, Armenia.
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Hi,

It's all right enT(excuse me, I don't know your real name :) ).
Your question was really confusing for me in the beginning, since I found few interpretations of it. However, during the conversation everything became clear.
Always happy to help.

Regards,
Armen
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Param in template match=" " iceandrews XSLT 2 May 7th, 2008 07:37 AM
value-of inside template match RoeZ XSLT 5 April 17th, 2008 12:09 PM
template match doesnt match the required node Tomi XSLT 2 March 12th, 2007 06:24 AM
template m,atch doesnt match Tomi XSLT 1 March 9th, 2007 07:56 AM
xsl:template match 'overlapping' ? Kabe XSLT 1 February 25th, 2005 06:03 AM



All times are GMT -4. The time now is 12:31 AM.


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