
2007年08月22日 22:31:26
XML与XSL的“匹配”问题
|
WYH桑在工作之中遇到的一个问题(由于没接触过且时间紧急,故请人帮助,嘿嘿,其实身边的Me就会啊^_^),为了方便测试本人还是做了一个小练习。 内容是XSL与XML的“匹配”问题 A.XML文档如下: <?xml version="1.0"?> <?xml:stylesheet type="text/xsl" href="A.XSL" ?> <Root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <FirstName>Gao</FirstName> <LastName>Xiao</LastName> <Sex>Man</Sex> <Birth>1983-06-03</Birth> <Tel>010-12541254</Tel> <Country>China</Country> <City>BeiJing</City> <Street>NanJing Road</Street> <Salary>3000</Salary> <Time>2008-12-01</Time> </Root> A.XSL文档如下: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="whatever"> <msxsl:script implements-prefix="user" language="Javascript"> <![CDATA[ function formatTime(strTime) { return strTime.substring(0,4) + strTime.substring(5,7) + strTime.substring(8,10) + strTime.substring(11,13) + strTime.substring(14,16); } function formatDate(strDate) { return strDate.substring(0,4) + strDate.substring(5,7) + strDate.substring(8,10); } ]]> </msxsl:script> <xsl:template match="Root"> <H2>XML-XSL</H2> <SPAN>Name: </SPAN> <Name> <xsl:value-of select="FirstName"/><xsl:text> </xsl:text><xsl:value-of select="LastName"/><BR/> </Name> <Birth>Birth:<xsl:value-of select="user:formatDate(string(Birth))"/></Birth> <BR/> <Sex>Sex: <xsl:if test="Sex='False'">Women</xsl:if> <xsl:if test="Sex='True'">Man</xsl:if> <xsl:if test="Sex!='False' or Sex!='True' "><xsl:value-of select="Sex"/></xsl:if><BR/> </Sex> <Tel> <xsl:if test="Tel!=''">Phone:<xsl:value-of select="Tel"/></xsl:if> </Tel> <Address> <xsl:if test="Country!='' or City!='' or Street!='' ">Address:<xsl:value-of select="Street"/> <xsl:text> </xsl:text> <xsl:value-of select="City"/> <xsl:text> </xsl:text> <xsl:value-of select="Country"/> <xsl:text> </xsl:text>* </xsl:if> </Address> <Salary>Salary: <xsl:value-of select="format-number(string(Salary),'00')"/><BR/> </Salary> <Time>Time:<xsl:value-of select="user:formatTime(string(Time))"/></Time> </xsl:template> </xsl:stylesheet> 将二者放置于同一个目录下,在IE中浏览a.xml即可得到所需要的数据,结果如下: XML-XSL Name: Gao Xiao Birth:19830603 Sex: Man Phone:010-12541254 Address:NanJing Road BeiJing China * Salary: 3000 Time:20081201 |