在一个大型的项目中往往用到好几种开发语言,例如C# Java PHP 。。。
那么如何更好更方便的实现这几种语言之间的相互通讯和结合呢,我下面来介绍一种新的模式,当然已经有很多人用过,但我还是要罗嗦一下:
通过XML进行交互,每种语言设计的对象类都可以转化为内存中的xml对象,然后每种语言都可以对xml对象进行操作与交互。
最近做了一个项目就是改造以前的程序提升性能,由于以前页面上的构造非常乱而且杂,有从Mysql数据库取数据的,也有从SqlServer数据库取数据的,而且数据库分布在多个服务器上,由于每个项目组之间的协调不够所以在读取数据上有读取重复数据的情况,大大浪费了资源。一个页面上访问数据库很多次,所以造成访问速度非常慢。我们现在设计了一个模式,就是一次性从数据库加载所有的数据然后转化为内存中的xml对象,然后用xsl对数据包装转化为一个带有样式的Html,所有的Js中变量都一次性打到页面上以便使用。
下面介绍几个使用xsl的方法:
<?xml version="1.0"?>
<Data>
<ME>
<UserID>1</UserID>
<BasicInfo>
<UserID>1</UserID>
<Gender>true</Gender>
<Age>27</Age>
<UserName>Eric Liu</UserName>
<Height>184</Height>
<Marriage>1</Marriage>
<Education>2</Education>
<Income>5</Income>
<City>861105</City>
<Desc>南方人了,普通话说不好,人比较笨,父母是农民,自小放牛、插秧、割稻、砍柴都干过了。自己条件也不知道算好还是差,竹竿一只,不过各个部件还算运转正常,长得不好看,父母也没有对不起我了,就是一般一般那种。没车没房,唉,一说这条全国人民都跑了,在别人看来,是在一家还算不错的公司一个也算不错的职位,应该也会有不错的前景。性格比较温和,也比较迷糊,在百合这里就是碰,看看是否可以碰到了。不过我想看完我的介绍,估计所有的鸭子都飞了,唉,坦白也是一种错嘛?
如果有阶层观念或者对父母不是很好的就别和我说话了,我从事IT相关的,但是平时也会给媒体供稿,每天晚上一碗面条就将我养活了</Desc>
<LoveType>12</LoveType>
<PhotoUrl>SW1hZ2VHVUlEJDF8UERUQnwyMDA2XDhcMVx8MjAzMmI1OTYtZDM5Mi00YWE5LTg5YjEtNDU0MDJj
ZjhjNThlfEpQRUc=</PhotoUrl>
<MinAge>21</MinAge>
<MaxAge>26</MaxAge>
<MinHeight>166</MinHeight>
<MaxHeight>180</MaxHeight>
<MatchMarriage>1</MatchMarriage>
<MatchEducation>6</MatchEducation>
<MatchOccupation>131071</MatchOccupation>
<MatchIncoming>126</MatchIncoming>
<MatchLocation>8611</MatchLocation>
<IsVIP>true</IsVIP>
<IsAuthenticated>false</IsAuthenticated>
<HeartCode>000000</HeartCode>
</BasicInfo>
<SupplyInfo>
<UserID>1</UserID>
<Gender>true</Gender>
<Birthday class="sql-timestamp">1980-11-17 00:00:00.0</Birthday>
<Nationality>1</Nationality>
<Astrology>8</Astrology>
<AnimalSign>9</AnimalSign>
<Weight>66</Weight>
<BodyType>2</BodyType>
<Occupation>4</Occupation>
<JobTitle>架构师</JobTitle>
<Industry>1</Industry>
<Institution></Institution>
<PhysicalLooking>8</PhysicalLooking>
<NativePlace>863508 </NativePlace>
<HaveChildren>1</HaveChildren>
<WantChildren>1</WantChildren>
<Religion>1</Religion>
<Smoking>2</Smoking>
<Drinking>2</Drinking>
<Favorites>
<FavoriteItem>
<Name>读书</Name>
<Value>1</Value>
<Category>Interest</Category>
</FavoriteItem>
<FavoriteItem>
<Name>听音乐</Name>
<Value>1</Value>
<Category>Interest</Category>
</FavoriteItem>
<FavoriteItem>
<Name>计算机/互联网</Name>
<Value>1</Value>
<Category>Interest</Category>
</FavoriteItem>
<FavoriteItem>
<Name>看电视</Name>
<Value>1</Value>
<Category>Interest</Category>
</FavoriteItem>
<FavoriteItem>
<Name>喝咖啡</Name>
<Value>1</Value>
<Category>Interest</Category>
</FavoriteItem>
<FavoriteItem>
<Name>轻音乐</Name>
<Value>1</Value>
<Category>Music</Category>
</FavoriteItem>
<FavoriteItem>
<Name>中国流行乐</Name>
<Value>1</Value>
<Category>Music</Category>
</FavoriteItem>
<FavoriteItem>
<Name>港台流行乐</Name>
<Value>1</Value>
<Category>Music</Category>
</FavoriteItem>
<FavoriteItem>
<Name>蓝调(Blue)</Name>
<Value>1</Value>
<Category>Music</Category>
</FavoriteItem>
<FavoriteItem>
<Name>欧美流行乐</Name>
<Value>1</Value>
<Category>Music</Category>
</FavoriteItem>
<FavoriteItem>
<Name>羽毛球</Name>
<Value>1</Value>
<Category>Sports</Category>
</FavoriteItem>
<FavoriteItem>
<Name>跳舞</Name>
<Value>1</Value>
<Category>Sports</Category>
</FavoriteItem>
<FavoriteItem>
<Name>极限运动</Name>
<Value>1</Value>
<Category>Sports</Category>
</FavoriteItem>
<FavoriteItem>
<Name>爱情</Name>
<Value>1</Value>
<Category>Movie</Category>
</FavoriteItem>
<FavoriteItem>
<Name>武打动作</Name>
<Value>1</Value>
<Category>Movie</Category>
</FavoriteItem>
<FavoriteItem>
<Name>动作片</Name>
<Value>1</Value>
<Category>Movie</Category>
</FavoriteItem>
<FavoriteItem>
<Name>警匪枪战</Name>
<Value>1</Value>
<Category>Movie</Category>
</FavoriteItem>
<FavoriteItem>
<Name>清淡</Name>
<Value>1</Value>
<Category>Food</Category>
</FavoriteItem>
</Favorites>
</SupplyInfo>
<PPSettings>
<UserID>1</UserID>
<AllowAll>true</AllowAll>
<AllowLoginUser>false</AllowLoginUser>
<AllowCredentialUser>false</AllowCredentialUser>
<AllowAuthorizedUser>false</AllowAuthorizedUser>
<AllowVIPUser>false</AllowVIPUser>
<Password>1</Password>
</PPSettings>
</ME>
<OPP>
<UserID>1913037</UserID>
<BasicInfo>
<UserID>1913037</UserID>
<Gender>true</Gender>
<Age>25</Age>
<UserName>leoliu</UserName>
<Height>175</Height>
<Marriage>1</Marriage>
<Education>2</Education>
<Income>3</Income>
<City>861111</City>
<Desc>
善良,诚恳,真诚 
热情,大方,谦虚 
乐观,开朗,好动 
善良,诚恳,真诚 
热情,大方,谦虚 
乐观,开朗,好动 
善良,诚恳,真诚 
热情,大方,谦虚 
乐观,开朗,好动 
善良,诚恳,真诚 
热情,大方,谦虚 
乐观,开朗,好动</Desc>
<LoveType>15</LoveType>
<PhotoUrl>SW1hZ2VHVUlEJDF8Rk1TUHwyMDA3XDVcMjJcfGQzOWE5MDM5LWFhYWMtNGJiYi04MjkwLWU3NzIy
MTg5YzM1ZnxKUEVH</PhotoUrl>
<MinAge>17</MinAge>
<MaxAge>30</MaxAge>
<MinHeight>160</MinHeight>
<MaxHeight>180</MaxHeight>
<MatchMarriage>1</MatchMarriage>
<MatchEducation>30</MatchEducation>
<MatchOccupation>131071</MatchOccupation>
<MatchIncoming>129</MatchIncoming>
<MatchLocation>8611</MatchLocation>
<IsVIP>false</IsVIP>
<IsAuthenticated>true</IsAuthenticated>
<HeartCode>000000</HeartCode>
</BasicInfo>
<SupplyInfo>
<UserID>1913037</UserID>
<Gender>true</Gender>
<Birthday class="sql-timestamp">1982-03-28 00:00:00.0</Birthday>
<Nationality>1</Nationality>
<Astrology>1</Astrology>
<AnimalSign>11</AnimalSign>
<Weight>75</Weight>
<BodyType>8</BodyType>
<Occupation>17</Occupation>
<Industry>1</Industry>
<PhysicalLooking>8</PhysicalLooking>
<NativePlace>864201 </NativePlace>
<HaveChildren>1</HaveChildren>
<WantChildren>3</WantChildren>
<Religion>1</Religion>
<Smoking>1</Smoking>
<Drinking>2</Drinking>
<Favorites>
<FavoriteItem>
<Name>运动</Name>
<Value>1</Value>
<Category>Interest</Category>
</FavoriteItem>
<FavoriteItem>
<Name>轻音乐</Name>
<Value>1</Value>
<Category>Music</Category>
</FavoriteItem>
<FavoriteItem>
<Name>电子音乐</Name>
<Value>1</Value>
<Category>Music</Category>
</FavoriteItem>
<FavoriteItem>
<Name>说唱</Name>
<Value>1</Value>
<Category>Music</Category>
</FavoriteItem>
<FavoriteItem>
<Name>羽毛球</Name>
<Value>1</Value>
<Category>Sports</Category>
</FavoriteItem>
<FavoriteItem>
<Name>乒乓球</Name>
<Value>1</Value>
<Category>Sports</Category>
</FavoriteItem>
<FavoriteItem>
<Name>篮球</Name>
<Value>1</Value>
<Category>Sports</Category>
</FavoriteItem>
<FavoriteItem>
<Name>足球</Name>
<Value>1</Value>
<Category>Sports</Category>
</FavoriteItem>
<FavoriteItem>
<Name>爱情</Name>
<Value>1</Value>
<Category>Movie</Category>
</FavoriteItem>
<FavoriteItem>
<Name>武打动作</Name>
<Value>1</Value>
<Category>Movie</Category>
</FavoriteItem>
<FavoriteItem>
<Name>清淡</Name>
<Value>1</Value>
<Category>Food</Category>
</FavoriteItem>
<FavoriteItem>
<Name>能填饱肚子就好</Name>
<Value>1</Value>
<Category>Food</Category>
</FavoriteItem>
</Favorites>
</SupplyInfo>
<PPSettings>
<UserID>1913037</UserID>
<AllowAll>false</AllowAll>
<AllowLoginUser>false</AllowLoginUser>
<AllowCredentialUser>false</AllowCredentialUser>
<AllowAuthorizedUser>false</AllowAuthorizedUser>
<AllowVIPUser>true</AllowVIPUser>
<Password></Password>
</PPSettings>
</OPP>
</Data>
先附上xsl, 名字为BasicTemplate.xsl
1
<?xml version="1.0"?>2
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:BitHelper="BitHelper">3
<xsl:output method="html" omit-xml-declaration="no" standalone="yes" indent="yes"/>4

