Passing xml as parameters
I have a problem with passing xml fragments as parameters.
I have several different formats. In order to make life easy, I have a
simple common message format.
A simple case is this.
So I have a xsl that transforms formatA to cmf. I have another xsl
that transforms cmf to html.
What I would like to do is to create an xsl that combines the two xsls
to make a new xsl that
goes direct from formata to html. A bit like dominos. 6:1 with a 1:3 makes a 6:3
So, I've split this into 5 files. First is a formata file that
converts formata to cmf.
Second is a html file that converts cmf to html. I then have what
appears at first sight
a couple of redundant files, formata-cmf.xsl which calls the
conversion in formata.xsl,
and another cmf-html.xsl that converts cmf to html. I then want to
combine the two conversions in
one, domino style.
However I get different behaviour when I pass on xml fragments, rather
than going through the files.
I've cut down the problem to a smallish(!?) set of files.
Any ideas?
Any other improvements welcome too!
Thanks
Nick
The first looks like this
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
exclude-result-prefixes="#all"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:import href="FormatA.xsl"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/MESSAGE">
<message>
<xsl:call-template name="formata-cmf">
<xsl:with-param name="message" select="TRADE"/>
</xsl:call-template>
</message>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
and the second looks like this
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
exclude-result-prefixes="#all"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:import href="html.xsl"/>
<xsl:output
indent="yes"
method="xml"
encoding="iso-8859-1"
doctype-public="-//W3C//DTD XHTML 2.0//EN"
doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml2.dtd"
/>
<xsl:template match="/message">
<xsl:call-template name="cmf-html">
<xsl:with-param name="message" select="trade"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
Applied in turn to a formata.xml, I get a correct cmf.xml output.
If I take that output and feed it into the cmf-html, I get the correct
answer too.
However, I now try and create a formata-html.xsl that looks like this.
This is the domino idea
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
exclude-result-prefixes="#all"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:import href="formata.xsl"/>
<xsl:import href="html.xsl"/>
<xsl:output
indent="yes"
method="xml"
encoding="iso-8859-1"
doctype-public="-//W3C//DTD XHTML 2.0//EN"
doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml2.dtd"
/>
<xsl:template match="/MESSAGE">
<xsl:variable name="cmf">
<xsl:call-template name="formata-cmf">
<xsl:with-param name="message" select="TRADE"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="cmf-html">
<xsl:with-param name="message" select="$cmf"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
I've tried the call in two ways. Above sets a variable to the cmf
fragment, and then passes it on.
However I get different behaviour from output. It's rather strange.
Here are the other files
Any ideas as to why?
Nick
======================
FormatA.xml
<?xml version="1.0" encoding="UTF-8"?>
<MESSAGE>
<TYPE>INSERT_TRADE</TYPE>
<VERSION>1.0</VERSION>
<TIME>2010-05-18 08:01:32</TIME>
<SOURCE>amba</SOURCE>
<TRADE>
<TRDNBR>99999</TRDNBR>
<PRFNBR.PRFID>Finance</PRFNBR.PRFID>
<INSADDR.ISIN>ES0264101017</INSADDR.ISIN>
<INSADDR.INSID>EUR/CPNCD/MINICE4.81 11/34 /341129/4.81</INSADDR.INSID>
<CURR.INSID>EUR</CURR.INSID>
<ACQUIRE_DAY>2010-04-23</ACQUIRE_DAY>
<VALUE_DAY>2010-04-23</VALUE_DAY>
<INSADDR.ISIN>ES0264101017</INSADDR.ISIN>
<INSTRUMENT>
<INSID>EUR/CPNCD/MINICE4.81 11/34 /341129/4.81</INSID>
<GENERIC>No</GENERIC>
<NOTIONAL>No</NOTIONAL>
<INSTYPE>INS_BOND</INSTYPE>
<ISIN>ES0264101017</ISIN>
</INSTRUMENT>
</TRADE>
</MESSAGE>
CMF.xml
<?xml version="1.0" encoding="UTF-8"?>
<message>
<trade>
<tradeid>99999</tradeid>
<currency>EUR</currency>
<acquireday>2010-04-23</acquireday>
<valueday>2010-04-23</valueday>
<portfolio>Finance</portfolio>
<instrument>
<isin>ES0264101017</isin>
<instype>BOND</instype>
</instrument>
</trade>
</message>
html.html
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 2.0//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml2.dtd">
<html>
<head>
<title>Trade</title>
<link rel="stylesheet" type="text/css" href="tradesheet.css"/>
</head>
<body>
<div id="trade">
<h2>Trade</h2>
<table summary="Table shows list of fields for a trade" border="1">
<tr>
<th id="labeltrade">label</th>
<th id="valuetrade">value</th>
</tr>
<tr>
<td>tradeid</td>
<td>99999</td>
</tr>
<tr>
<td>currency</td>
<td>EUR</td>
</tr>
<tr>
<td>acquireday</td>
<td>2010-04-23</td>
</tr>
<tr>
<td>valueday</td>
<td>2010-04-23</td>
</tr>
<tr>
<td>portfolio</td>
<td>Finance</td>
</tr>
<tr>
<td>instrument</td>
<td>
</td>
</tr>
<tr>
<td>isin</td>
<td>ES0264101017</td>
</tr>
<tr>
<td>instype</td>
<td>BOND</td>
</tr>
</table>
</div>
<div id="instrument">
<h2>Instrument</h2>
<table summary="Table shows list of fields for a instrument"
border="1">
<tr>
<th id="labelinstrument">label</th>
<th id="valueinstrument">value</th>
</tr>
<tr>
<td>instrument</td>
<td>
</td>
</tr>
<tr>
<td>isin</td>
<td>ES0264101017</td>
</tr>
<tr>
<td>instype</td>
<td>BOND</td>
</tr>
</table>
</div>
</body>
</html>
[**** Instrument section is populated ****]
FormatA.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
exclude-result-prefixes="#all"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions">
<!-- ********************************* from formata
********************************* -->
<xsl:template name="formata-cmf">
<xsl:param name="message"/>
<trade>
<tradeid>
<xsl:value-of select="$message/TRDNBR"/>
</tradeid>
<currency>
<xsl:value-of select="$message/CURR.INSID"/>
</currency>
<acquireday>
<xsl:value-of select="$message/ACQUIRE_DAY"/>
</acquireday>
<valueday>
<xsl:value-of select="$message/VALUE_DAY"/>
</valueday>
<portfolio>
<xsl:value-of select="$message/PRFNBR.PRFID"/>
</portfolio>
<xsl:call-template name="formata-instrument-cmf">
<xsl:with-param name="message" select="$message/INSTRUMENT"/>
</xsl:call-template>
</trade>
</xsl:template>
<xsl:template name="formata-instrument-cmf">
<xsl:param name="message"/>
<instrument>
<isin>
<xsl:value-of select="$message/ISIN"/>
</isin>
<instype>
<xsl:call-template name="formata-instype-cmf">
<xsl:with-param name="message" select="$message/INSTYPE"/>
</xsl:call-template>
</instype>
</instrument>
</xsl:template>
<!-- ********************************* Mappers
********************************* -->
<!-- Convert a formata day instype -->
<xsl:template name="formata-instype-cmf">
<xsl:param name="message"/>
<xsl:value-of select="fn:substring($message,5)"/>
</xsl:template>
<xsl:template name="cmf-instype-formata">
<xsl:param name="message"/>
<xsl:text>INS_</xsl:text>
<xsl:value-of select="$message"/>
</xsl:template>
</xsl:stylesheet>
html.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
exclude-result-prefixes="#all"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:template name="cmf-html">
<xsl:param name="message"/>
<html>
<head>
<title>Trade</title>
<link rel="stylesheet" type="text/css" href="tradesheet.css"/>
</head>
<body>
<xsl:call-template name="trade">
<xsl:with-param name="message" select="$message"/>
</xsl:call-template>
<xsl:call-template name="instrument">
<xsl:with-param name="message"
select="$message/instrument"/>
</xsl:call-template>
</body>
</html>
</xsl:template>
<xsl:template name="trade">
<xsl:param name="message"/>
<div id="trade">
<h2>Trade</h2>
<xsl:call-template name="table">
<xsl:with-param name="message" select="$message"/>
<xsl:with-param name="entity">trade</xsl:with-param>
</xsl:call-template>
</div>
</xsl:template>
<xsl:template name="instrument">
<xsl:param name="message"/>
<div id="instrument">
<h2>Instrument</h2>
<xsl:call-template name="table">
<xsl:with-param name="message">
<xsl:copy-of select="$message"/>
</xsl:with-param>
<xsl:with-param name="entity">instrument</xsl:with-param>
</xsl:call-template>
</div>
</xsl:template>
<xsl:template name="table">
<xsl:param name="message"/>
<xsl:param name="entity"/>
<xsl:element name="table">
<xsl:attribute name="summary">
<xsl:text>Table shows list of fields for a </xsl:text>
<xsl:value-of select="$entity"/>
</xsl:attribute>
<xsl:attribute name="border">1</xsl:attribute>
<tr>
<xsl:element name="th">
<xsl:attribute name="id">
<xsl:text>label</xsl:text>
<xsl:value-of select="$entity"/>
</xsl:attribute>
<xsl:text>label</xsl:text>
</xsl:element>
<xsl:element name="th">
<xsl:attribute name="id">
<xsl:text>value</xsl:text>
<xsl:value-of select="$entity"/>
</xsl:attribute>
<xsl:text>value</xsl:text>
</xsl:element>
</tr>
<xsl:for-each select="$message/descendant::*">
<tr>
<td>
<xsl:value-of select="name()"/>
</td>
<td>
<xsl:value-of select="text()"/>
</td>
</tr>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--