//SYS21///INTEGRAS/ELS/PAGINATION/ELSEVIER UK/CMF/3B2/FINALS_21-11-03/CH006.3D–54– [54–63/10]
21.11.2003 3:26PM
Chapter 6
XML and transformation using XSL
//SYS21///INTEGRAS/ELS/PAGINATION/ELSEVIER UK/CMF/3B2/FINALS_21-11-03/CH006.3D–55– [54–63/10]
21.11.2003 3:26PM
6.2 XML
An XML file containstaggedvalues; the XML elements. A simple XML element is represented as:
<TAG>VALUE</TAG>
For example a share priced at 170.0 pence and with an annual return of 0.1 could be tagged as follows:
</PRICE>170.0</PRICE><RETURN>0.1</RETURN>
This format is not very useful because we haven’t provided the name of the share.
This can be achieved by using an XML element containing an attribute. An XML element with an attribute is represented as:
<TAG1 ATTRIBUTE¼VALUE1><TAG2>VALUE2</TAG2></TAG1>
For example ifBT.Ashares are priced at 170.0 pence, with annual return of 0.1, andBPshares are priced at 440.0 pence, with an annual return of 0.18, then this can be tagged as:
<ITEM SHARE¼‘‘BT.A’’></PRICE>170.0</PRICE><RETURN>0.1</RETURN></ITEM>
<ITEM SHARE¼‘‘BP’’></PRICE>440.0</PRICE><RETURN>0.18</RETURN></ITEM>
This technique can be used to describe all the information contained in an XML file. As an example let us consider an XML file that contains daily information concerning the prices and annual returns of nine shares. The file is also assumed to contain the results of analysis which give the average annual returns and optimal holdings (based on portfolio optimization).
In Code excerpt 6.1 we give an outline of the structure of the XML file stockmarket_data.xml, which is used to contain this information. It can be seen that:
. All the information is contained within the XML elementSTOCK_DATA.
. The XML elementSTOCK_DATA is composed of the XML elementsALL_DATA andPORTFOLIO_ANALYSIS, which hold the complete share data and portfolio analysis results respectively.
. All the portfolio analysis results are contained in the XML element PORTFOLIO_ANALYSISwhich is made up of severalPORT_ITEMelements.
. All the stock market data is held in the XML elementALL_DATA. This element is made up ofDATA_REC XML elements; one for each day of stock market data.
The DATA_REC element is in turn composed of two XML elements: the single element DAY, which gives the day of the month, and an ITEM element for each share, to store the daily price andcurrentannual return.
XML and transformation using XSL 55
//SYS21///INTEGRAS/ELS/PAGINATION/ELSEVIER UK/CMF/3B2/FINALS_21-11-03/CH006.3D–56– [54–63/10]
21.11.2003 3:26PM
<STOCK_DATA>
<ALL_DATA>
<DATA_REC>
<DAY>1</DAY>
<ITEM SHARE¼string><PRICE>real</PRICE><RETURN>real</RETURN></ITEM>4
</DATA_REC>
</ALL_DATA>}
<PORTFOLIO_ANALYSIS>
<PORT_ITEM SHARE¼string FULL_NAME¼string><AVERAGE_ RETURN>real</AVERAGE_RETURN>
<OPTIMAL_HOLDING>real</OPTIMAL_HOLDING></PORT_ITEM>
</PORTFOLIO_ANALYSIS>
</STOCK_DATA>
Code excerpt 6.1 The overall structure of the filestockmarket_data.xmlused to contain both share prices information and portfolio analysis results. String values are denoted by string and floating point
numbers are denoted by real
In Code excerpt 6.2 we give a more complete code fragment of the XML file stockmarket_data.xml to show in more detail the information that is actually stored. It can be seen that the XML file makes reference to the schema file stockmarket_data.xdr. This file specifies the allowed XML elements, the order the elements occur in the file, and also the permitted data types contained within the XML elements.
<?xml version¼’1.0’?>
<!-- This file contains stock market data, and the results of portfolio optimization -->
<?xml-stylesheet type¼‘‘text/xsl’’ href¼‘‘report_style.xsl’’?>
<STOCK_DATA xmlns¼‘‘x-schema:stockmarket_data.xdr’’>
<ALL_DATA>
<DATA_REC>
<DAY>1</DAY>
<ITEM SHARE¼‘‘BT.A’’><PRICE>170.50</PRICE><RETURN>0.10</RETURN></ITEM>
<ITEM SHARE¼‘‘OOM’’><PRICE>31.73</PRICE><RETURN>0.20</RETURN></ITEM>
<ITEM SHARE¼‘‘ISYS’’><PRICE>62.15</PRICE><RETURN>0.01</RETURN></ITEM>
<ITEM SHARE¼‘‘VOD’’><PRICE>87.15</PRICE><RETURN>0.099</RETURN></ITEM>
<ITEM SHARE¼‘‘BP’’><PRICE>440.10</PRICE><RETURN>0.18</RETURN></ITEM>
<ITEM SHARE¼‘‘LGEN’’><PRICE>91.70</PRICE><RETURN>0.089</RETURN></ITEM>
<ITEM SHARE¼‘‘HSBA’’><PRICE>673.13</PRICE><RETURN>0.096</RETURN></ITEM>
<ITEM SHARE¼‘‘BARC’’><PRICE>392.23</PRICE><RETURN>0.08</RETURN></ITEM>
<ITEM SHARE¼‘‘SHEL’’><PRICE>398.75</PRICE><RETURN>0.24</RETURN></ITEM>
</DATA_REC>
</ALL_DATA>
<PORTFOLIO_ANALYSIS>
<PORT_ITEM SHARE¼‘‘BT.A’’ FULL_NAME¼‘‘BT Group’’><AVERAGE_RETURN>0.09</AVERAGE_RETURN>
<OPTIMAL_HOLDING>0.01</OPTIMAL_HOLDING></PORT_ITEM>
<PORT_ITEM SHARE¼‘‘OOM’’ FULL_NAME¼‘‘Mmo2’’><AVERAGE_RETURN>0.10</AVERAGE_RETURN>
<OPTIMAL_HOLDING>0.05</OPTIMAL_HOLDING></PORT_ITEM>
<PORT_ITEM SHARE¼‘‘ISYS’’ FULL_NAME¼‘‘Invensys’’><AVERAGE_RETURN>0.12</AVERAGE_RETURN>
<OPTIMAL_HOLDING>0.06</OPTIMAL_HOLDING></PORT_ITEM>
<PORT_ITEM SHARE¼‘‘VOD’’ FULL_NAME¼‘‘Vodaphone Group’’><AVERAGE_RETURN>0.20</AVERAGE_RETURN>
<OPTIMAL_HOLDING>0.05 </OPTIMAL_HOLDING></PORT_ITEM>
<PORT_ITEM SHARE¼‘‘BP’’ FULL_NAME¼‘‘BP Plc’’><AVERAGE_RETURN>0.20</AVERAGE_RETURN>
<OPTIMAL_HOLDING>0.28</OPTIMAL_HOLDING></PORT_ITEM>
<PORT_ITEM SHARE¼‘‘LGEN’’FULL_NAME¼‘‘Legal and General Group’’><AVERAGE_RETURN>0.11
</AVERAGE_RETURN><OPTIMAL_HOLDING>0.12</OPTIMAL_HOLDING></PORT_ITEM>
56 Using Numerical Software Components within Microsoft Windows
//SYS21///INTEGRAS/ELS/PAGINATION/ELSEVIER UK/CMF/3B2/FINALS_21-11-03/CH006.3D–57– [54–63/10]
21.11.2003 3:26PM
<PORT_ITEM SHARE¼‘‘HSBA’’ FULL_NAME¼‘‘HSBC Holdings’’><AVERAGE_RETURN>0.13</AVERAGE_RETURN>
<OPTIMAL _HOLDING>0.13</OPTIMAL_HOLDING></PORT_ITEM>
<PORT_ITEM SHARE¼‘‘BARC’’ FULL_NAME¼‘‘Barclays’’><AVERAGE_RETURN>0.14</AVERAGE_RETURN>
<OPTIMAL_HOLDING>0.05</OPTIMAL_HOLDING></PORT_ITEM>
<PORT_ITEM SHARE¼‘‘SHEL’’ FULL_NAME¼‘‘Shell Transport’’><AVERAGE_RETURN>0.21</AVERAGE_RETURN>
<OPTIMAL_HOLDING>0.25</OPTIMAL_HOLDING></PORT_ITEM>
</PORTFOLIO_ANALYSIS>
</STOCK_DATA>
Code excerpt 6.2 Fragment of the XML filestockmarket_data.xmlcontaining both share prices information and portfolio analysis results. The file uses the schema contained in
stockmarket_data.xdrand the XSL inreport_style.xsl
More detail concerning schema are given in Section 6.3 below.