5
<xsl:include href="lovetype.xsl"/>6
<xsl:include href="codeTab.xsl"/>7
<xsl:include href="city.xsl"/>8
<xsl:include href="ppsettings.xsl" />9
<xsl:include href="MatchControl.xsl" />10
<xsl:template match="/">11
<xsl:variable name="curUserID" select="Data/ME/BasicInfo/UserID"/>12
<xsl:variable name="oppUserID" select="Data/OPP/BasicInfo/UserID"/>13
<xsl:variable name="oppGender" select="Data/OPP/BasicInfo/Gender"/>14
<xsl:variable name="oppAge" select="Data/OPP/BasicInfo/Age"/>15
<xsl:variable name="oppHeight" select="Data/OPP/BasicInfo/Height"/>16
<xsl:variable name="oppEducation" select="Data/OPP/BasicInfo/Education"/>17
<xsl:variable name="oppCity" select="Data/OPP/BasicInfo/City"/>18
<xsl:variable name="oppIncome">19
<xsl:call-template name="GetIncome"><xsl:with-param name="Income" select="Data/OPP/BasicInfo/Income"/></xsl:call-template>20
</xsl:variable>21
22
<div class="PagePart">23
<div class="BasicInfo">24
<div class="UserName"><xsl:value-of select="Data/OPP/BasicInfo/UserName"/>.个人资料</div>25
<div class="FocusFont" id="OppUserIsFocus">(重点关注对象)</div>26
<div>27
<table border="0" cellspacing="0" cellpadding="5" width="100%">28
<tr>29
<td>性别:<xsl:if test="$oppGender='true'">男</xsl:if>30
<xsl:if test="$oppGender='false'">女</xsl:if>31
</td>32
<td>年龄:<xsl:value-of select="$oppAge"/>岁</td>33
</tr>34
<tr>35
<td>身高:<xsl:value-of select="$oppHeight"/>厘米</td>36
<td>婚姻状态:<xsl:call-template name="GetMarry"><xsl:with-param name="Marry" select="Data/OPP/BasicInfo/Marriage"/></xsl:call-template></td>37
</tr>38
<tr>39
<td>学历:<xsl:call-template name="GetDegree"><xsl:with-param name="Degree" select="Data/OPP/BasicInfo/Education"/></xsl:call-template></td>40
<td>月收入(元):<xsl:call-template name="ppsettings">41
<xsl:with-param name="PrivateContent" select="$oppIncome" />42
<xsl:with-param name="oppUserId" select="$oppUserID" /> 43
</xsl:call-template>44
45
</td>46
</tr>47
<tr>48
<td colspan="2">居住地:<xsl:call-template name="GetCity"><xsl:with-param name="City" select="Data/OPP/BasicInfo/City"/></xsl:call-template></td>49
</tr>50
</table>51
</div>52
53
54
<div class="credit">55
<xsl:element name="script">56
<xsl:attribute name="type">text/javascript</xsl:attribute>57
<xsl:attribute name="charset">gb2312</xsl:attribute>58
<xsl:attribute name="src">http://192.168.0.184:9006/serviceproxy.aspx?Type=/PageModules/Comm/Match2/CreditIdentifyForJava&OppositeID=<xsl:value-of select="/Data/OPP/BasicInfo/UserID"/></xsl:attribute>59
</xsl:element>60
</div>61
</div>62

