//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.
//SYS21///INTEGRAS/ELS/PAGINATION/ELSEVIER UK/CMF/3B2/FINALS_21-11-03/CH006.3D–58– [54–63/10]
21.11.2003 3:26PM
Here the keywordcontent=‘‘eltOnly’’means that the XML elementITEMis only permitted to contain previously defined XML elements. In this exampleITEMis defined to have a character string attribute called SHARE, and contain the XML elementsPRICEandRETURN; in that order.
The complete XDR schema for the XML filestockmarket_data.xmlis given in Code excerpt 6.3 below.
<?xml version¼‘‘1.0’’?>
<!-- This is the validation file stockmarket_data.xdr -->
<Schema xmlns¼‘‘urn:schemas—microsoft—com:xml—data’’xmlns:dt¼‘‘urn:schemas-microsoft-com:datatypes’’>
<ElementType name¼‘‘DAY’’ content¼‘‘textOnly’’ dt:type¼‘‘i4’’ />
<ElementType name¼‘‘PRICE’’ content¼‘‘textOnly’’ dt:type¼‘‘r4’’ />
<ElementType name¼‘‘RETURN’’ content¼‘‘textOnly’’ dt:type¼‘‘r4’’ />
<AttributeType name¼‘‘SHARE’’ dt:type¼‘‘string’’ required¼‘‘yes’’/>
<AttributeType name¼‘‘FULL_NAME’’ dt:type¼‘‘string’’/>
<ElementType name¼‘‘AVERAGE_RETURN’’ content¼‘‘textOnly’’ dt:type¼‘‘r4’’ />
<ElementType name¼‘‘OPTIMAL_HOLDING’’ content¼‘‘textOnly’’ dt:type¼‘‘r4’’ />
<ElementType name¼‘‘ITEM’’ content¼‘‘eltOnly’’>
<attribute type¼‘‘SHARE’’/>
<element type¼‘‘PRICE’’/>
<element type¼‘‘RETURN’’/>
</ElementType>
<ElementType name¼‘‘PORT_ITEM’’ content¼‘‘eltOnly’’>
<attribute type¼‘‘SHARE’’/>
<attribute type¼‘‘FULL_NAME’’/>
<element type¼‘‘AVERAGE_RETURN’’/>
<element type¼‘‘OPTIMAL_HOLDING’’/>
</ElementType>
<ElementType name¼‘‘DATA_REC’’ content¼‘‘eltOnly’’>
<element type¼‘‘DAY’’/>
<element type¼‘‘ITEM’’/>
</ElementType>
<ElementType name¼‘‘ALL_DATA’’ content¼‘‘eltOnly’’ order¼‘‘many’’>
<element type¼‘‘DATA_REC’’/>
</ElementType>
<ElementType name¼‘‘PORTFOLIO_ANALYSIS’’ content¼‘‘eltOnly’’>
<element type¼‘‘PORT_ITEM’’/>
</ElementType>
<ElementType name¼‘‘STOCK_DATA’’ content¼‘‘eltOnly’’ order¼‘‘seq’’>
<element type¼‘‘ALL_DATA’’/>
<element type¼‘‘PORTFOLIO_ANALYSIS’’/>
</ElementType>
</Schema>
Code excerpt 6.3 The XDRschema file,stockmarket_data.xdr, used by the XML filestockmarket_data.xml
Once we have defined the schema the XML file can be validated using it. In Figure 6.1 we show the validation error caused when stockmarket_data.xml contains following invalid XML:
<DAY>1.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>
Here the contents of the XML elementDAY, which should be a four byte integer, have instead been replaced by a floating point number.
58 Using Numerical Software Components within Microsoft Windows
//SYS21///INTEGRAS/ELS/PAGINATION/ELSEVIER UK/CMF/3B2/FINALS_21-11-03/CH006.3D–59– [54–63/10]
21.11.2003 3:26PM
6.4 XSL
In this section we will briefly describe the Extensible Stylesheet Language (XSL), and show how it can be used totransformXML files into HTML files. The transforma- tion from XML to HTML occurs dynamically as the XML file is loaded into a Web browser, and is achieved by interpreting the contents of an associated XSL file. This means the manner in which information contained in single XML file is displayed within a Web browser entirely depends on the associated XSL file. We will now describe a few of the features of XSL. It contains the usual features that one might expect, for instance there is:
Iteration through a list of items using<xsl:for-each>and variable assignment using<xsl:variable>.
<xsl:for-each select¼‘‘stock_xdr:ITEM’’>
<xsl:variable name¼‘‘v1’’ select¼‘‘@SHARE’’ />
<xsl:variable name¼‘‘v2’’ select¼‘‘stock_xdr:PRICE’’ />
</xsl:for-each>
Sets the variablev1to the value of the attributeSHAREand the variablett v2to the value contained in the child elementPRICE.
Selection from a set of alternatives using xsl:choose, output the value of a variablexsl:value-of, and evaluating expressions usingtest.
Figure 6.1 Validation error for the XML filestockmarket_data.xml; the value forDAYshould be an integer but the XML file contains the floating point number 1.1 instead
XML and transformation using XSL 59
//SYS21///INTEGRAS/ELS/PAGINATION/ELSEVIER UK/CMF/3B2/FINALS_21-11-03/CH006.3D–60– [54–63/10]
21.11.2003 3:26PM
<xsl:choose>
<xsl:when test¼‘‘$return < 0.10’’>
<td bgcolor¼‘‘pink’’ align¼‘‘center’’><xsl:value-of select¼‘‘$price’’/>
</td>
</xsl:when>
<xsl:when test¼‘‘$return > 0.20’’>
<td bgcolor¼‘‘yellow’’ align¼‘‘center’’>
<font color¼‘‘red’’>*<xsl:value-of select¼‘‘$price’’/>*</font>
</td>
</xsl:when>
<xsl:otherwise>
<td bgcolor¼‘‘yellow’’ align¼‘‘center’’><xsl:value-of select¼‘‘$price’’/>
</td>
</xsl:otherwise>
</xsl:choose>
Here if the variablereturnis less than 0.1 then the background colour of the cell is set to pink to indicate a bad share, but if value ofreturnis greater than 0.2 then the background colour of the cell is set to yellow and red stars are output to indicate that this is a good share. If the value ofreturnis between 0.1 and 0.2 then the code contained in the<xsl:otherwise>clause is executed and the background colour is just set to yellow.
It is also possible to create procedures in XSL, for instance:
<xsl:template name¼‘‘OUTPUT_ELEMENT’’>
<xsl:param name¼‘‘share’’/>
<xsl:param name¼‘‘price’’/>
<xsl:param name¼‘‘return’’/>
<xsl:choose>
<xsl:when test¼‘‘$return < 0.10’’>
<td bgcolor¼‘‘pink’’ align¼‘‘center’’><xsl: value-of select¼‘‘$price’’/>
</td>
<xsl:otherwise>
<td bgcolor¼‘‘yellow’’ align¼‘‘center’’><xsl:value-of select¼‘‘$price’’/>
</td>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
defines an XSL procedure called OUTPUT_ELEMENT with parameters share, price, andreturn. It can be called using the following syntax:
<xsl:call-template name¼‘‘OUTPUT_ELEMENT’’>
<xsl:with-param name¼‘‘share’’ select¼‘‘$v1’’/>
<xsl:with-param name¼‘‘price’’ select¼‘‘$v2’’/>
<xsl:with-param name¼‘‘return’’ select¼‘‘$v3’’/>
</xsl:call-template>
where, for instance, the parameterpriceis given the value of the XSL variable v2; the variablesv1,v2, andv3are assumed to have been set earlier in the code.