Jump to content

Trying to simplify XSLT


WithoutPause

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:
<?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>
XSLT:
<?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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...