63
<div class="MatchInfo">64
<xsl:call-template name="MatchCtrl"></xsl:call-template>65
</div>66
<div class="clear"></div>67
</div>68

69
<div class="PagePart">70
<table border="0" cellpadding="0" cellspacing="0" width="100%">71
<tr>72
<td class="HeadBar">她的择偶标准</td>73
<td><img src="../Images/bar2.gif" alt="" /></td>74
</tr>75
</table>76
77
<br />78
79
<div class="TextPart">80
<span class="bold">希望心中的他应该是:<br /></span>81
年龄:<span class="bold"><xsl:value-of select="Data/OPP/BasicInfo/MinAge"/>岁~<xsl:value-of select="Data/OPP/BasicInfo/MaxAge"/>岁,</span>82
身高:<span class="bold"><xsl:value-of select="Data/OPP/BasicInfo/MinHeight"/>cm~<xsl:value-of select="Data/OPP/BasicInfo/MaxHeight"/>cm,</span>83
婚姻状况:<span class="bold"><xsl:call-template name="GetMatchMarry">84
<xsl:with-param name="MatchMarry" select="Data/OPP/BasicInfo/MatchMarriage"/>85
</xsl:call-template>,</span>86
从事职业:<span class="bold"><xsl:call-template name="GetMatchOccupation">87
<xsl:with-param name="MatchOccupation" select="Data/OPP/BasicInfo/MatchOccupation"/>88
</xsl:call-template>,</span>89
教育程度:<span class="bold"><xsl:call-template name="GetMatchEducation">90
<xsl:with-param name="MatchEducation" select="Data/OPP/BasicInfo/MatchEducation"/>91
</xsl:call-template>,</span>92
平均月收入(元):<span class="bold"><xsl:call-template name="GetMatchIncome">93
<xsl:with-param name="MatchIncome" select="Data/OPP/BasicInfo/MatchIncoming"/>94
</xsl:call-template>,</span>95
居住地:<span class="bold"><xsl:call-template name="GetCity"><xsl:with-param name="City" select="Data/OPP/BasicInfo/MatchLocation"/></xsl:call-template></span>96
</div>97
98
</div>99
100
<div class="PagePart">101
102
<table border="0" cellpadding="0" cellspacing="0" width="100%">103
<tr>104
<td class="HeadBar">她的个人介绍</td>105
<td><img src="../Images/bar2.gif" alt="" /></td>106
</tr>107
</table>108
<br />109
<div class="TextPart">110
<xsl:value-of select="Data/OPP/BasicInfo/Desc"/>111
</div>112
</div>113

114
115
116
</xsl:template>117
</xsl:stylesheet>
1、在xsl中定义变量,用xpath取出数据,然后可以在用于显示的地方进行调用
<xsl:variable name="curUserID" select="Data/ME/BasicInfo/UserID"/>
<xsl:value-of select="$curUserID"/>
2、xsl也可以像导入控件一样来引入另一个xsl
<xsl:include href="lovetype.xsl"/>
3、条件语句的使用
<xsl:if test="$oppGender='true'">男</xsl:if>
<xsl:if test="$oppGender='false'">女</xsl:if>
$oppGender代表一个变量,值为true or false
这里的if语句没有else部分,要想使用else只能用when
语法如下:
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>
4、for-each 循环语句
<xsl:template match="/">
<TABLE>
<xsl:for-each select="//car">
<xsl:sort data-type="text" select="@id"/>
<TR>
<TH>
<xsl:text>Car-</xsl:text>
<xsl:value-of select="@id"/>
</TH>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>