Jump to content

Correct Use of xsl:template and xsl:for-each


Recommended Posts

Hi,

I am not a developer, but I have to deal with xslt to format xml and generate pdfs occasionally for work.

below you can see the XML and the XSL I am trying to format it with. It all worked fine until I tried to insert the second template. Obviously I am doing something wrong. I am trying to use template and for-each to add a row in the table for each <order_line_item> nested under <order_line_items> and in each of the four cells there should be the four values nested under <order_line_item> Could you help? Thanks!

I was told that you cannot nest templates, so obviously that needs correction. Could someone provide a worked out solution please?

 

XML

<hash>
    <order_line_items type="array">
        <order_line_item>
                <document_currency_unit_price_after_discount_value amount="300.0" code="GBP">£300.00</document_currency_unit_price_after_discount_value>
                <document_currency_net_after_main_discount amount="300.0" code="GBP">£300.00</document_currency_net_after_main_discount>
                <description>Subscription</description>
                <unit_quantity type="decimal">1.0</unit_quantity>
        </order_line_item>
        <order_line_item>
            <document_currency_unit_price_after_discount_value amount="300.0" code="GBP">£200.00</document_currency_unit_price_after_discount_value>
                <document_currency_net_after_main_discount amount="300.0" code="GBP">£200.00</document_currency_net_after_main_discount>
                <description>Additional Services</description>
                <unit_quantity type="decimal">1.0</unit_quantity>
        </order_line_item>
        <order_line_item>
            <document_currency_unit_price_after_discount_value amount="300.0" code="GBP">£-50.00</document_currency_unit_price_after_discount_value>
                <document_currency_net_after_main_discount amount="300.0" code="GBP">£-50.00</document_currency_net_after_main_discount>
                <description>Discount</description>
                <unit_quantity type="decimal">1.0</unit_quantity>
        </order_line_item>
    </order_line_items>
</hash>

 

XSL

<fo:table margin-bottom="5mm">
                <fo:table-column column-width="25%"/>
                <fo:table-column column-width="25%"/>
                <fo:table-column column-width="25%"/>
                <fo:table-column column-width="25%"/>
                    <fo:table-body>
                        <fo:table-row>
                        <fo:table-cell>
                            <fo:block>
                                Product
                            </fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block>
                                Quantity
                            </fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block>
                                Unit Price
                            </fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block>
                                Net Amount
                            </fo:block>
                        </fo:table-cell>
                      </fo:table-row>
                    <xsl:template match="hash/order_line_items">
                     <xsl:for-each select="order_line_item"> 
                      <fo:table-row>
                        <fo:table-cell>
                            <fo:block>
                                <xsl:value-of select="description">
                            </fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block>
                                <xsl:value-of select="document_currency_unit_price_after_discount_value">
                            </fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block>
                                <xsl:value-of select="document_currency_net_after_main_discount">
                            </fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block>
                                <xsl:value-of select="unit_quantity">
                            </fo:block>
                        </fo:table-cell>
                      </fo:table-row>
                         </xsl:for-each>
                    </xsl:template>
                </fo:table-body>
        </fo:table>
 </fo:flow>
      </fo:page-sequence>
    </fo:root>  
  </xsl:template>
 </xslt:stylesheet>

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...