Matching parameters

J

johkar

I need a hint (a big hint) on how to use XSL variables and parameters. It
is definitely not clicking yet. Below is my XML stripped down. There are
multiple Extension nodes and multiple FieldType nodes. Notice that the
fieldTypeCode nodes match in value. I need to pass in a fieldTypeCode
(fieldTypeCode='1') to a template which will iterate through the Extension
nodes and display the reportable Amount. I also need to do the same thing
to display the corresponding label which is the description node within
FieldType. I.E. GROSS INCOME PD 4808.76.

<?xml version="1.0" encoding="UTF-8"?>
<taxRecord>
<Extension>
<reportableText />
<reportableAmount>4808.76</reportableAmount>
<fieldTypeCode>1</fieldTypeCode>
</Extension>
<FieldType>
<description>GROSS INCOME PD</description>
<fieldTypeCode>1</fieldTypeCode>
</FieldType>
</taxRecord>

<xsl:variable name="field_type" select="fieldTypeCode"/>

<xsl:apply-templates select="taxRecord/Extension">
<xsl:with-param name="field_type" select="$field_type"/>
</xsl:apply-templates>
 
M

Marrow

Hi,

I think you've stripped down your XML a little too far ;) Sometimes when
there is only one example item it is difficult to see the overall purpose of
the data against possible meta-data.

At a guess, a slightly less stripped down XML might look like?...

== XML ========================================
<?xml version="1.0"?>
<taxRecord>
<Extension>
<reportableText />
<reportableAmount>4808.76</reportableAmount>
<fieldTypeCode>1</fieldTypeCode>
</Extension>
<Extension>
<reportableText />
<reportableAmount>1000.05</reportableAmount>
<fieldTypeCode>2</fieldTypeCode>
</Extension>
<FieldType>
<description>GROSS INCOME PD</description>
<fieldTypeCode>1</fieldTypeCode>
</FieldType>
<FieldType>
<description>INCOME TAX PD</description>
<fieldTypeCode>2</fieldTypeCode>
</FieldType>
</taxRecord>

== end of XML =================================

So if you want to list all the <Extension> element <reportableAmount> values
along with the meta data for the corresponding field types, e.g.

GROSS INCOME PD 4808.76
INCOME TAX PD 1000.05

If that is what you are trying to do - then you really don't need to use any
variables or parameters. For example...

== XSL1 =======================================
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="html" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:apply-templates select="taxRecord/Extension"/>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="Extension">
<tr>
<td>
<xsl:value-of select="/taxRecord/FieldType[fieldTypeCode =
current()/fieldTypeCode]/description"/>
</td>
<td align="right">
<xsl:value-of select="reportableAmount"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
== end of XSL1 ================================

or you could consider using keys, e.g.

== XSL2 =======================================
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="html" indent="yes"/>
<!-- key look-up for field type meta data -->
<xsl:key name="kFieldTypeLookup" match="FieldType" use="fieldTypeCode"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:apply-templates select="taxRecord/Extension"/>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="Extension">
<tr>
<td>
<xsl:value-of
select="key('kFieldTypeLookup',fieldTypeCode)/description"/>
</td>
<td align="right">
<xsl:value-of select="reportableAmount"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
== end of XSL2 ================================

Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator
 
J

johkar

Thanks, I knew I was making a mountain out of a molehill. I have ordered a
couple of XSL books. I appreciate your help.

J.

Marrow said:
Hi,

I think you've stripped down your XML a little too far ;) Sometimes when
there is only one example item it is difficult to see the overall purpose of
the data against possible meta-data.

At a guess, a slightly less stripped down XML might look like?...

== XML ========================================
<?xml version="1.0"?>
<taxRecord>
<Extension>
<reportableText />
<reportableAmount>4808.76</reportableAmount>
<fieldTypeCode>1</fieldTypeCode>
</Extension>
<Extension>
<reportableText />
<reportableAmount>1000.05</reportableAmount>
<fieldTypeCode>2</fieldTypeCode>
</Extension>
<FieldType>
<description>GROSS INCOME PD</description>
<fieldTypeCode>1</fieldTypeCode>
</FieldType>
<FieldType>
<description>INCOME TAX PD</description>
<fieldTypeCode>2</fieldTypeCode>
</FieldType>
</taxRecord>

== end of XML =================================

So if you want to list all the <Extension> element <reportableAmount> values
along with the meta data for the corresponding field types, e.g.

GROSS INCOME PD 4808.76
INCOME TAX PD 1000.05

If that is what you are trying to do - then you really don't need to use any
variables or parameters. For example...

== XSL1 =======================================
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="html" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:apply-templates select="taxRecord/Extension"/>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="Extension">
<tr>
<td>
<xsl:value-of select="/taxRecord/FieldType[fieldTypeCode =
current()/fieldTypeCode]/description"/>
</td>
<td align="right">
<xsl:value-of select="reportableAmount"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
== end of XSL1 ================================

or you could consider using keys, e.g.

== XSL2 =======================================
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="html" indent="yes"/>
<!-- key look-up for field type meta data -->
<xsl:key name="kFieldTypeLookup" match="FieldType" use="fieldTypeCode"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:apply-templates select="taxRecord/Extension"/>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="Extension">
<tr>
<td>
<xsl:value-of
select="key('kFieldTypeLookup',fieldTypeCode)/description"/>
</td>
<td align="right">
<xsl:value-of select="reportableAmount"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
== end of XSL2 ================================

Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator


johkar said:
I need a hint (a big hint) on how to use XSL variables and parameters. It
is definitely not clicking yet. Below is my XML stripped down. There are
multiple Extension nodes and multiple FieldType nodes. Notice that the
fieldTypeCode nodes match in value. I need to pass in a fieldTypeCode
(fieldTypeCode='1') to a template which will iterate through the Extension
nodes and display the reportable Amount. I also need to do the same thing
to display the corresponding label which is the description node within
FieldType. I.E. GROSS INCOME PD 4808.76.

<?xml version="1.0" encoding="UTF-8"?>
<taxRecord>
<Extension>
<reportableText />
<reportableAmount>4808.76</reportableAmount>
<fieldTypeCode>1</fieldTypeCode>
</Extension>
<FieldType>
<description>GROSS INCOME PD</description>
<fieldTypeCode>1</fieldTypeCode>
</FieldType>
</taxRecord>

<xsl:variable name="field_type" select="fieldTypeCode"/>

<xsl:apply-templates select="taxRecord/Extension">
<xsl:with-param name="field_type" select="$field_type"/>
</xsl:apply-templates>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top