Jump to content

Trying to simplify XSLT

Recommended Posts

In short, I feel like this can be simplified a lot so instead of writing out the code for each section I do a for-each instead.
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xml" href="budgets.xsl"?><?xml-stylesheet type="text/css" href="budgets.css"?><budgets>    <budget id="1">        <income>            <projected>                <base id="1">1,000</base>                <extra>0</extra>                <total>1,200</total>            </projected>            <actual>                <base id="1">1,000</base>                <extra>0</extra>                <total>1,000</total>            </actual>            <balance>                <projected>0</projected>                <actual>0</actual>                <difference>0</difference>            </balance>        </income>        // This is what gets repeated with changing tag names for each item: housing, insurance, etc.        <housing>            <mortgage>                <projected></projected>                <actual></actual>                <difference></difference>            </mortgage>        </housing>        // end of repeat    </budget><budgets>
<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    xmlns:xs="http://www.w3.org/2001/XMLSchema"    exclude-result-prefixes="xs"    version="2.0">    <xsl:template match="budgets">        <html>            <xsl:apply-templates />        </html>    </xsl:template>    <xsl:template match="budget">        <h1>Budget</h1>        <xsl:apply-templates />    </xsl:template>    <xsl:template match="income">        <div id="income">            <h3>Projected Monthly Income</h3>            <div>                Base: <xsl:value-of select="projected/base" />                Extra:<xsl:value-of select="projected/extra" />                Total: <xsl:value-of select="projected/total" />            </div>            <h3>Actual Monthly Income</h3>            <div>                Base: <xsl:value-of select="actual/base" />                Extra:<xsl:value-of select="actual/extra" />                Total: <xsl:value-of select="actual/total" />            </div>        </div>        <div id="balance">            <h3>Balance</h3>            <div>                Projected: <xsl:value-of select="balance/projected" />                Extra:<xsl:value-of select="balance/actual" />                Total: <xsl:value-of select="balance/difference" />            </div>        </div>    </xsl:template>     // Again, here is where the labels and values start to get repeated.    <xsl:template match="housing">        <table id="housing">            <tr>                <th>Housing</th>                <th>Projected Cost</th>                <th>Actual Cost</th>                <th>Difference</th>            </tr>            <tr>                <td>Mortgage</td>                <td><xsl:value-of select="mortage/projected" />0</td>                <td><xsl:value-of select="mortage/actual" />0</td>                <td><xsl:value-of select="mortage/total" />0</td>            </tr>        </table>    </xsl:template></xsl: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.

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.

  • Create New...